Basics LINUX Commands


What is the Linux command:-
Linux commands are used to perform tasks in a Linux operating system. They are executed from the Linux terminal (or) command-line interface.
Some Category
Directory Commands
pwd
- Print working directorymkdir
- Make a directoryrmdir
- Remove directoryls
- List directory contents
File Commands
touch
- Create an empty filecat
- Concatenate and display file contentrm
- Remove the filecp
- Copy filemv
- Move or rename the file
File Content Commands
head
- Display the first lines of a filetail
- Display the last lines of a filemore
- View file page by pageless
- View file page by page with navigation
User Commands
su
- Switch to another userid
- Print current user and group IDsuseradd
- Add new userpasswd
- Change user password
Filter Commands
grep
- Search file contentcut
- Cut out selected fields from the filesort
- Sort lines of text fileswc
- Count lines, words and characters in files
Utility Commands
find
- Find files matching certain criterialocate
- Find files by namedate
- Print or set system date and timedf
- Report file system disk space usage
Networking Commands
ip
- Assign an IP address to interfaceping
- Test connectivity to network hostssh
- Log into remote machinestraceroute
- Trace route to network host
To view what's written in a file:-
cat
simply prints the contents of the files (your screen)
Example of command
Basic syntax:
cat [filename]
To view the contents of a single file:
cat test.txt
To view multiple files:
cat test1.txt test2.txt
You can also redirect the output of cat
to a file instead of displaying it:
cat test.txt > output.txt
This will write the contents of test.txt
to output.txt
.
The cat
command has some more options:
-n
: Display line numbers-s
: Suppress repeated empty lines-e
: Display end-of-line characters-T
: Display tab characters as ^I
To change the access permissions of files:-
Viewing File Permissions
You can view permissions of a file or directory using the ls -l
command:
ls -l filename
-rwxr-xr--
The permissions are displayed in 10 characters:
The first character indicates the file type (
-
for file,d
for directory)The next three characters are the permissions for the owner/user
The next three are for the group
The last three are for others
Changing File Permissions
You can change file permissions using the chmod
command.
Symbolic mode - Uses symbols like u
(user), g
(group), o
(others), a
(all) and +
/- (add/remove) permission.
example, to add write permission for the group:
chmod g+w filename
Absolute (octal) mode - Uses a three-digit octal number where each digit represents owner, group and other permissions respectively.
example, to make a file readable and writable by owner, readable by group and others:
chmod 644 filename
Here 6
is for owner (read + write), 4
is for group (read only) and 4
is for others (read only).
You can also change permissions recursively for directories using the -R
flag:
chmod -R 755 dirname
This will apply the permissions to all files and subdirectories within dirname
.
To check which commands you have run till now:-
The history Command
The history
command lists the commands you have entered in the terminal/CLI.
history
You can specify a number to limit the output:
history 100 # Shows the last 100 commands
The history
command stores your command history in the .bash_history
file in your home directory.
You can view and edit this file directly to view all commands across terminal sessions:
cat ~/.bash_history
vim ~/.bash_history
Repeating Commands
The history
command also allows you to easily repeat previous commands.
You can use !!
to repeat the last command:
!!
Or use a command number to repeat a specific command:
!342 # Repeats command number 342
You can also search for a command and repeat the last one that matches:
!ls # Repeats the last command that starts with "ls"
!?sudo # Repeats the last command that contains "sudo"
To remove a Directory/ Folder:-
Deleting Folders in Linux Command Line
There are two main commands used to delete folders in Linux
rmdir
rm
The rmdir
command is used to delete empty folders, while rm
can delete both empty and non-empty folders.
Using rmdir
The rmdir
command simply removes empty folders. It has the syntax:
rmdir folder_name
For example, to delete an empty folder named test
:
rmdir test
If the folder is not empty, rmdir
will give an error:
rmdir: failed to remove 'test': Directory not empty
In this case, you'll need to use the rm
command.
Using rm
The rm
command can delete both empty and non-empty folders.
-d
: Delete empty folders
-r
or -R
: Recursively delete folders and their contents
To delete an empty folder:
rm -d folder_name
To delete a non-empty folder:
rm -r folder_name
For example:
rm -r test
This will delete the test
folder and all its files and subfolders.
You can also use rm
with some other options:
-f
: Force removal without prompting-i
: Prompt before deleting each file-v
: Verbose mode
So a full command to delete a non-empty folder would be:
rm -r -f folder_name
The -f
option is useful to avoid having to confirm deletion for each file.
To create a fruits.txt file and to view the content:-
Using the touch
command
The simplest way is to use the touch
command:
touch fruits.txt
This will create an empty text file called fruits.txt
.
Using the echo
command
The echo
command can be used to create a file with some content:
echo "Apple" > fruits.txt
echo "Orange" >> fruits.txt
The first echo
will overwrite any existing file, while the second will append to the end.
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava:-
Adding Content to a Text File in Linux
You can add content to a text file and name it devops.txt
using the vim
editor in Linux. Steps:
- Open the file in
vim
vim devops.txt
Since the file is empty, you will see an empty buffer.
Start typing the content, one item per line
To write any thing
I
to get into insert mode.
apple
mango
banana
cherry
kiwi
orange
guava
Press
Esc
to exit insert mode.Save and close the file using:
:wq
- You can verify the content was added using the
cat
command:
cat devops.txt
Output:
apple
mango
banana
cherry
kiwi
orange
guava
Show only the top three fruits from the file:-
Using Head
The head
command displays the first few lines of a file. To display the top 3 lines
head -n 3 filename.txt
The -n
option specifies the number of lines to display.
example:
$ head -n 3 lines.txt
line 1
line 2
line 3
Using Tail and Head Together
We can also use tail
and head
together to display the top 3 lines
head -n 3 filename.txt | tail
This works because
head -n 3
displays the first 3 linesPiping that output to
tail
will then display the last 3 lines, which in this case are also the top 3 lines.
example:
$ head -n 3 lines.txt | tail
line 1
line 2
line 3
Using Sed
With sed
you can display a specific line range. To display the top 3 lines
sed -n '1,3p' filename.txt
The
-n
suppresses automatic printing of pattern space1,3p
prints lines 1 through 3
example:
$ sed -n '1,3p' lines.txt
line 1
line 2
line 3
To Show only bottom three fruits from the file:-
Using Tail
The tail
command is used to display the last few lines of a file. To display the last 3 lines
tail -n 3 filename.txt
The -n
option specifies the number of lines to display from the end of the file.
example:
$ tail -n 3 lines.txt
second last line
third last line
last line
Using Head and Tail Together
You can pipe the output of head
into tail
to display the last 3 lines:
head -n 1000 filename.txt | tail -n 3
This works because:
head -n 1000
displays the first 1000 linesPiping that output to
tail -n 3
will then display the last 3 lines from that output.To find the difference between fruits.txt and Colors.txt file:-
We can use the
diff
command to compare two text files and see the differences.example:
diff file1.txt file2.txt
It will output all lines that are different between the two files.
Happy Learning!
Thanks for Reading :)
Subscribe to my newsletter
Read articles from Afroz Shaik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
