EX.NO:1b UNIX COMMANDS
(I)GENERAL PURPOSE COMMANDS
- date
The date command is used to display the current date with day of week, month, time and the year.
Syntax: $ date
$date +[%format]
Example:
a) $ date
Fri Mar 4 09:13:05 IST 2011
b) $ date +%m – Displays only the current month (1..12)
03
c) $ date +%M – Displays only the minutes (00…59)
18
d) $ date +%d – Displays only the day of month (1…31)
04
e) $ date +%D – Displays the date in mm/dd/yy format
03/04/11
f) $ date +%h – Displays current abbreviated month name
Mar
g) $ date +%H – Displays the hour (00…23)
09
h) $ date +%Y – Displays the year
2011
i) $ date + %y – Displays the last two digits of the year
11
- cal
The cal command displays the specified month or year calendar
Syntax : $ cal [options]
$cal [month][year]
i)$ cal – Displays the current month calendar
ii) cal -3 – Displays the prev/current/next month calendar.
iii)$ cal 06 2011 – Displays the calendar for June 2011
iv) $ cal 2011 –Displays the calendar for the entire year 2011
- bc
Unix offers an online calculator and it can be invoked by the bc command.
Syntax: $ bc
When u type bc at the $ prompt, you are in the calculator mode and the $ prompt disappears. Type the input and press enter and observe the output. To quit calculator mode press ctrl d.
Example:
$ bc
3 + 4 * 5
23
- who
The who command displays the details about all the users who have currently logged into the system.
Syntax: $ who
Example : $ who
root tty1 2011-03-04 08:30
- who am i
The who am I command displays the details about the current user.
Syntax : who am i
Example : $ who am i
punitha pts/0 2011-03-04 09:04 (10.0.5.32)
- finger
The finger command displays the details about all the users who have currently logged into the system
Syntax: $ finger
Example : $finger
Login Name Tty Idle Login Time Office Office Phone
punitha punitha pts/0 Mar 4 09:04 (10.0.5.32)
- echo
The echo command is used to display the specified text on the screen
Syntax: $ echo “some text”
Example : $ echo “Good day”
Good day
(II)FILE RELATED COMMANDS
- cat
a)The cat command is used to create a file.
Syntax: $ cat > filename
Example : $ cat > sample
welcome to
unix lab
[press ctrl + d]
b)The cat command is used to view the contents of the specified file.
Syntax: $ cat filename
Example : $ cat sample
welcome to
unix lab
c)The cat command is used to append contents to an existing file.
Syntax: $ cat >> filename
Example: $ cat >> sample
This is our first program
[press ctrl + d]
$ cat sample
welcome to
unix lab
This is our first program
- cp
a)The cp command is used to copy the contents of one file to another file.
Syntax: $ cp sourcefile destinationfile
Example : $ cp sample test
$ cat test
welcome to
unix lab
This is our first program
b)The cp command is also used to copy a file to another directory.
Syntax: $ cp source_file destination_directory
Example : $ cp sample dir1
- mv
a)The mv command is used to remove a file from its current location and copy it to a new location.
Syntax: $ mv source_file destination_directory
Example: $ mv test dir1
b)The mv command is also used to rename a file.
Syntax: $ mv old_filename new_filename
Example: $ mv test test1
- ln
The ln command is used to establish an additional link for the same file.
Any modification made in the additional file or in the original file is reflected in both of them.
Syntax: $ ln existing_file new_file
Example : $ ln sample sample1
- rm
The rm command is used to remove the specified file.
Syntax: $ rm filename
Example: $ rm sample
- cmp
The cmp command is used to compare two files. It tells u whether two files differ and if they differ, it reports the position in the file where the first difference occurs.
Syntax : $ cmp filename1 filename2
Example : $ cat file1
welcome to
unix lab
$ cat file2
welcome to
linux lab
$ cmp file1 file2
file1 file2 differ : byte 12, line 2
- comm
The comm command compares two sorted files line by line.
It produces a three-column output. Column one contains lines unique to file1, column two contains lines unique to file2, and column three contains lines common to both files.
Syntax: $ comm [options] filename1 filename2
Following are the different options.
a) -1 suppress lines unique to file1
b) -2 suppress lines unique to file2
c) -3 suppress lines that appear in both files
Example : $ cat file3
one
three
$ cat file5
one
five
i)$ comm file3 file5
one
five
three
ii)$ comm -1 file3 file5
one
five
iii)$ comm -2 file3 file5
one
three
iv)$ comm -3 file3 file5
five
three
- paste
The paste command joins files together line by line.
Syntax : $ paste filename1 filename2
Example : $ cat countries
India
Pakistan
Australia
$ cat capitals
Newdelhi
Islamabad
Canberra
$ paste countries capitals
India Newdelhi
Pakistan Islamabad
Australia Canberra
- join
The join command joins two files together on the basis of a key field that contains entries common to both of them.
Syntax : $ join filename1 filename2
Example : $ cat country_cap
India Newdelhi
Pakistan Islamabad
Australia Canberra
$ cat players
India Sachin
Pakistan Afridi
Australia Ponting
$ join country_cap players
India Newdelhi Sachin
Pakistan Islamabad Afridi
Australia Canberra Ponting
(III) DIRECTORY RELATED COMMANDS
- pwd
The pwd command displays the path of the current working directory
Syntax: $ pwd
Example:
[punitha@localhost ~]$ pwd
/home/staff/punitha
- mkdir
The mkdir command is used to create a new directory in the current working directory.
Syntax: $ mkdir directory_name
Example:
[punitha@localhost ~]$ mkdir sample
- ls
The ls command displays the list of files in the current working directory.
Syntax: $ ls
$ ls [options]
Following are the different options.
a)ls -l – The files are displayed with their mode, number of links, owner of the file, file
size, modification date & time and filename.
b)ls -t – The files are displayed in the order of last modification time.
c)ls -a – Displays all the files including the hidden files.
d)ls -s – Displays the size of each file.
Example:
[punitha@localhost ~]$ ls
file1 file2 sample new
[punitha@localhost ~]$ ls -l
-rw-rw-r– 1 punitha punitha 26 2011-02-23 10:42 file1
-rw-rw-r– 1 punitha punitha 38 2011-02-22 10:40 file2
drw-rw-r– 1 punitha punitha 15 2011-02-22 10:41 sample
-rw-rw-r– 1 punitha punitha 20 2011-02-22 10:44 new
- cd
a)The cd command is used to change from the current working directory to the specified directory.
Syntax: $ cd directory_name
Example :
[punitha@localhost ~]$ cd sample
[punitha@localhost sample] $
b)The cd command when given without any argument takes you to the home directory.
Syntax: cd
Example:
[punitha@localhost example]$ cd
[punitha@localhost ~]$
c)The cd command when used with two dots takes you to the parent directory of the current working directory.
Syntax: cd ..
- rmdir
The rmdir command removes the specified directory. It requires the specified directory to be empty before removing it.
Syntax: $ rmdir directory_name
Example:
[punitha@localhost ~]$ rmdir sample
A pipe is a mechanism in which the output from one command can be redirected as input to another command.
To send the output of one command as input for another, the two commands must be joined using a pipe ( | ) character.
Example:
1. $ who | wc -l
The output of the command “who” is passed as input to the “wc -l” command.
The above example displays the number of users who have currently logged into the system.
2. $ ls | wc -l
The output of the command “ls” is passed as input to the “wc -l” command.
The above example displays the number of files present in the current working directory.
3. $ ls | grep “^f”
The above example lists all the files beginning with the character ‘f’ on the screen.
4. $ ls | tail -5
The above example displays the list of last 5 files present in the current working directory.
(IV) INPUT/OUTPUT REDIRECTION
The standard input device is the keyboard and the standard output device is the screen.
Input can be taken from sources other than the standard input and output can be passed to any source other than the standard output. Such a process is called redirection.
INPUT REDIRECTION
When the input for a command is taken from sources other than the standard input device, then the process is called as input redirection.
Syntax: $ command < filename
Example : $ wc < file1
In the above example, the command “wc” takes its input from the file “file1”.
OUTPUT REDIRECTION
When the output from a command is sent to sources other than the standard output device, then the process is called as output redirection.
Syntax: $ command > filename
Example: who > file2
In the above example, the output of who command is sent to the file “file2” instead of being displayed on the screen.
(V) FILTERS
A filter is a command which receives data from the standard input, processes it, and sends the results to the standard output.
- wc
The wc command counts the number of lines, words and characters in the specified file.
Syntax : $ wc [options] filename
Following are the different options.
a)- l counts the number of lines
b)- w counts the number of words
c)- c counts the number of characters
Example : $ cat file1
welcome to
unix lab
i)$ wc file1
2 4 20 file1
ii)$ wc -l file1
2 file1
iii)$ wc -w file1
4 file1
iv)$ wc -c file1
20 file1
- sort
The sort command sorts the contents of a file.
Syntax : $ sort [options] filename
Following are the different options.
a)-n Sorts in numeric order
b)-r Sorts in reverse order
c)-u Sorts and displays only the unique output.
Example : $ cat countries
India
Pakistan
Australia
Australia
i)$ sort countries
Australia
Australia
India
Pakistan
ii)$ sort -u countries
Australia
India
Pakistan
iii)$ sort -r countries
Pakistan
India
Australia
Australia
- head
The head command displays the first 10 lines of the specified file by default.
Syntax : $ head [options] filename
Following are the different options.
a)-n Displays the first n lines of the specified file.
b)-cn Displays the first n characters of the specified file.
Example : $ cat countries
India
Pakistan
Australia
Australia
i)$ head -2 countries
India
Pakistan
ii)$ head -c3 countries
Ind
- tail
The tail command displays the last 10 lines of the specified file by default.
Syntax : $ tail [options] filename
Following are the different options.
a)-n Displays the last n lines of the specified file.
b)-cn Displays the last n characters of the specified file.
Example : $ cat countries
India
Pakistan
Australia
Australia
i)$ tail -2 countries
Australia
Australia
ii)$ tail –c4 countries
lia
- uniq
The uniq command removes repeated lines from the specified file.
Syntax : $ uniq [options] filename
Following are the different options.
a)-d Displays only the lines that are duplicated in the input file.
b)-u Displays only the lines with single occurrences.
c)-c Precedes each line displayed by the number of times it occurs.
Example : $ cat countries
India
Pakistan
Australia
Australia
i)$ uniq countries
India
Pakistan
Australia
ii)$ uniq –d countries
Australia
iii)$ uniq –u countries
India
Pakistan
iv)$ uniq –c countries
1 India
1 Pakistan
2 Australia
- grep
The grep command is used to search for a particular pattern from a file and display those lines on the standard output.
Syntax : $ grep [pattern] filename
Example : $ cat countries
India
Pakistan
Australia
Australia
i) grep “dia” countries
India
ii) grep “^A” countries (Displays lines that start with A)
Australia
Australia
iii) $ grep “n$” countries (Displays lines that end with n)
Pakistan
- nl
The nl command adds line numbers to a file and displays the file.
Syntax : $ nl filename
Example : $ cat countries
India
Pakistan
Australia
Australia
$ nl countries
1 India
2 Pakistan
3 Australia
4 Australia
- cut
The cut command selects particular columns or fields from the specified file and prints them on the standard output.
Syntax : $ cut [options] filename
Following are the different options.
a)–c Selects the specified characters from the file and displays them.
b)–f Selects the specified fields from the file and displays them
Example : $ cat state_cap
Tamilnadu Newdelhi Tamil
Andhra Hyderabad Telugu
Karnataka Bangalore Kannada
[Note:] Use tab to separate fields.
i) $ cut -f1 state_cap
Tamilnadu
Andhra
Karnataka
ii) $ cut -f1,3 state_cap
Tamilnadu Tamil
Andhra Telugu
Karnataka Kannada
iii) cut -c1 state_cap
T
A
K
- colrm
The colrm command removes selected columns from the specified file.
The colrm command, if called with one parameter the columns of each line will be
removed starting from the specified column. If called with two parameters the columns from the first column to the last column will be removed.
Syntax : $ cut [startcol] [endcol] < filename
Example : $ cat countries
India
Pakistan
Australia
Australia
i) $ colrm 3 < countries
In
Pa
Au
Au
ii) $ colrm 2 4 < countries
Ia
Pstan
Aralia
Aralia
- tr
The tr command translates characters present in the standard input, and writes them to the standard output.
Syntax : $ tr [input_characters] [output_characters] < filename
Example : $ cat countries
India
Pakistan
Australia
Australia
i) $ tr a A < countries
IndiA
PAkistAn
AustrAliA
AustrAliA
ii) $ tr ‘[a-z]’ ‘[A-Z]’ < countries
INDIA
PAKISTAN
AUSTRALIA
AUSTRALIA
- more
The more command displays the contents of a file on the screen page by page. The next
screen can be viewed by pressing the space bar.
Syntax : $ more filename.