UNIX Commands With Examples

Commands in UNIX are broadly Classified into Four types
  1. Environmental Commands
  2. File Manipulation
  3. Directory Manipulation
  4. Process Control 

   Basic commands :

man <command>
Shows the manual help page for the <command> 
man –f <command>
Provides one line details about the <command>  
man –k <keyword>
Provides a list of summary info about the manual sections in the keyword database for specified keyword <keyword>
logout or exit 
Exit command Prompt and Close the Shell  
Changing the current user password 
Displays the Date and Time  
Who am i  
Displays the Current Logged in User data   
List all users logged onto the Current workstation  
ftp hostname  
File Transfer Protocol to transfer file between local and remote workstations. Remote workstation is named hostname
telnet hostname  
log onto remote workstation named hostname  
df -k  
print list of disks and current capacity (in kilobytes)  
Displays the list of environmental variables  
which <command> 
list location of executable <command>   

Calendar Commands:
cal 2003
Shows calendar for the year 2003
cal 2 1997
Shows the calendar for February 1997
cal dec 1995
Shows calendar for December 1995
cal ja 1998
Shows calendar for January 1998
cal ju 2000
Shows calendar for June 2000
cal f 1999
Shows calendar for February 1999

Uname Commands :
uname –n
Shows the name of the Unix server being used in the network
uname –r
Show the version no of the Operating system

Script Commands:
Helps to record login session in a file called: ‘typescript’ in the current directory. All the commands, their output and error messages are stored for later view.
After starting the scripting, user continues with his job. All the commands he uses, their output and error messages are stored for later view
When the user exits from the scripting(writing : exit from OS prompt), the script file is saved and a message is shown : Script done, file is typescript
 script –a
Append activities to existing file : typescript
 script newfile
To log activities to a new file : newfile

Create Commands :
cat >file1
Creates file file1 where a user enters text and presses <Ctrl-D> to end text editing
cat >>file1
Append lines to existing content of file : file1 and is ended when <Ctrl-D> is pressed
cat file1
Shows the contents of the file: file1
cat file1>file2
Copies the contents of file : file1 into new or existing file : file2
cat file1 file2 > file3
Concatenates the content of file1 and file2 and places it into new or existing file file3
cat file1 >>file2
Appends the contents of file1 after the last line of file2. If file2 does not exist, new file is created

Copy Commands :
Copies[ one file to another file]  [one directory to other directory]  [files into directories]
Synatax: cp [-flag] [ old files/directories] [ new files/directories]
 cp –I <old dir/file>  <new dir/file>
Interactive copying : prompt appears for user accent before copying
 cp –f <old dir/file>  <new dir/file>
Force copying
 cp –p <old dir/file>  <new dir/file>
Preserve permissions. Preserves modification time, access time, file mode, user id, user group etc.
 cp –r <old dir/file>  <new dir/file>
Recursive copying

Move Commands :
  •     Moves a  file to new or existing file .
  •     Moves One/more files to existing directory.
  •         Moves One or more directories to a new or existing directory
  •     Moves If the access permission of the destination directory or existing destination file forbids writing, mv command asks for overwriting the file 
mv –f <old dir/file> <new dir/file>
Performs move operation without prompting for permission
mv –i <old dir/file> <new dir/file>
Interactive moving

Remove Commands:
  •     Removes the entries for one/more files from a directory.
  •     Destroys the file whose last link is deleted
  •     Removal of a file requires write permission for that file
  •     Removal of a file from a directory required write and execute permission in the directory

      Syntax: rm [-f|-i] [ -r|-R] [files|directories]  
rm –f <file/dir>
Forceful deletion
rm –i <file/dir>
Interactive deletion
rm –r <file/dir>
Recursively delete the entire contents of the directory before removing the directory itself

     Example of rm commands
mv *.txt  ../testdir
Moves all the files with .txt extension under the directory testdir residing under the parent directory of the current directory
 rm *.temp
Remove all files with extension : .tmp under the current directory
 rm –r backupdir
Remove the directory backupdir with all its contents

Word count Commands :
Counts the number of words/bytes/characters/lines in a file
Syntax :   wc [flag] <filename(s)>
wc –c file1
Counts the total no. of words of file1
wc –l file1
Counts the total no. of lines of file1
wc –w file1
Counts the total no. of characters of file1

Current directory command:
Navigates from the current directory to another directory
cd scripts
cd ./scripts
Moves to directory : scripts under current working directory
cd ../program
 Change to directory program residing in the current directory's parent directory
cd /usr/fin/user/ manfiles
Change to the directory whose absolute pathname is : /usr/fin/user/ manfiles
cd ~
Move to home directory of the user
cd ..
Move to the parent directory of the current working directory
cd ~/sample1
Move to folder : sample1 under the home directory for the user

Chmod commands:
Any permission on a directory percolates down to the files and sub-directories under it.
Syntax:  chmod   <flag>   <filename(s)|directory name(s)>
chmod 744 file1
Grant all permissions to : User, and read permission to group and others
chmod 776
Grant all permission to user and group, read and write permission to others
chmod 777 file1
Grant all permission to all

     Representation of permissions:
Read permission
Write permission
Execute permission
User creating the file
Other users in the same group of the creator
Any other users
All(creator, other users in the same group, and other users)
      Example of grating authorization :
chmod u=rwx,go=r file1
Assign read,write and execute permission to user, but only read permission to group and others
chmod o-x file1
Revoke execute permission from others
 chmod a+x file
Assign execute permission to everybody
 chmod –R ug+r,o-r,a+x /home/ems2000
Traverse the directory subtree under directory /home/ems2000 making all regular files readable by user and group only, revoke read permission from others and grant execute permission to all

Creates directory Command:
  Syntax :  mkdir [flag] <directory name(s)>
Intermediate directories are created as necessary.Otherwise, the full path prefix of dirname must already exist.  mkdir requires write permission in the parent directory.
-m <mode>
Mode of permission for the directory and all the files under it

  Example of mkdir command :
Mkdir –m755 testdir
 testdir is created with permission = 755
mkdir –p testdir/sample1
If testdir is not created, it is first created. Then, a directory : sample1 is created under the directory testdir

Remove Directory Command:
Removes the directory entry for each empty directory referred
Syntax :  rmdir [-f|-I|-p] <directory>
Interactive removal of empty directory

sdiff command:
Syntax : sdiff [flag] <file1> <file2>
Compares both the files side by side and get the difference of them.
Only print on the left hand side where columns are identical
Do not print identical lines

Example of sdiff command:
$cat file1
(created file with below content)
$cat file 2
(created file with below content)
$sdiff file1 file2
(result after differentiating)


1 !  2
Johnny   johnny
              > polo
3               3
cricket     cricket
bottle   < 

I/O redirection & Piping :
      redirect output to a file
forcefully redirect output to a file
append output to the end of the file
pipe output to another command
input redirection

Saving work in a File:
Save the changes made to the file
:w <filename>
Same as “Save As..” in windows. Saves the contents to the specified file <filename> . If it does not exist previously, a new file is created
:w! <filename>
Save the changes to file <filename>, if the file already exists
:w >> <file1>
Append the contents of the opened file after the last line of the file <file1>. File <file1> should exist previously
:<n1>,<n2>w <newfile>
Copies the contents of lines <n1> to <n2> into a new file <newfile>
:<n1>,<n2>w! <newfile>
Moves the contents of lines <n1> to <n2> into an existing file <newfile> , overwriting its previous contents
:.,.+<n>w <newfile>
Appends from current line to <n> number of lines below it into file <newfile>
:.,.+<n>w >> <nextfile>
Appends from current line to <n> number of lines below it after the last line of the file <nextfile>
Quits the file editing in vi, provided no unsaved change remains
Quits vi neglecting all the unsaved changes made to the file
:wq or :x
Save the unsaved changes in the opened file and quit vi editor