The syntax for all Linux command is as follow.
command [- options] [arguments]
1. man command
command [- options] [arguments]
The 'command' is a Linux command. Options are always
followed by a hyphen (-) sign. Only one hyphen is used if a user want to use
more than one options. Options are used to change the way of working of command.
Options are followed by arguments. The Linux commands perform operations on the
arguments.
$ ls -l /bin
In the above example 'ls' is a command. '-l' is an option
and '/bin' is an argument.
1. man command
Stands for Manual-gives help about Linux commands
Examples
Examples
$ man cat
Above command will display information
about cat command.
$ man -k file
-k stands for keyword. Gives information
about commands related to the files
2. ls command
ls - used to display the list of
sub-directories and files in the directory.
Examples
Examples
$ ls
Displays the list in current directory.
$ ls /bin
Displays the list in /bin directory
$ ls -l
Displays the long list-more detailed
information
3. cat command
cat - used to create, show, copy and
conatenate files.
Examples
$ cat > abc.txt
$ cat > abc.txt
Creates a fie named abc.txt.
$ cat abc.txt
Displays the content of abc.txt file.
$ cat abc.txt > xyz.txt
Copies the content of abc.txt to
xyz.txt-overwrites the content of xyz.txt.
$ cat abc.txt >> xyz.txt
Copies the content of abc.txt to
xyz.txt-append the content of abc.txt to the end of xyz.txt.
$ cat abc.txt xyz.txt >con.txt
Concatenate the content of abc.txt and
xyz.txt and write to the con.txt.4. date command
Used to check and set date.
Examples
$ date
Print the system date in default format,
$ date “Date is %d-%m-%y”
Print the date in specified format.
$ date –d “May 20 2019”
Print the specified date instead of system date.
Print the system date in default format,
$ date “Date is %d-%m-%y”
Print the date in specified format.
$ date –d “May 20 2019”
Print the specified date instead of system date.
$ date –d “next sun”
Print the date of next Sunday.
$ date
–d “1 month ago”
Displays the date one month ago from the
current date.
Displaying dates from a file. First we need to create a file having dates.
$ cat >myfile
Dec 11 2019 16:50:22
Nov 18 2011
April 22 1960
Ctrl + d
$ date –r myfile
Above command displays the dates from the file 'myfile'
$ date +%Y%m%d -s "20200110"
$ date -s "01 JAN 2020 11:14:00"
Both the commands above sets the date to the specified date.
Above command displays the dates from the file 'myfile'
$ date +%Y%m%d -s "20200110"
Both the commands above sets the date to the specified date.
5. mkdir command
This command is used to create new directories.
Examples
$ mkdir mydir
Above command will create a directory named 'mydir' in
the current directory.
$ mkdir mdir1 mdir2
Above command will
create two directories in the current directory.
6. cd command
The cd command is used to change the current working
directory.
Examples
$ cd mydir
Above command will change the current working directory
to 'mydir'.
$ cd or cd ~
Both the commands will change the current working
directory to user's home directory.
$ cd ..
This command moves to the parent directory of current
working directory.
$ cd /
Move to the root directory.
$ cd /bin
Above command will change the current working directory
to the /bin directory.
Examples
Examples
33. diff command
7. pwd command
Stands for Print Working Directory. This
command is used to show the present working directory.
Examples
$ pwd
8. rmdir command
This command is used to delete empty
directories.
Examples
$ rmdir mydir
Above command will delete ‘mydir’
directory from the file system if it is empty.
$ rmdir –r mydir
Above command
will delete the directory even if it is not empty. –r means
recursively-recursively delete files in the directory.
9. touch command
This command
updates the access time and modification time of file. It can also create a
file if file does not exist.
Examples
$ touch myfile
This command
will change the time stamp of ‘myfile’ file if this file exists. Otherwise it
will create this file.
$ touch -a
myfile
Changes only
Access Time.
$ touch -m
myfile
Changes only
Modification Time.
10. cal command
'cal' stands for
calendar. This command is used to display the calendar.
Examples
$ cal
This command
displays the current month of current year.
$ cal 1992
This command
will display the whole calendar of year 1992.
$ cal 1 2012
This command
will display the first month of the year 2012.
$ cal –m
Above command
will display Monday as the first day of the week.
11. tput command
'tput' command is
used to control the terminal settings.
Examples
$ tput clear
This command
clears the terminal screen.
$ tput cup 5 6
‘cup’ stands for
cursor position. Moves the cursor to the 6th column of the 5th
line.
$ tput line
This command
displays the number of lines.
$ tput bold
This command set
the bold attribute.
$ tput sgr0
This command
reset all the attributes.
$ tput smul
Above command
starts the underline mode.
$ tput rmul
Above command
ends the underline mode.
$ echo `tput
bold` Linux `tput sgr0`
This command
prints the Linux in bold.
$ tput cuu 7
Moves the cursor
7 lines up.
$ tput cud 6
Moves the cursor
6 lines down.
$ tput rc
This command
reset the cursor position.
$ tput setf 4
This command will
set the text color to red.
$ tput setb 4
This command will
set the background color to red.
12. expr command
'expr' command is
used to perform arithmetic operations on integer values and string opeartions.
Arithmetic operators (+, -, *, / and %) , Relational operators (=, !=, <,
>, <= and >=) and Logical operator (&, | and !) can be used with
this command.
Examples
$ expr 4+5-2
The result of
above command is 7.
$ expr 9 = 10
The above
command returns 0 (false).
$ expr 9 = 9
The above
command returns 1 (true).
$ expr length
Linux
Returns the
length of string. The result of this command is 5. The string ‘Linux’ has 5
characters.
$ expr index
Linux n
This command
will return the index of character ‘n’ in string ‘Linux’. Output is 3.
$ expr substr
Redhat 4 3
This command
will return ‘hat’. ‘substr’ return the sub string from 4th index to
the length of 3.
13. bc command
‘bc’ stands for
Basic Calculator. This command is used to perform basic calculation even on floating
values.
Write ‘bc’ on
the command prompt. Then ‘bc’ takes the control and starts its own prompt. Here
a user can perform arithmetic operations as given below.
Examples
$ bc
5+5
10
Press ctrl+d to
get out of ‘bc’ command.
$ echo “4.9+1.2”|
bc
This command
will add 4.9 and 1.2.
$ echo “a=6;++a;”|bc
The output of
above command will be 7.
$ echo ‘if
(4>5) print “Greater “ else print “Smaller”’|bc
14. uname command
This command is
used display information about the hardware and operating system.
Examples
$ uname –a
Provides information
about everything.
$ uname –m
This command
provides the information about the machine.
$ uname –n
This command will
display the hostname of the computer.
15. tr command
‘tr’ command is
used to translate characters in a string.
Run this command
on the command prompt. Then type strings and press enter. Press ctrl+d to exit
from the ‘tr’ command.
Examples
$ tr a o
Type strings
here
Ctrl+d
$ echo “Hit”|tr i o
The output of
this command will be ‘Hot’. The character ‘a’ will be replaced with ‘o.
$ cat myfile | tr
'[A-Z]' '[a-z]'
Here ‘myfile’ is
a file name. This command will convert all the upper case letters into lower
case.
16. who command
‘who’ command is
used to display the list of users who are logged in.
Examples
$ who
Above command
will display the list of users.
$who am i
This command
displays the information about the user who run this command.
17. cp command
This command is
used to copy files from one directory to another.
Examples
$ cp
/home/mydir1/myfile1.txt /home/mydir2/
This command
will copy the file ‘myfile1.txt form the ‘mydir1’ directory to ‘mydir2’
directory with same name.
$ cp
/home/mydir1/myfile1.txt /home/mydir2/myfile2.txt
This command
will also copy the file but the file will be renamed to ‘myfile2.txt’.
18. mv command
This command is
similar to the ‘cp’ command. The purpose of this command is to rename and move
files.
Examples
$ mv /home/mydir1/myfile1.txt /home/mydir2/
Above command
will move the ‘myfile1.txt’ from the ‘mydir1’ to ‘mydir2’ directory.
$ mv /home/mydir1/myfile1.txt /home/mydir2/myfile2.txt
This command
will move and rename the file.
19. rm command
This command is
used to remove one or more files.
Examples
$ rm myfile.txt
This command
will remove myfile.txt from the storage.
$ rm –d mydir
Above command
will remove the directory mydir.
$ rm file1 file2
file3
This command
will remove multiple files.
$ rm –i
myfile.txt
‘i’ stands for
interactive. Confirms before deleting file.
20. find command
The ‘find’
command finds the file by name, type, owner, group, date or permission.
Examples
$ find file1.txt
The above
command will search the file1.txt.
$ find –atime +8
This command
will display the files accessed before 8 days.
$ find –atime -8
This command
will display the files accessed after 8 days.
$ find $HOME –print
Lists all files
and directories in home directory.
21. ln command
‘ln’ command is
used to create soft link (like windows shortcut) or hard link of file and
directories.
Examples
$ ln /etc/passwd /user/home
This command
will create a (hard) link of ‘passwd’ file in the user’s home directory.
$ ln –s
/etc/passwd /user/home
This command
will create a soft link of ‘passwd’ file in the user’s home directory.
22. more command
‘more’ command
is used to display the output page by page.
Examples
$ more
myfile.txt
This command
will display the content of file page by page.
$ ls –lr | more
This command
will display the list of file and directories page by page.
23. less command
‘less’ command
is similar to the ‘more’ command. This command need to press ‘q’ to quit.
Examples
$ less
myfile.txt
24. Redirecting
Input and Output
Most of Linux
commands take input from standard input and outputs to standard output. The
input and output of the commands can be redirected to the files.
A. Input
Redirection
Command can get input from a file.
Examples
$ tr a A<myfile.txt
‘tr’ command
cannot take input from a file so we need to redirect its input. In the above
example ‘tr’ command will replace the ‘a’ with ‘A’ in the file myfile.txt
B. Output
Redirection
By default
command displays the output on the screen. The output of commands can be
redirected to the files using > or >> signs.
$ ls
–l>myfile.txt
This command
will redirect the output to the file ‘myfile.txt’ instead of showing on the
screen.
$ man
ls>>myfile.txt
‘>’ overwrite
the content of existing file. If a user wants to append the data at the end of
file he needs ‘>>’ sign.
25. Piping
With the help of
piping the output of one command can be the input of other command. A user can
combine the features of different commands.
Examples
$ ls | wc –c
The output of
‘ls’ command will be the input of ‘wc’ command. So this command will count the
number of files and directories in the current directory.
$ ls –lr | more
This command
will show the output of ls command page by page.
$ sort
myfile.txt | uniq
This command
will sort the content of file myfile.txt and display the unique content of
file.
26. tee command
The ‘tee’
command helps to redirect the output as well as display the output on the
screen. This command is used with pipe.
Examples
$ ls –l | tee
sfile.txt
Above command
will display the output on the screen and it will also store the output in the
file sfile.txt.
27. grep command
The ‘grep’
command is used search words in files using regular expression.
Examples
$ grep linux myfile.txt
$ grep ‘linux’ myfile.txt
This command
displays the lines having ‘linux’.
$ grep –c linux myfile.txt
This command
will count the number of occurrence of word ‘linux’ in file myfile.txt.
$ grep -c
/bin/tcsh /etc/passwd
Lists the number
of users who use tcsh shell.
$ grep ^linux
myfile.txt
$ grep ‘^linux’
myfile.txt
This command
finds ‘linux’ in the beginning of the line.
$ grep ‘linux$’
myfile.txt
This command
finds ‘linux’ at the end of the line.
$ grep “[aeiou]”
myfile.txt
This command
displays the lines having any character among a, e, i, o or u.
28. head command
The ‘head’
command is used to print the first (Bydefault Top 10) line of one or more
files.
Examples
$ head myfile.txt
This command
will print the top 10 lines of the file myfile.txt
$ head -20
myfile.txt
This command
will print the top 20 lines of the file myfile.txt
$ ls| head -5
This command will display the 10 files in the current directory.
29. tail command
This command
displays the last (Bydefault 10) lines of the file.
Examples
$ tail
myfile.txt
This command will
display the last 10 lines of the file.
$ tail -15 myfile.txt
This command will
display the last 15 lines of the file.
$ ls | tail 5
This command will display last 5 files in the current directory.
$ head -15
myfile.txt | tail 10
This command
will display lines between 5th and 15th line.
30. cut command
The ‘cut’
command allows a user to extract selected columns or fields from one or more
files.
Examples
$ cut –c2,4
mydata.txt
This command
will display 2nd and 4th
column from the file mydata.txt.
$ cut –c3-5
mdata.txt
The above
command will display columns from 3rd column to 5th column.
$ cut –d”:” –f3,6
mydata.txt
This command
will extract fields rather than columns. Delimiter is required to specify if
default (tab) is not used.
31. paste command
The ‘paste’ command
is used to combine the fields of one or more files.
Examples
$ paste file1 file2
This example
will combine the line of file1 and
file2.
$ paste –d”:”
file1 file2
The option ‘–d’
is used to specify the delimiter.
32. cmp command
The ‘cmp’
command is used to compare the content of files.
Exmaples
$ cmp file1 fil2
The above
command will display the differences between both files.
$ cmp –s file1
file2 && echo “No changes”
This command
will display ‘No changes’ if files are not different.
33. diff command
Comments
Post a Comment