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 :

Command
                                                               Significance
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  
passwd 
Changing the current user password 
date 
Displays the Date and Time  
Who am i  
Displays the Current Logged in User data   
who  
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)  
setenv  
Displays the list of environmental variables  
which <command> 
list location of executable <command>   

Calendar Commands:
Command
Significance
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 :
Command
Significance
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
Command
Significance
 script –a
Append activities to existing file : typescript
 script newfile
To log activities to a new file : newfile


Create Commands :
Command
Significance
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]
Command
Significance
 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 
Command
Significance
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]  
Command
Significance
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
Example
Significance
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)>
Command
Significance
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
Command
Significance
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
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)>
Command
                                           Significance
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:
              Value
                                           Significance
r
Read permission
w
Write permission
x
Execute permission
u
User creating the file
g
Other users in the same group of the creator
o
Any other users
a
All(creator, other users in the same group, and other users)
  
      Example of grating authorization :
Example
                                    Significance
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)>
Flag
 Significance
-p
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 :
Example
Significance
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>
Flag
Significance
-i
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.
Flag
Significance
-l
Only print on the left hand side where columns are identical
-s
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
fourthbottle
3
cricket
bottle

2
fourthbottle
polo
3
cricket

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

I/O redirection & Piping :
Symbol
Significance
> 
      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:
             Command
                    Significance
:w
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>
:q
Quits the file editing in vi, provided no unsaved change remains
:q!
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