You are on page 1of 365

Unix Overview

Concept,presentation and training by Biswa

Expectation from this training


This is not an exhaustive training on Unix , covering all the arenas with full details. Even the topics covered in this presentation are not all inclusive.

But, it will definitely help you to be a confident sailor in the ocean of Unix no matter what the circumstance is.

Dont be nervous,please
This training will teach you about

90 commands!!! 350 slides!!!!

And will make you bore with more than

But dont be nervous remember the utilities of the commands for this presentation will be with you for the rest of your life to provide you the syntax for using them.

Duration 15 Hours ( 3 Days)

Schedule 29th Sept-1st Oct,2003

Timings 11:00 am 1 pm 2:00 pm 5 pm

Purpose
Get introduced to Unix Operating System

To get acquainted with the basic Unix commands

To learn a bit of Shell Programming

Operating System
An operating system is a living, breathing software entity. The soul of the computing machine, it is the nervous system that turns electrons and silicon into a personality. It brings life to the computer - from Mike Gancarz's "The UNIX Philosophy"

The OS sits between users and hardware providing translation services. It speaks the language of the hardware to perform basic tasks such as the definition of memory or the allocation of disk space using the hardware.

Unix Operating System


Shell Kernel

Hardware

In UNIX, the operating system is broken into three pieces: the kernel, the shell, and the built-in utilities. The kernel is responsible for low level hardware communication, the shell provides human users with a user-friendly interface, and the built-in utilities provide basic tools for doing work.

Kernel
Heart of The Unix OS. Collection of C programs directly communicating with hardware Part of Unix system loaded into memory when Unix is booted

Manages:1. System resources 2. Allocates time between user and processes 3. Decides process priorities
Exit

Shell
Human interface point for Unix

Program layer provides an environment for the user to enter commands to get desired results.

Korn Shell, Bourne Shell, C Shell are various shells used by Unix users
Exit

User login process


Unix booted.

Program Unix(kernel) is booted into main memory, and remains active till the computer is shut down

Program init runs as a background task and remains running till shutdown

User attempts to log in.

Kernel calls program init.

init scans file /etc/inittab , which lists the ports with terminals and their characteristics and returns an active open terminal to init.

init calls program getty, which issues a login prompt in the monitor

User enters login and password

getty calls program login which scans file /etc/passwd to match username and password

After validation, control passes to session startup program /bin/sh , session startup program

Program /bin/sh reads file /etc/profile and .profile and sets up system wide and user specific environment.

User gets a shell prompt


Exit

Understanding Commands

Controlling Environment

General Purpose Utilities

Process

Handling Files

System Administration

Filters

Communication

Understanding Unix Commands


By end of this section , you will be able to know

What a command is?

Which program related to a command

Man get online help of commands

Alias Call a Command by another name

History previously executed commands

To get work done by Unix, there is no way but to execute : commands. Commands can be typed in from keyboard or taken from a file Unix offers a variety of commands for each category of jobs. Commands execute a program in the background which performs the desired job

Independent Commands
Do not require any input for execution
$pwd /home/ems2000

Dependent Commands
Require input for execution
$type ls

ls is a tracked alias for /usr/bin/ls

Mixed Commands:- Can work independently/dependently


$ls input sentwebcatalogs queue web $ls queue queue

General syntax to use commands


<command> [ <option flag> ] [<arguments>] A statement asking for execution of a command usually consists of three distinct sections:<command> : Keyword for the command <option flag> : Starts with - sign. Decides the nature of output from the results of command execution <arguments> : May be a string or the name of file(s) on which the command will act upon for desired output. Example : $grep -l ems2000 *.sh The above command from the OS prompt searches for the string ems2000 in all the files with extension : .sh and shows the name of the files containing it.

More than one commands can be stated from OS prompt at a time


$pwd;tty /home/ems2000/queue /dev/pts/tb First, pwd command is executed which displays the output : /home/ems2000/queue Then , tty command shows the terminal no: /dev/pts/tb Any number of commands can be specified at the OS prompt for execution separated by a ;

A command can spawn more than one lines $ echo


Hello Hello

which
Shows the name of the program related to the command

Syntax : which <command> Example : $ which ls


/usr/bin/ls

Other similar commands are : type <command>


whence <command>

man
Displays the online manual page for a given command, file, subroutine etc.

Use man <command> man f <command> man k <keyword>

Significance Shows the manual help page for the command Provides one line details about the command Provides a list of summary info about the manual sections in the keyword database for specified keyword <keyword>

Examples
$ man which

alias
Short hand notation of one/more than one commands

Syntax : alias [<alias_name>=<value>] Example:$alias sdf=ls ;ls|wc l $ $sdf


input logfiles 9 queue sap sentwebcatalogs web webcatalogs

pricelist_feed scripts

To unalias : unalias <alias_name>

history

Korn shell command. Shows an automatic recording of the commands entered, kept as a numbered list in a special disk file in the home directory and preserve it from login session to session. Commands from the history list can be re-executed using the tracking number.

Can be used for:Viewing the previously executed commands Re-submit a previously executed command from the list using r command Modify a previous command in the list and execute it

Environment variables:HISTFILE : HISTSIZE:


File name of Korn shell history file

Integer number containing the maximum number of commands to be retained in history list. Default is 16 for Korn shell

Use history history 5 history 5 history ls history 5 rn r -n r vi r prog1.sh=prog2.sh vi

Significance Shows last 16 lines of history in Korn shell Shows last 5 commands from history list Shows from command number 5 onwards in history list Shows recent ls command and from that line onwards in history list Shows from command number 5 onwards in history list Re-execute the command in line number n in history list Executes the nth last command executed Re-execute the last vi command If the earlier vi command was : vi prog1.sh , then the command to be executed is vi prog2.sh

General Purpose Utilities


By end of this section , you will be able know
banner set up poster cal get Calendar date get current date calendar get schedule who User information uptime System info login server login telnet server login exit- exit current shell lock lock a terminal expr Calculations factor factor : number primes-Prime number units-Unit conversion tput-Control display time-Time taken by a command

tty terminal information script get the whole job uname- machine info bc Unix Calculator

banner
Creates posters by blowing up its arguments on the screen. Each line can display a maximum of ten characters

Syntax : banner <text>


Use Banner Hello Jim Banner Hello Jim Significance
In a single line, Hello Jim will be shown as a poster The word Hello will be shown in one line and Jim in the next line as poster

Example

cal
Print the calendar for a year / for a particular month of a year

Syntax : cal [ <mon>] [ <4 digit year>]


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

date
Shows the current date in different formats/sets the current date

Syntax : date [ +%<flag for formatting options>]


Use date Significance
Shows the current date as a combination of :Three letter word for day of the week/ Short abbreviation for the current month/Day of the month Time in hh:mm:ss/Time zone/4 digit year $date Wed Sep 10 02:06:33 MDT 2003 Sets the Unix system clock to the date and time specified Abbreviated weekday name ( eg., Wed)

date [mmddhhmm[yy]] date +%a

Use date +%A date +%b date +%c date +%C date +%d date +%e date +%H date +%I date +%m date +%M date +%n date +%p

Significance Full weekday name(eg., Wednesday) Full month name(Jan) Current date & time representation Century(year/100, truncated to nearest number) Current Day of the month Current Day of the month Current hour of time(00-23) Current hour of time(12 hour clock) Current Month as decimal two digit number Current minute of time(00-59) Newline character AM or PM

Use date +%R date +%S date +%t date +%u date +%w date +%V date +%x date +%X date +%y date +%Y date +%Z

Significance Current time as %H:%M Current second of time(00-59) Tab character Weekday as 1 digit number[1-7 for Monday-Sunday] Weekday as 1 digit number [ 0-6 for SundaySaturday] Week number of the year Current date as dd/mm/yy Current time in hh:mm:ss Two digit year 4 digit year Time zone name

Examples of Date

calendar

Provides a useful reminder mechanism for the user. It is a sort of engagement diary. It searches for the file calendar in the current directory for lines containing any date equal to current date or next working date. Identifies numeric dates as : mm/dd/yy. Other date formats identified are:- <Mon> <dt> <Mon> <dt> , <year>

$ cat calendar I have a meeting on 07/10/2003 with Subhomoy I have a meeting on 08/10/2003 with Biswajit I have a meeting on 06/10/2003 with Sugata I have a meeting on 09/10/2003 with my delivery manager I plan for a dinner on Sep 10, 2003 with my team I have a meeting on 11/10/2003 I have a meeting on 12/10/2003 I will not come to office on 09/11/2003 $ calendar I have a meeting on 09/10/2003 with my delivery manager I plan for a dinner on Sep 10, 2003 with my team I will not come to office on 09/11/2003

who
Show the other persons logged into the system. Use who Significance
Produces a three columnar output on the users logged in:1st column shows the userid , 2nd column shows their terminal no. and third one shows the date and time of logging in Produces five column output with column headers at the top. The fourth column shos how many minutes the user is idle, fifth column shows the PID of the parent shell for the user. Produces single line of output pertaining to the user who invoked the command

who Hu

who am I

$who sqian ems2000 $who -Hu NAME sqian ems2000 $who am i ems2000 pts/te Sep 10 01:41 LINE pts/td pts/te TIME IDLE PID COMMENTS pts/td pts/te Sep 9 18:27 Sep 10 01:41

Sep 9 18:27 7:32 28935 135.148.207.175 Sep 10 01:41 0:02 29362 129.42.68.104

tty
Shows the terminal being used by the user Example $ tty /dev/pts/te

uname
Know the name of the machine/Version no of the Operating System Use uname n uname r Significance Shows the name of the Unix server being used in the network Show the version no of the Operating system

Similar command : hostname

uptime
Syntax : uptime [-hlsuw] [user]
Displays the current time, the length of time the system has been up, the number of users logged on to the system, and the average number of jobs in the run queue over the last 1, 5, and 15 minutes for the active processors.

Flag -u -h

Significance Print only the first line describing the overall state of the system. This is the default for the uptime command. Suppress the first line and the heading line. This option should not be used with the -u option. This option assumes the use of the -w option to uptime. Use long output. This option assumes the use of the w option to uptime.

-l

$uptime -u 2:17am up 122 days, 11:03, 2 users, load average: 0.41, 0.41, 0.41 $uptime -h sqian pts/td 6:27pm 7:39 1:41am rlogin htstbw00 -ksh

ems2000 pts/te $uptime -l

2:18am up 122 days, 11:04, 2 users, load average: 0.40, 0.41, 0.41

login
Syntax : login <server name| IP address>
Starts terminal session. Used at the beginning of each terminal session to properly identify a prospective user Can be invoked as an user command also. When used as a command, the previous terminal session is replaced. Prompt for userid and password appears.

telnet
Syntax : telnet <server name | IP address|alias>
Helps to connect to another Unix server connected to the same network, without being logged out of the current environment. If one does not specify a server, a prompt telnet> appears. One can use a number of commands from that prompt. Commands open <hostname|IP address> exit quit Significance Opens a connection to the user Returns to telnet> prompt after closing connection to the host Exits telnet

exit
Allows to exit the current shell

Syntax : exit

lock
Syntax : lock [-<minutes to lock after logout>]
Helps to lock an open terminal to prevent other people gaining access When invoked as a command, password has to be entered and re-entered to confirm it To unlock the terminal to use it, the user has to use this password A locked terminal remains locked for default time of 30 minutes after which it logs out. To change the default time:$lock n (Terminal remains locked for n minutes, after which it logs out)

Example

script
Syntax : script [ -a] [<newfile>]
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

Uses
script a script newfile

Significance
Append activities to existing file : typescript To log activities to a new file : newfile

Examples

bc
Binary Calculator for Unix.

$bc 12 + 5 17 ^d $
Addition

$bc 12 - 5 7 ^d $
Subtraction

$bc 12*5 60 ^d $
Multiplication

$bc 20/5 4 ^d $
Division

$bc 2^3 8 ^d $
Exponential

Scale:By default, bc performs truncated division.One have to set scale to the number of digits of precision before performing any division. $bc scale=2 10/4 2.50 ^d $ If answer to division is greater than the value as dictated by the scale variable, then the value dictated by the scale is ignored and the real value is shown.

ibase and obase:By default, the input and output are interpreted as decimal values.But, if the demand required input and/or output in different number system(binary, hexadecimal), variables ibase and obase are set. To convert binary input to decimal output:ibase=2 To convert decimal input(default) into binary output:obase=2 For hexadecimal systems, value 16 is used in ibase/obase variable.

Handling Variables:Variables can be used in bc mode and values can be assigned to them.
$bc X=12 ; y=19 Z=x+y Z 31

Conditional logics(if) ,loops(for,while), arrays, functions are supported by bc

Square Root of a Number


Syntax : sqrt ( x ) Example : $bc Sqrt ( 4 ) 2

Length of a Number
Syntax : length ( x ) Example : $bc length ( 1234.5678 ) 8

Trigonometric Functions
To use trigonometric functions of bc, one have to include math library. For that , issue the following command from Os prompt:$ bc l Various trigonometric functions available with bc are:s(x) c(x) e(x) l(x) sine cosine exponential log

expr
Allows to perform calculations and handle strings

Calculations
$expr 100 + 50 150 $expr 100 50 50 $expr 100 \ * 2 200 $expr 12 / 3 4 $expr 12 % 5 2 $z=`expr 100 50` 50

String handling To find length of a string:expr <string> : .* Example : $ expr Unix : .* 4

To extract a substring from a string:$ expr Subhendu : (\..\) he # Shows 4 th to 5th character

To locate first position of a character in a string:$expr Subhendu : [^d]*h 4 # Shows h is at 4th position

factor
Finds out factor of the integer provided

Syntax : factor <number>

$factor 15 15 3 5 $

$factor 18 18 2 3 3 $

primes
Shows all prime numbers between integers <lower value> and <upper value>. If upper value is not provided, it is considered to be 2,147,483,647.

Syntax : primes <lower value> <upper value>

$primes 0 10 2 3 5 7 $

units
Converts quantities expressed in various standard scales to equivalents in other scales. Acts interactively as follows:System Prompt You have: You want: User Response inch cm their

The system responds with two factors; one used if multiplying (preceded by *), the other if dividing (preceded by /): * 2.540000e+00 / 3.937008e-01 For a complete list of units, examine the file: /usr/share/lib/unittab

tput
Controls screen display Options
tput clear tput cup <r> <c> tput bold tput blink tput rev tput cols tput bel tput lines tput smso tput rmso

Significance
Clears the screen Moves cursor to row <r> and column <c> Bold display Blink display Reverse display Shows number of columns in the screen Echo bell character Shows number of lines in the screen Starts reverse display Ends reverse display

Examples

time
Syntax : time <command>

Times a command. Command <command> is executed and time prints:Elapsed time during the command Time spent in the system Time spent executing the command

Example:$ time grep -i "Subhendu" *


ABCDEF:subhendu bounce_off.ksh:# Developed by drawbox.ksh:# Developed by header:# heading:# Developer Developer : SUBHENDU MAJUMDAR : SUBHENDU MAJUMDAR : SUBHENDU MAJUMDAR : SUBHENDU MAJUMDAR

real user sys

6.6 0.8 0.7

Handling Files
This section will introduce you with
Architecture Types of Files Inode File System pwd-Current Directory cd Change Directory ls List contents of a dir Cat Create,View, append Files VI Visual Editor more display files pg View files cp,mv Copy and rename files rm Remove Files wc Count word,line and characters file know file type chmod Change permissions chown Change owner chgrp Change group touch Change time stamp of a file ln Link a file to other

Handling Filescontd
dircmp Compare Directories lp Print a file in a printer lpstat View printer status cancel Cancel print jobs pr Format file contents Compressing and Uncompressing Files

mkdir Create Directory rmdir Remove Directory cmp compare two files comm Compare two files sdiff find differences between two files

File maintenance architecture


Formatted Disk

Partition

Partition File system

Partition

Directory

Directory

Directory

Directory

Directory

Directory

File

File

File

Types of Files
Normal files
Can be text/binary files. Can be a text file, compiled source code , executables

Directory files
Contains no external data, but details of files and sub-directories it contains.

Device files
Printers, tapes, floppy drives, CD ROMs, hard disks, terminals all are considered as files

Inode

Inode is a fixed format structure containing the attributes of the files stored in the file system. Every file has one inode, and a list of such inodes is kept in a disk area not directly accessible by user. Each inode is accessed by inode number, specifying the position of the inode in the list.

Some of the important information that inode contains are:Information Mode Link count User id Group Id Size Access time Mod time Inode time Significance Permission mask and type of file No. of links associated with the file ID of the owner ID of the group No. of bytes in the file Time of last access of the file Time of last modification of the file Time at which the inode structure was last modified

Typical File System


/ (root)

Unix

bin

dev

usr

tmp

etc

usr1 /Unix folder contains the kernel

usr2

usr3

bin

/tmp contains temporary files /usr/bin contains additional binary unix commands /etc contains binary executable files for system administration

/bin contains binary executable files /dev contains device related files /usr is the home directory for all users /usr/usr1 is the home dir. for user : usr1

pwd
Shows the directory where the user is currently in

Syntax : pwd Example


$pwd /home/ems2000

cd
Navigates from the current directory to another directory

Syntax : cd <new directory specification>


Commands
cd scripts cd ./scripts cd ../program

Significance
Moves to directory : scripts under current working directory Change to directory program residing in the current directory's parent directory Change to the directory whose absolute pathname is : /usr/fin/subhendu/ manfiles Move to home directory of the user

cd /usr/fin/subhendu/ manfiles

cd cd ~

Commands
cd .. cd ~/subhendu

Significance
Move to the parent directory of the current working directory Move to folder : subhendu under the home directory for the user

cdpath
This is an environmental variable which specifies the list of directories to be searched when an user issues a cd command.

$CDPATH=.:..:$HOME
This means, when a cd command is issued, search for the new directory first in current working directory(.) If not found, move to the parent directory of the current directory and search there. If, still, not found, search for the directory under users home directory

ls
Shows the contents of a directory/existence of specific files with their attributes

Syntax : ls [ flag] [string for filename]


Commands
ls ls a ls x ls R ls l ls F

Significance
Shows the name of all the files and directories under the current directory, excluding those starting with . Lists all files including those starting with . Multi columnar output Shows all files and recursive listing of all files in subdirectories Long listing showing seven attributes of a file Marks executables with * and directories with /

Commands
ls t ls ut ls r ls ltr

Significance
Sorts files by modification time the file modified most recently comes at the top Sorts files by access time Sorts file in reverse order Shows long listing of files with their attributes, sorted in reverse order by access time(most recently edited file comes last in the list) Shows inode number of a file Shows the name of all files with .ksh at the end of their name Shows the files with name starting with vowels Lists all files starting with d and ending with .sh in their name Lists all files with first letter as d and third letter as l Shows the files with names not starting with vowels

ls i ls *.ksh ls [aeiou]* ls d*.sh ls d?l* ls [!aeiou]*

cat
Creates , shows, concatenates,copies files Commands
cat >file1 cat >>file1 cat file1 cat file1>file2 cat file1 file2 > file3 cat file1 >>file2

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

Commands
cat n file1 cat b file1 cat e file1 cat r file1 cat t file1 cat s file1 file2 >file3

Significance
Displays the contents of file : file1 with line number Displays the contents of file : file1 with line number for all lines excepting the blank lines Prints $ at the end of each line Replaces multiple consecutive empty lines with one empty line Prints tab character as ^I and form feed character as ^L Suppresses error and does the job. If file file2 does not exist, the command will copy the contents of file1 into file3

Examples

Examplescontinued

Examplescontinued

VI
First Unix Full screen Editor

First full screen editor, developed by William(Bill) Joy, a graduate student from University of California, Berkley. Divides Unix users into two camps: Those who hate vi Those who love vi

Haters say that it is the worst thing ever happened in the Unix world. Lovers are totally biased towards it and would go any length supporting its cause.

Modes of Operation

Ex mode

First session with vi

Environmental variables

Append mode

.exrc & EXINIT variable

Command mode

view

Mode of Operation
Command mode : Default mode when a file is opened using
vi. All the keys pressed by the user are interpreted as user commands

Append Mode : Permits insertion of new text, editing


existing texts.

Ex mode : Permits commands at the command line(last line of


the screen)

Command Mode
R,R,I,I,c,C,o, O,s,S,a,A

Esc

Enter

Append Mode

Ex Mode

First Session with vi

Step 1 : Create a new file by typing the following command from the OS Prompt : vi newfile

vi clears the screen and display a window. The _ on the top line indicates that the cursor is waiting for commands Every other line starts with ~, symbol for empty line.

Step 2 : Press i to enter into Append mode. Add text to the file

Step 3 : Press <Esc> key to return to command mode

Step 3 : Press :. The cursor takes to the ex mode at the command line. Enter wq and press enter.

Append Mode
Inserting Texts Cmd
i I a A o O

Significance
Appends text from the left of the current cursor position Appends text at the start of the current line. Appends text from the right of the current cursor position Appends text at the end of the current line. Opens a line immediately below the current line in input mode Opens a line immediately before the current line in input mode

Changing Texts Cmd


<n>r R <n>s <n>S c0 c$ C

Significance
Replaces <n> characters from current cursor posn. with inserted text Replaces text from cursor to right Replaces <n> characters from cursor with entered text Replaces <n> lines from the current cursor line with entered text Changes from cursor to beginning of line with the text entered Changes from cursor to end of line with the text entered Change from current cursor posn. to end of line with the text entered

<n>cw Changes <n> words from the current cursor position with text entered <n>cc cG Replaces <n> lines from the current cursor line with entered text Changes from current cursor position to end of the file with entered text.

Command Mode
Saving work in a file and quit

Cmd
ZZ

Significance
Saves the work done in the file and quits editing by vi editor

Deleting texts/lines

Cmd
<n>x <n>dd or <n>D d$ dG d<n>G df<char> d/<pattern> d?<pattern>

Significance
Deletes <n> characters from current cursor position Deletes <n> lines counting from current cursor line to below Deletes from current cursor position to end of line Deletes from current cursot position to end of file Deletes from current line to line no <n> Deletes from current cursor position to first occurrence of character <char> Deletes from cursor upto the first occurrence of string <pattern> in forward direction Deletes from cursor upto the first occurrence of string <pattern> in backward direction

Moving/Copying Texts Cmd


<n>yy or <n>Y <n>yw y$ yG a<n>yy p P ap bd

Significance
Yank <n> lines starting from current line onwards into undo buffer Yank <n> words starting from current cursor position onwards into undo buffer Yank from current cursor position to end of the line in undo buffer Yank from current cursor position to end of the file in undo buffer Yank <n> lines starting from current line onwards into buffer named a Paste the contents of undo buffer( as a result of deleting or yanking) after the cursor position Paste the contents of undo buffer( as a result of deleting or yanking) before the cursor position Paste the contents of buffer a after the cursor position Delete text into named buffer b

Navigation in same line

Cmd
<n>h <n>l <n>b <n>w <n>e f<ch> F<ch> t<ch> T<ch> ;

Significance
Moves cursor left to nth previous character w.r.t. the current cursor position Moves cursor right to nth next character w.r.t. the current cursor position Moves cursor left to start of nth previous word w.r.t the current cursor position. Punctuation marks are taken into account. Moves cursor right to start of nth next word w.r.t the current cursor position. Punctuation marks are taken into account. Moves cursor right to end of nth next word w.r.t the current cursor position. Punctuation marks are taken into account. Move the character to the next character <ch> on same line Move the character to the prv. character <ch> on same line Move the character to one column before the next character <ch> on same line Move the character to one column after the next character <ch> on same line Repeats search in the same direction along which the prv. Search was made using f/t

Cmd
,

Significance
Repeats search in the opposite direction along which the prv. Search was made using f/t Moves the cursor to specified column <n> Moves to 1st character of the current line Moves to last character of current line Moves to 1st non-space character of the line Moves cursor left to start of nth previous word w.r.t the current cursor position.Punctuation marks are ignored Moves cursor right to start of nth next word w.r.t the current cursor position. Punctuation marks are ignored Moves cursor right to end of nth next word w.r.t the current cursor position. Punctuation marks are ignored

<n>| 0 or ( $ or ) ^ <n>B <n>W <n>E

Navigation across lines Cmd


<n>j or <n>^n <n>k or <n>^p H L M <n>G + -

Significance
Move the cursor down to the <n>th next line in the same column Move the cursor up to the <n>th prv line in the same column Moves the cursor to the top line of the screen Moves the cursor to the last line of the screen Moves the cursor to the middle line of the screen Moves to line number <n> Moves the cursor to next lines first non-blank character Moves the cursor to previous lines first non-blank character

Redraw screen Cmd


zz+ z. Ctrl-l /pattern/z-

Significance
Makes the current line the last line of the screenand redraws the screen Makes the current line the first line of the screenand redraws the screen Makes the current line the middle line of the screenand redraws the screen Redraws the screen Find the next occurrence of <pattern> and make that last line of the screen

Scrolling across pages

Cmd
<n>^f <n>^b <n>^d <n>^u <n>^e <n>^y

Significance
Move forward by <n> screens Move backward by <n> screens Move forward by <n> number of half screens Move backward by <n> number of half screens Scroll window down by <n> lines Scroll window up by <n> lines

Pattern searching Cmd


/pattern ?pattern n N /pattern/+<n> /pattern/-<n> %

Significance
Searches for specified <pattern> forward. IF end of file is reached, search wraps around. Searches for specified <pattern> backward. Repeat the last search in the same direction as was specified in the last search Repeat the last search in the opposite direction as was specified in the last search Positions the cursor <n> number of lines after the line where the specified <pattern> is found Positions the cursor <n> number of lines before the line where the specified <pattern> is found Find the matching braces or parenthesis

Joining lines Cmd


<n>J

Significance
Joins current line and <n> lines below it together to form a single line

Undo changes Cmd


u U

Significance
Undo last change Undo all the changes in the current line

Marking text Cmd


m<char> <char>

Significance
Marks position of the file with mark <char> Moves to portion of the file marked with <char> Toggle to most recently marked location

Restoring previously deleted line Cmd


<n>p 1pu.u.u

Significance
Paste the content of <n>th last delete ( n<=9) Till the last change is found

Filtering texts Cmd


!<n>G sort !<n>G tr [a-z] [A-Z] !! tr [a-z] [A-Z]

Significance
Sort from current line to line no. <n> Translates all the characters from current line to line <n> to uppercase Translates all the characters of current line to uppercase

Repeat factor Cmd


<n>i<ch>

Significance
Inserts <ch> character <n> number of times in input mode at a stretch

Miscellaneous in command mode Cmd ~ . <n>. << >> Significance Change the character under cursor from lowercase to uppercase and vice versa Repeat the last change Repeat the last action n times Shift current line to shift width character left Shift current line to shift width character right

Options available with vi command Cmd


vi r <filename> vi R <filename> vi +<n> <filename> vi + <filename> vi w<n> <filename> vi +/<pattern> <filename> vi x <filename>

Significance
Recover the file <filename> as much as possible after system crash and open it Open the file <filename> in read-only mode Opens the file <filename> with cursor positioned in line number <n> Opens the file <filename> with cursor at the last line Opens file <filename> in vi mode with window size of <n> number of lines Opens file <filename> in vi editor and places the cursor at first occurrence of pattern <pattern> Opens encrypted file <filename> in vi mode and asks for the password before opening that

Ex Mode
Saving work in a file Cmd :w :w <filename> Significance Save the changes made to the file 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 Save the changes to file <filename>, if the file already exists Append the contents of the opened file after the last line of the file <file1>. File <file1> should exist previously Copies the contents of lines <n1> to <n2> into a new file <newfile> Moves the contents of lines <n1> to <n2> into an existing file <newfile> , overwriting its previous contents

:w! <filename> :w >> <file1>

:<n1>,<n2>w <newfile> :<n1>,<n2>w! <newfile>

Cmd
:.,.+<n>w <newfile> :.,.+<n>w >> <nextfile> :q :q! :wq or :x

Significance Appends from current line to <n> number of lines below it into file <newfile>
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 Save the unsaved changes in the opened file and quit vi editor

Temporary exit to shell Cmd


:sh

Significance
Temporarily allows the user to come out of the vi file and use the shell. After the job of the user is done and command : exit is triggered from OS prompt, control returns to vi editor again

Navigating to desired line Cmd


:<n>

Significance
Custor moves to line number <n>

Search and replace texts in ex mode

Syntax :- :<line address>s/<old pattern>/<new pattern>/g


Line address
% . <n1>,<n2> $ 1,$ .,.+<n>

Significance
All lines where matching pattern is found Current line Refers from line <n1> to <n2> Last line First to last line From current cursor line to <n> number of lines downwards

Examples of Search and Replace Example


:%s/ex/vi/c

Significance
Substitutes 1st occurrence of string ex with vi by showing them and asking for confirmation. When each string will be shown with pause in cursor, press y for substitution Replaces amaze , where available as a full word, with delight. Note, any word like amazed will not be replaced Replaces every occurrence of string majumdar with mazumder on all lines containing the pattern subhendu Replaces 0 with * at all lines having 9 after 9th position Delete all blank lines Append the string To be done at the end of all lines not containing the string complete

:%s /<amaze \ >\/delight/g

:g/subhendu/s/majumdar/ mazumder/g :g/.\ {9\ }9/s/0/*/g :g/^$/d :g!/complete/s/$/To be done/

Example
:g/vi/s/^/editor/ :%s/$/ : see my note/g :g/^.$/d :g/^..o/d :%s/$//g

Significance Append the string editor at the first of all lines containing the string vi Appends the string : see my note at the end of all lines Deletes all lines containing 4 letters Deletes all lines with o as 3rd character Delete the last three character of every line

Reading below the current line in the vi editor Command :r <nextfile> :r! <command> Significance Reads the contents of the file <nextfile> below current line Places the output of the command <command> below the current line

Editing another file Command


:e <nextfile>

Significance
Stops editing the current file; leaves the current file and starts editing file <nextfile>; provided there are no unsaved changes in the current file Edits file <nextfile> abandoning all the changes done to the current file Loads last saved version of current file Edits next file mentioned in the vi queue Edits first file in the command line Edit starts at line <n> of file <nextfile>

:e! <nextfile> :e! :n :rew :e +<n> <nextfile>

Abbreviating texts Command


:ab <short_string> <long_string>

Significance
When the user writes the string <short_string> in input mode, the <long_string> is written

Mapping Command
:map g :w^M

Significance
Pressing g, one wants to save the file(:w is for saving, and ^M is for pressing Enter key. While writing it in the command line, write ^V^M) When you position your cursor to any character in a line and press z , the line will be broken from that point and two lines will be formed. The control will remain in command mode (^[ represents <Escape> key) Pressing z in command mode saves the file and executes it in one shot

:map z i^M^[

:map z :w^M:!%^M

To unmap a key, write at the command line :unmap <key>

Miscellaneous Command :! <command> :f ^g Significance Executes the command <command> remaining in vi editor Shows the name of the current file and line Same as :f

Setting environmental variables for vi


Works in ex mode. To set an environment variable to customize vi, the following syntax needs to be followed::set <env.variable> [= <value>]

Environment variables
autoindent(ai)

Significance
Newly inserted lines of text are indented to the same distance from left margin as the preceding line. Opposite of this option is noautoindent(noai) Automatically saves the unsaved changes in a file before opening the next file with :n or using a shell command with :! <command>. The opposite to this option is noautowrite(noaw) Sounds the bell when error occurs. Opposite is noeb Allows an .exrc file in the current directory to override the .exrc file in users home directory. Opposite is noex Displays special characters in the screen: tabs are shown as ^I, end of line are marked with $. Opposite is nolist System messages allowed when vi is running. Opposite is nomesg Displays line numbers. Opposite is nonu

autowrite(aw)

errorbells(eb) exrc(ex) list mesg number( nu )

Environment variables
report=<val> scroll=<val> shiftwidth(sw)=<val> showmatch(sm) showmode tabstop=<val> ignorecase(ic) wrapmargin(wrm)=<val>

Significance
When any operation affects more lines than this settings, message is displayed Number of screen lines to scroll Number of spaces to be used for backtabs/<</>> Shows match for ) or } . Opposite is nosm Indicates type of mode No. of spaces the tab character moves over Ignores case when searches patterns. Opposite is noic When set to a value >0 , carriage returns are inserted automatically when the cursor gets to within that number of spaces from the right edge of the screen

.exrc file and EXINIT system variable


One can store all the values for environment variables, all the key mappings and all the abbreviations in a file .exrc under the home directory for the user.VI looks for this file on startup and executes the instructions as ex mode commands.

Besides, there is also a system variable , EXINIT which can also be used to save the settings. EXINIT=set report=5 ignorecase ai; export EXINIT

view
Syntax : view <filename> Description :Shows the file <filename> in vi mode. File remains
read-only. No changes done to the file cannot be saved.

more
Syntax : more [ -n <number> ] [ -<number>] [ -d] [-i] [-e] [ -c] [-f] [-s] [ +/pattern] [filename]

Filter for examining continuous text, one screenful at a time. It pauses after each screenful,printing the filename at the bottom of the screen. To display one or more next lines, press <Return> To display another screenful, press <Space>

Command line option


-n <number> -<n> -d -i -c -s +/pattern

Significance
Sets the no of lines in the display window to that <number>. Same as above Prompts user with message :- Press space to continue, q to quit, h for help at the end of each screenful of display Performs case independent pattern matching Draw each page by starting at the top of screen, and erase each line before drawing on it Squeeze multiple blank lines from the file , showing only one blank line

Start listing such that the current position isset to two lines above the line matching the regular expression pattern.

Internal commands in more mode

Options
f b q /pattern ?pattern v n or N <n>j <n>G G . :n

Significance
Scrolls forward one screen Scrolls backward one screen Quit Searches pattern <pattern> forward Searches pattern <pattern> backward Opens vi mode for the file viewed Repeats last search Scrolls forward by <n> number of lines

Moves to line number <n> Goes to last line of the file Repeats prv. command Moves to next file specified in the command line

Options
:p i<space> i<return> <i>^d/ <I> <i>^u /<i>u <n>k/ <n>^y <n>z <n>f / <n>^f <n>b / <n>^b ^g

Significance
Moves to previous file specified in the command line Scrolls forward by i number of screens Scrolls forward by i number of lines Scrolls forward i lines, with a default of of the screen size Scrolls backward i lines, with a default of of the screen size Scrolls backward by <n> number of lines Displays <n> more lines and sets the window size to <n> Moves forward by <n> lines Moves backward by <n> lines Writes the name of the file currently being examined, the no. relative to the total number of files to be viewed, the current line no,current byte no, total no of bytes to write and what % of the file precedes the current position

Options
h !<command> :e <newfile> <I>:n r / ^l . m<char> <char>

Significance
Display a descriptive list of all the more commands Invoke a shell with a command <command> Leaves the current file and starts viewing a new file <newfile> Examines the <I>th next file specified in the command line Refreshes the screen Repeats the prv command Marks the current position with letter <char> Returns to the position previously marked by the specified letter <ch>

pg
Displays the content of a file one page at a time

Syntax :pg < flag and string> <filename(s)


Flag
-c -e -f -p <string> -s +<n>

Significance
Clear the screen at the end of each page of display and start the display at the top of the screen Continues to the next file after the end of one file, if more than one files are specified at the command line Truncate lines longer than the width of the screen display Display the <string> at the pg command prompt.Default is : . If string is %d the pageno is displayed atr the prompt Highlights all messages and prompts issued by the pg command Start the display from line no <n> of the file specified

Flag
-<n> +/pattern/

Significance
Sets the size of the display screen to <n> number of lines Search for pattern <pattern> in the file and start the display at that line

Keystrokes in pg mode
Option
-<n> +<n> l <n>l +<n>l -<n>l d -d ^l

Significance
Go backward by <n> number of pages Go forward by <n> number of pages Go forward by one line Start the display in the file at line specified by <n> Go forward by <n> lines Go backward by <n> lines Go forward by screen Go backward by screen Redraws the screen

Option
$ <n>/<pattern>

Significance
Move to last page of the file Searches forward for the pattern <pattern> in the file from beginning of the next page. If a number <n> is specified, pg searches for the specified occurrence number <n> of the <pattern> Starts showing the <n>th previous file. Start showing the next file Quits the pg command

<n>p n q

cp
Copies one file to another file/one directory to another directory/files into directories

Syntax :cp [-flag] [ old files/directories] [ new files/directories]


Flag -i -f -p -r Significance Interactive copying : prompt appears for user accent before copying Force copying Preserve permissions. Preserves modification time,access time,file mode, user id, user group etc Recursive copying

Examples
Example
cp file1 file2 cp file1 file2 file3 dir1 cp R dir1 dir2

Significance
Copies the contents of file1 into new or existing file file2 Copies files: file1, file2 and file3 into directory dir1 Copies directory dir1 into a new directory dir2(if dir2 does not exist before) / copies the directory dir1 as a sub-directory under dir2(if directory dir2 exists before) If dir3 exists, two sub-directories under it are created : dir1 and dir2. IF dir2 directory does not exist, then a new directory dir2 is created with the contents same as dir1 and one additional sub-directory dir2

cp R dir1 dir2 dir3

mv
Syntax :mv [-flag] [ old files/directories] [ new files/directories]
Moves/renames:A file to new or existing file One/more files to existing directory One or more directories to a new or existing directory. If the access permission of the destination directory or existing destination file forbids writing, mv command asks for overwriting the file Flag
-f -i

Significance
Performs move operation without prompting for permission Interactive moving

rm
Syntax :rm [-f|-i] [ -r|-R] [files|directories]
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 Flag
-f -i -r or R

Significance
Forceful deletion Interactive deletion Recursively delete the entire contents of the cirectory before removing the directory itself

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

wc
Syntax :wc [-c|-l|-w] <filename(s)>
Counts the number of words/bytes/characters/lines in a file Flag
-w -l -c

Significance
Counts the total no. of words Counts the total no. of lines Counts the total no. of characters

Examples
$ wc c file1 32 file1 $ wc l file1 2 file1 $ wc w file1 8 file1

file
Syntax : file [-f ffile] [-h] file ...
Determines the file type of a file or list of files. Performs a series of tests on each file in an attempt to classify it. If file appears to be an ASCII file, file examines the first 512bytes and tries to guess its language. File /etc/magic is ued to identify files that have some sort of magic number, that is, any file containing a numeric or string constant that indicates its type. Commentary at the beginning of /etc/magic explains the format.

chmod
Syntax : chmod [file permission] <files|directories>
A file can have three type of permissions:Read : Authorized user can read the contents of the file. Write : Along with read permission, it allows the allowed user to modify its contents. Execute : If the file is an executable, any allowed user can execute it

A file can be accessed by:User : Person creating the file. He grants all the authorizations to the file Group : Group user for the file Others: All other users not belonging to the group or are not the creator.

Numeric representation of permissions


Value
4 2 1 6(=4+2) 7(=4+2+1) 5(=4+1)

Significance
Read permission Write permission Execute permission Read and write permission Read,write and execute permission Read and execute permission

Example of granting numeric authorization Syntax: chmod <val1><val2><val3> <filename(s)|directory name(s)> val1 is for users val2 is for group val3 is for others Any permission on a directory percolate down to the files and subdirectories under it. Example
chmod 744 file1 chmod 776 chmod 777 file1

Significance
Grant all permissions to : User, and read permission to group and others Grant all permission to user and group, read and write permission to others Grant all permission to all

Alphabetic representation of permissions


Value
r w x

Significance
Read permission Write permission Execute permission

Value
u g o a

Significance
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 granting alphabetic authorization Example


chmod u=rwx,go=r file1

Significance
Assign read,write and execute permission to user, but only read permission to group and others Revoke execute permission from others Assign execute permission to everybody 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

chmod o-x file1 chmod a+x file chmod R ug+r,o-r,a+x /home/ems2000

Chown chgrp
and

Syntax : chown [-h] [-R] [owner] [file]


chgrp [-h] [-R] [group] [file]

chown changes owner of the file and chgrp changes group of each file specified. To change user or groyp, one must own the file and have the CHOWN privilege granted to him by the system administrator

Options
-h -R

Significance
Change owner/group of a symbolic link Recursively change the owner and group of all the files and sub-directories under the directory named

Example
chown ems2000 auto.profile chgrp ems2000 auto.login chown R ems2000: users shell_scripts

Significance
User ems2000 becomes the owner of the file auto.profile User ems2000 becomes the group user to access the file auto.login The command searches the directory : shell_scripts and changes each file in that directory to owner : ems2000 and group : users

touch
Updates access time/modification time of file(s)

Syntax : touch [-a|m|c] [ -r <ref-file> | -t <time> ] <filename(s)>

Flag
-a -m -r ref-file -t <time>

Significance
Change the access time of the file to the time specified/ if no time is specified, use the current time Change the modification time of the file to the time specified/ if no time is specified, use the current time. Use the corresponding time of file ref-file to change the modification/access time of the file Use the specified time <time> instead of current time. The time format is : <YYYY><MM><DD><hh><mm>.<ss>

Example
touch a wot.ksh

Significance
Changes the access time of wot.ksh with the current date and time (Changes can be perceived if the command : ls ut is
fired in the directory containing the file : the file wot.ksh will come at the top of the list)

touch m wot.ksh

Changes the modification time of wot.ksh with the current date and time (Changes can be perceived if the command : ls lt is
fired in the directory containing the file : the file wot.ksh will come at the top of the list)

touch -m -t 201012122300 new.del

Changes the modification time of file : new.del to Dec 12,2010 time : 23:00 Seeing the attributes of the file new.del will reveal the information:-rw-rw-r-- 1 ems2000 dba 10 Dec 12 2010 new.del

touch -m -r new.del new1 Change the modification time of file : new1 and make it same as the modification time of the file : new.del

ln
Description Link files and directories
Command
ln [-f|-i] file1 newfile ln [-f|-i] file1 file2 testdir ln [-f|-i] dir1 dir2 testdir

Significance
Links file : file1 to a new or existing file : file1 Creates link for files new1 and new2 under the directory : testdir with two new files/existing files new1 and new2 Creates link for directories dir1 and dir2 under the directory : testdir with two new /existing directories : dir1 and dir2

Flag Significance
-f -i Force esisting files or directories to be removed to allow the link Interactive linking

mkdir
Creates directories

Syntax : mkdir [-p] [-m <mode>] <directory name(s)>


Flag -p Significance Intermediate directories are created as necessary.Otherwise, the full path prefix of dirname must already exist. mkdir requires write permission in the parent directory. Mode of permission for the directory and all the files under it

-m <mode>

Example Mkdir m755 testdir mkdir p testdir/subhendu

Significance Directory : testdir is created with permission = 755 If testdir is not created, it is first created. Then, a directory : subhendu is created under the directory testdir

rmdir
Removes the directory entry for each empty directory referred

Syntax : rmdir [-f|-I|-p] <directory>


Flag
-f -i -p

Significance
Forcefully remove a directory, even though it is not empty Interactive removal of empty directory Path removal.If, after, removing a directory with more than one pathname component, the parent directory of that directory is empty, rmdir removes the parent directory also. This continues till rmdir encounters non-empty parent directory

cmp
Syntax : cmp [-l|-s] <file1> <file2> Description Compares two files
Flag
-l -s

Significance
Print the byte number(decimal) and differing byte(octal) for each difference Prints nothing; return exit codes only

Return codes
0 1 2

Significance
Files identical Files not identical Inaccessible/missing arguments

comm
Syntax : comm [-[123]] <file1> <file2> Description Select or reject lines common to both files.
Case
comm filenew fileold

Significance
Produces a three columnar output:1st column displays lines only in file: filenew 2nd column displays lines only in file:fileold 3rd column displays lines common to both files Suppress display of 1st column Suppress display of 2nd column Suppress display of 3rd column Shows only lines common to both files

comm 1 filenew fileold comm 2 filenew fileold comm 3 filenew fileold comm 12 filenew fileold

sdiff
Syntax : sdiff [-l|-s] <file1> <file2> Description Side by side file difference program.
$cat file1
Suvendu Subhasish Dibyendu Diptoman Akash

$cat file2
Subhendu Subhasish Arka Dibyendu diptoman

$sdiff file1 file2


Suvendu ! Subhendu Subhasish Subhasish > Arka Dibyendu Dibyendu

Diptoman Diptoman Akash <

Flag
-l -s

Significance
Only print on the left hand side where columns are identical Do not print identical lines

dircmp
Syntax : dircmp [ -d|-s|-wn] <directory1> <directory2> Description Examines two directories and generates various
tabulated info about the contents of the directories. Flag
-d

Significance
Also compares the contents of the files with same name in both directories and output a list telling what must be done to bring them into agreement Suppresses messages about identical files

-s

$ >ls -ltr dir1 dir2


dir1: total 10 -rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r-dir2: total 8 -rw-rw-r--rw-rw-r--rw-rw-r--rw-rw-r-$ dircmp ./subha ./subhendu 1 ems2000 1 ems2000 1 ems2000 1 ems2000 1 ems2000 1 ems2000 1 ems2000 1 ems2000 1 ems2000 dba dba dba dba dba dba dba dba dba 13 Jul 20 07:23 abc 26 Jul 20 07:23 def 15 Jul 20 07:30 subhendu 28 Aug 13 00:30 subha 5 Aug 13 00:39 samefile1 26 Jul 20 07:24 def 12 Aug 13 00:32 nsf 30 Aug 13 00:32 abc 5 Aug 13 00:39 samefile1

dir1 dir2
./nsf

Aug 13 00:42 2003 dir1 only and dir2 only Page 1

Aug 13 00:42 2003 Comparison of dir1 dir2 Page 1 directory different same same . ./abc ./def ./samefile1

lp
Syntax : lp -d<printer> [-m] [-n<copies>] [-t<title>] <file(s) Description Prints one/more files to a specified printer
Flag
-d<printer> -m -n<copies> -t<title>

Significance
Specify the printer <printer> where the print request is to be directed Notify the requesting user at successful completion of the print request by mail Specifies number of copies to print Print the specified title <title> on the banner page

lpstat
Syntax : lpstat [ -p<printer>] [-t] [-u<username>] [-v
<printername>]

Description Shows current status of the print request


Flag
-p<printer> -t -u<username> -v<printername>

Significance
Shows all printing jobs queued at printer <printer> Displays detailed status information about the print from all users Displays the status of print requests triggered by user <username> Displays a list for the specified printername

cancel
Description
Used to cancel a specific print request or cancel all queued requests to a specific printer queue. Any ordinary user can cancel only those jobs triggered by his own userid

Example:$ cancel 734 $ cancel lipi # cancels print job with id 734 # cancels all queued jobs in printer lipi

pr
Description Prepares a file for printing by adding suitable
headers, footers and formatted texts. Example
pr file1

Significance
Adds 5 lines of margin at the top and 5 lines at the bottom of file : file1. Header shows date and time of last modification of the file along with filename and pageno Changes default page size from 66 to 72 Show file file1 from 10th page Prints a file in three columns Suppresses page headers and footers for file : file1 Form feed is used for a new page instead of a sequence of line feed characters Set the width of each page to 80 instead of a default of 72

pr l 72 file1 pr +10 file pr 3 file1 pr t file1 pr f file1 pr w 80 file1

Example
pr P file1 pr o5 file1 pr d file1 pr h List of files file1

Significance
Pauses after each page is displayed in the terminal Indent each line by 5 columns Generate output with double specing Print List of files instead of the filename as header on each page.

Compressing files
Compress
Compress <filename(s)>
Reduces the size of the named files using adaptive Lempel-zev coding. If reduction is possible, each file is replaced by a new file with same name suffixed sith .Z. Original ownership, modes, access and modification times are preserved.To uncompress a file, issue the command : uncompress <filename>.Z . To view a compressed file, issue : zcat <filename>.Z

Gzip
gzip <filename(s)>
Reduces the size of the named files . If reduction is possible, each file is replaced by a new file with same name suffixed sith .gz. Original ownership, modes, access and modification times are preserved. To uncompress a file, issue the command : gunzip <filename>.gz . To view a compressed file, issue : gzcat <filename>.Z

Filters

Filters
By end of this section , you will be able know
head First few lines of a file tail Last few lines of a file cut View columns/fields paste join lines of files split Break long lines fmt Formats texts fold Folds long lines Sort Sorts file contents tr Translates file contents nl Shows line numbers Spell Catch spelling mistakes find Find files grep Search Pattern in a file Sed Display specific lines awk Reporting Tool

head
Gives first few lines of a file

Syntax :head [-<n>] [-c] [-l] [ -n <I>] [file(s)]


Example
head 5 newfile head n 5 newfile head -l -n 5 newfile head -c n 14 newfile

Significance
Shows first 5 lines of file : newfile Same as above Same as above Shows first 14 characters of newfile

tail
Gives last few lines/characters of a file

Syntax : tail [-f] [-c number] [file]


tail [-f] [-n number] [file]

Flag
-n <no>

Significance
Shows last <no> lines of the file if <no> is specified with no sign or ve sign. If specified with a + sign, it shows from <no>th line onwards till end of the file Shows last <no> bytes of the file if <no> is specified with no sign or ve sign. If specified with a + sign, it shows from <no>th byte onwards till end of the file Works in constant pilot mode . Used for variable sized files those are growing constantly.

-c <no>

-f

Example
tail 5 newfile tail n 5 newfile tail +5 newfile tail n +5 newfile tail -c 5 newfile tail c +5 newfile tail fn 3 newfile

Significance
Shows last 5 lines of newfile Same as above Shows from 5th line to end of the file Same as above Shows last 5 bytes of a file Shows from 5th byte till end of the file Constantly displays the last three lines of file : newfile and leave the tail in follow mode. The $ prompt does not return even after the work is over. One have to abort the process to exit to the shell.

cut
Syntax : cut [-c list] [file]
cut [-f list] [-d <char>] [file]
Extracts selected fields/characters on each line of a file

Example
cut c 6-22,24-32 file1 cut c 22 newfile cut c 6, 22,24- file1

Significance
Extracts texts from column 6 to 22 and 24 to 32 and displays in the terminal in two columns Extracts text from column 1 to 22 from the file : newfile and displays it in the terminal Extracts texts from column 1 to 6, column 22 and from column 24 to end of the line and display it in the terminal Extracts field contents of 2nd and 3rd fields and display in the terminal. Use | as a field separator while extracting fields Using | as field separator, it extracts field contents of 1st to 5th fields and display in the terminal.

cut d| f 2,3 newfile

cut d| f 1-5 newfile

paste
Syntax : paste file1 file2 ...
paste -d list file1 file2 ... paste -s [-d list] file1 file2 ... Merges same lines of several files or subsequent lines of same file

Example
paste file1 file2

Significance
Display in the screen the join of line 1 of file1 with line 1 of file 2 with a space between them, line 2 of file1 with line 2 of file2 with a space delimiter and so on. Same as above with | as delimiter between the fields of both files on each line. Joins all the lines of file1 into a single line and show it in the terminal

paste d| file1 file2 Paste s file1

Example paste -s file1 file2

Significance
Displays in the screen a single line which is :Join of all the lines in file1 + Join of all the lines in file2 4th into another line and so on.

paste s -d t\n\ file1 Combines 1st and 2nd lines into a single line, 3rd and

split
Syntax : split [-<no. of lines to be put in each file>] [<initials>]
Splits up a file into equiline smaller lines. Large files are sometime difficult to edit with an editor. The split command breaks up a larger file into several equi line smaller files, each containing a default of 100 lines. It creates a group of files xaa,xabtill xaz and then again from xba,xbb.. till xbz. Total 26x 26 = 676 files can be created in this way.

Example:
File: newfile consists of 100 lines. $ split 20 newfile 5 files nfa,nfb,nfc,nfd and nfe will be prepared each containing 20 lines from newfile

fmt
Syntax : fmt [-s] [-w <width>] [file]
Simple text formatter that fills and join lines /split lines to produce output lines upto the number of characters specified in the w option(default is 72). It also counts the spaces between words in a lineand also considers a space between joining of two lines. The s option split lines only. It does not join short lines to form longer ones.
$ >cat file1 Today, we have a meeting. It will start at 6 pm. Bye $ >fmt -w10 file1 Today, we have a meeting. It will start at 6 pm. Bye

fold
Syntax : fold [-b|-s] [-w <width>] <file.>
Folds long lines

Flag Significance
-b -s Width in bytes for counting Break the line on the last blank character found before the specified number of column position specified in the w<width> option(default : 80)

$ cat file1 Today, we have a meeting. It will start at 6 pm. Bye $ fmt -s -w15 file1 Today, we have a meeting. It will start at 6 pm. Bye

sort
Sorts the contents of a file. Starts with 1st character on each line and proceeds to the next character only when the prv. Character in the two lines are identical. Can also work on fields. Default field separator is the space.This can also be changed.

Example
sort file1 Sort t | +2 file1 Sort t | r +1 file1 Sort c file1 sort t | +2 c file1

Significance
Shows the sorted contents of file1 as per character sorting Sort the contents of file1 based on 3rd field Reverse sort on 2nd field Checks whether the file : file1 is already sorted or not. Value of system variable $? Is 0 if it is sorted Checks whether the file is already sorted on 3rd field or not

Example
Sort o sort.lst +3 file1 Sort t | +3 4 file1

Significance
Sort the contents of file : file1 based on 4th field and store the output in a file : sort.lst Start sorting of file1 after 3rd field(+3) and stop sorting after 4th field(-4). Basically sort file : file1 on 4th field Sorts the contents of the file : file1 and removes duplicate entries Start sort after 5th(+7.5) column of 8th field (+7.5) and stop sort on 6th column(11.6) of 12th field(11.6) Sort numerically file : file1. Used when the file contains numeric entries.

Sort u file1 Sort t | +7.5 11.6 file1 Sort n file1

tr
Syntax : tr [-c] [-d] <expr1> <expr2> <
<file> By default, it translates each character in <expr1> found in the file to its mapped character in <expr2>

Example
tr ABC abc < filenew tr [a-z] [A-Z] <filenew tr d | < fileold tr s A <fileold tr cd |/ < fileold

Significance
Translates the uppercase letters A,B and C in file : filenew to lowercase Translates all the lowercase letters in file : filenew to uppercase Deletes all the | characters from file : fileold Compress multiple consecutive presence of character A to one A Delete all characters except | and /.

nl
Syntax : nl [-w < number>] [ -s <delimiter_char>] <filename.>
Shows line number at the left of each line in the file. Reserves six characters for the number

Example
-w <number> -s <delimiter_char>

Significance
Specifies the width option for line number display to <number> characters Used to specify the character used for delimiting between line no and line content

spell
Syntax : spell [-a|-b] <filename(s)>
Lists all spellings in the file (s) the program recognizes as mistakes.

Example
-a -b

Significance
Use American spelling system Use British spelling system

find
Syntax : find <path_list> <mode> <sel-criteria> <action>
Recursively examines all files in the <path_list> Matches each file for one/more occurrence of <sel-criteria> depending on <mode> Takes some action on the files selected

Mode and Selection criteria


-name <filename> -user <username> -type <f> -group <grpname> -size +<x>[<c>] -atime +<x> -amin +<x> -atime -<x> -amin -<x> -mtime -<x> -mmin -<x> -newer <flname>

Significance
Selects file with name <filename> . Pattern matching is allowed Selects file owned by user specified by <username> Selects type of files specified by <f> Selects file if owned by group <grpname> Selects file if the size exceeds <x> blocks(characters if <c> is also specified) Selects file if accessed before <x> days Selects file if accesses before <x> minutes Selects file if accessed within <x> days Selects file if accesses within <x> minutes Selects files if modified within <x> days Selects files if modified within <x> minutes Selects file if modified after file <flname>

Action
-perm <permission_mode> -exec <command> {} \; -ok <command> {} ; -print

Significance
Finds the files with permission mode specified Executes command <command> after finding every file on the search Executes command <command> after user confirmation Shows selected files in display

grep
Syntax : grep [-c|-n|-v|-l|-I|-e|-h|-x ]
Searches a file for a pattern <pattern> <filename(s)>

Flag
-c -n -v -l -i -e -h -x

Significance
Counts number of occurrences. Output shows only the file name and number of times the searched pattern found within it Displays the line number containing the pattern, along with the lines Displays all lines excepting those containing the pattern Displays only file names containing the pattern Ignores case while searching for the pattern Extended search. More than one pattern is to be searched Suppresses printing filename when printing multiple files where the pattern is found Matches are recognized only when the entire line of the file searched matches the fixed string

Example
grep while wot.ksh

Significance
Searches for the string while in the file : wot.ksh and if found, displays the lines containing the pattern Searches for the string while in all files with extension .ksh and shows the file name and the line containing the pattern Searches for the string List of names in all .txt files. Case-independent searching of the string while takes place in all the .ksh files and the the name of the files where matched is displayed only Searches for pattern Subhendu and Suvendu in the file : name.lst Searches for the string Manoroma or Monoroma in the file : name.lst Searches all lines starting with A Searches all lines ending with Sinha

grep while *.ksh

grep List of names *.txt grep li while *.ksh

grep e Subhendu e suvendu name.lst grep 'M[ao]noroma' name.lst grep ^A name.lst grep "Sinha$" name.lst

egrep
Description Extended grep Expression
<ch>+ <ch>? <expr1>|<expr2> (<exp1>|<exp2>|<exp3>

Significance
Matches one/more presence of character <ch> Matches zero / presence of character <ch> Matches <expr1> or <expr2> Matches expression <exp1><exp3> or <exp2><exp3>

Expression
egrep '(Mano|Mono)roma' name.lst egrep 'Aka+sh' name.lst egrep f srchlist name.lst

Significance
Matches string Monoroma or Manoroma Matches Akash , Akaash,Akaash File : srchlist contains all the strings to be searched in file : name.lst

sed
Syntax : sed <options> <address,action> <filename(s)>
Stream text editor, used for:Displaying specific lines from a file by line no/pattern matching Inserting or changing texts in a file Deleting lines from a file

Can be addressed in two ways:By line number By specifying a pattern which occurs in a line

Addressing by lines
Done by specifying line number , or a pair of them separated by comma to specify the lower and upper boundaries of selection. Single word specifying action are:p q Print Quit

$p Last line Negative indicator ! can also be used to signify negation

Examples
sed 3q name.lst sed n 3,5p name.lst sed n 10,$p name.lst

Significance
Show first three lines of file : name.lst and quit Show 3rd to 5th line of file : name.lst Show from line no. 10 to end of file

sed n e 1,2p e 7,9p e $p Show 1st to 2nd line, 7th to 9th line and last line name.lst of the file : name.lst

Examples
$ sed 'a\ > subhendu > ' del>del1 $ sed i\ > subhendu > ' del>del1 $ sed $a\ > subhendu > ' del>del1

Significance
Inserts the line subhendu after every line in the file : del and save the display in file : del1 Inserts the line subhendu before every line in the file : del and save the display in file : del1 Inserts the line subhendu at the end of file : del and saves under file : del1

Context addressing
Example
sed n /getopts/p emp.ksh sed n /getopts/, /charstring/p emp.ksh sed n /[cC]hatterjee/p emp.lst sed -n '/Aka*sh/p' name.lst sed n /^A.a.s/p emp.lst

Significance
Searches the string getopts in file : emp.ksh and displays it Searches for the string getopts and charstring in file : emp.ksh Searches for the string Chatterjee or chatterjee in file : emp.lst Searches for any string with the pattern specified Searches for any line containing A as 1st character, a as 3rd character and s as 5th character Shows all but the blank lines from file emp.lst Shows all lines with 4 characters only Shows lines longer than 100 characters Shows lines containing 9 after 9 characters in a line

sed n /^$/!p emp.lst sed /^$/d emp.lst sed n /^.$/p emp.lst sed -n '/. {101, }/p' del sed -n '/. {9 }9/p' del

Examples
sed n /if/w iflist /while/w wlist emp.ksh sed n f instr.lst emp.ksh

Significance
Searches file emp.ksh for pattern : if and writes matching lines into file : iflist Searches file emp.ksh for pattern : while and writes matching lines to file : wlist Searches file : emp.ksh for the strings specified in the file instr.lst

Substitution Examples
sed s/exhausted/tired/ list1 sed s/exhausted/tired/p list1 sed 1,5s/basic/prelim/ list1

Significance
Shows the content of the file : list1 with the string exhausted replaced by tired at all occurences Shows from 1st to 5th line of file : list1 with the string basic replaced by prelim at all occurences Replaces staff with member in all the lines containing marketing Replaces student with ex student

sed n /marketing/s/staff/member/p list1 sed s/student/ex &/ name.lst

awk
Reporting tool for Unix. Developed by Alfred V Aho, Peter J Weinberger, Brian W Kernighan Emerging as programming language. Produces facilities similar to SQL language.

Syntax : awk <options> <address> { <actions> } <input|file(s)>

AWK is a pattern matching and processing language . It can search file(s) searching for a pattern, and when found, performs specified action. Works best with ascii files, preferably not to use with binary files.

Using awk from command line


$ awk { print $0 } Good morning Good morning Hello Hello ^d $ $ awk { print $1,$3 } Happy Birth day Happy day How are you? How you? ^d $

Read data from a file

name.lst
$ cat name.lst 0001 | Subhendu Majumdar | Team Lead | Avaya | 25000.00 0005 | Raghab Das 0006 | Sumit kumar Basu 0011 | Tamal Sen Sharma 0010 | Ratna Sengupta 0015 | Raghab Dasgupta 0025 | Rajib Banerjee 0012 | Damini Sen 0007| Akash Nag 0009| Anindya Das 0019| Sougata Das | Programmer| Avaya | 12000.00 | Programmer| Avaya | 12000.00 | Programmer| Avaya | 12000.00 | Accountant | Avaya | 12000.00 | Programmer| Avaya | 12000.00 | Programmer| Avaya | 12000.00 | Operator | Salesman | Manager | Manager | Nestle| 10000.00 | Nestle| 7800.00 | Nestle| 30000.00 | Avaya | 50000.00

| | | | | | | | | | |

$ awk F| { print $1,$2 } name.lst


0001 Subhendu Majumdar 0005 Raghab Das 0006 Sumit kumar Basu 0011 Tamal Sen Sharma 0010 Ratna Sengupta 0015 Raghab Dasgupta 0025 Rajib Banerjee 0012 Damini Sen 0007 Akash Nag 0009 Anindya Das 0019 Sougata Das

Taking awk instructions from a file


$ cat name.awk { print "-->" , $2 } $ awk -F"|" -f name.awk name.lst --> Subhendu Majumdar --> Raghab Das --> Sumit kumar Basu --> Tamal Sen Sharma --> Ratna Sengupta --> Raghab Dasgupta --> Rajib Banerjee --> Damini Sen --> Akash Nag --> Anindya Das --> Sougata Das

Pre-defined variables in awk


Variables
ARGC CONVFMT FIELDWIDTHS

Significance
Number of command line arguments Conversion format for numbers Whitespace separated string for the width of input fields. Provides a facility for fixed-length fields instead of using field separators. Name of current input file Current record number Input field separator 0(Case sensitive) 1 ( Case insensitive) Number of fields in the current record Current record number

FILENAME NR FS IGNORECASE NF FNR

Pre-defined variables in awkcontd


Variables
OFS ORS RS FILENAME NR FS IGNORECASE ENVIRON ERRNO

Significance
Output field separator Output record separator(default is newline) Input record separator( default is new line) Name of current input file Current record number Input field separator 0(Case sensitive) 1 ( Case insensitive) Unix environment variables Unix system error message number

Example of using pre-defined variables


$ cat name1.lst 0001 | Sourav Ghar $ cat name2.lst 0001 | Sourav Dutta 0002 | Ratna Sengupta $ cat name.awk { FS = "|" ; OFS = " ** " ; ORS = "\n-------------------------------------------\n"; print ARGC , ENVIRON["TERM"], CONVFMT , FILENAME ; print NR , $2 , $3 , "Total fields" , NF ; } | Project M | Avaya | 25000.00| | Team Lead | Avaya | 25000.00|

| Programmer| GFS

Example of using pre-defined variablescontd


$ awk -f name.awk name1.lst name2.lst 3 ** vt100 ** %.6g ** name1.lst ------------------------------------------1 ** Sourav Ghar ** Team Lead ** Total fields ** 6 ------------------------------------------3 ** vt100 ** %.6g ** name2.lst ------------------------------------------2 ** Sourav Dutta ** Project M ** Total fields ** 6 ------------------------------------------3 ** vt100 ** %.6g ** name2.lst ------------------------------------------3 ** Ratna Sengupta ** Programmer ** Total fields ** 4 -------------------------------------------

Regular Expression Metacharacters in Awk:Expression


\n \t ^ $ . [ABC] [A-Ca-c] [^ABC] Desk|Chair [ABC][DEF]

Significance
Newline Tab Starts match at the beginning Matches at the end of the string Matches any single character Matches any of A,B or C Matches any of A,B,C,a,b or c Matches any character other than A,B or C Matches any of Desk or Chair Matches any of A,B or C followed by D,E or F

Regular Expression Metacharacters in Awkcontd


Expression
[ABC]* [ABC]+ [ABC]? ( )

Significance
Matches zero/more occurences of A,B or C Matches one/more occurences of A,B or C Matches to an empty string or more of A,B or C (Blue|Black)berry matches to Blueberry or Blackberry

Comparison Operators in awk


Operators
== < <= > >= != ~ !~

Significance
Is equal to Less than Less than or equal to Greater than Greater than or equal to Not equal to Matched by regular expression Not matched by regular expression

Compound pattern Operators


Operators
&& || ! ( )

Significance
Logical AND Logical OR Logical NOT Used to group compound statements

Range pattern Operators


Operators
awk -F"|" 'NR==3,NR==5 { print NR,$2 } ' name.lst

Significance
Shows 3rd,4th and 5th record

BEGIN and END blocks


BEGIN { <declare variables> < write headings> }

......................... ......................... END { < show subtotals> }

$ awk /Programmer/ { print } name.lst

# Details of all programmers

0005 | Raghab Das | Programmer| Avaya | 12000.00| 0006 | Sumit kumar Basu | Programmer| Avaya | 12000.00| 0011 | Tamal Sen Sharma | Programmer| Avaya | 12000.00| 0015 | Raghab Dasgupta | Programmer| Avaya | 12000.00| 0025 | Rajib Banerjee | Programmer| Avaya | 12000.00|

$ awk /Prog*/ { print } name.lst

# Details of all programmers

0005 | Raghab Das | Programmer| Avaya | 12000.00| 0006 | Sumit kumar Basu | Programmer| Avaya | 12000.00| 0011 | Tamal Sen Sharma | Programmer| Avaya | 12000.00| 0015 | Raghab Dasgupta | Programmer| Avaya | 12000.00| 0025 | Rajib Banerjee | Programmer| Avaya | 12000.00|

$ awk '/Operator/ ' name.lst


0012 | Damini Sen

# Details of all Operators

| Operator | Nestle| 10000.00|

$ awk -F "|" '/Operator/ { print $2,$5 }' name.lst #Field 2 and 5


Damini Sen 10000.00

$ awk -F"|" 'NR==3,NR==5 { print NR,$2 } ' name.lst 3 Sumit kumar Basu 4 Tamal Sen Sharma 5 Ratna Sengupta (Shows line number and second field of 3rd-5th records)

$ awk -F"|" '/(Sen|Das)gupta/ { printf "%3d % -20s n" , NR , $2 }' name.lst


5 Ratna Sengupta 6 Raghab Dasgupta (Shows the record number and names of all Sengupta-s and Dasgupta-s)

$ awk -F"|" '$3==" Programmer" { print $2,$4,$5 }' name.lst Raghab Das Avaya 12000.00 Sumit kumar Basu Avaya 12000.00 Tamal Sen Sharma Avaya 12000.00 Raghab Dasgupta Avaya 12000.00 Rajib Banerjee Avaya 12000.00 (Shows 2nd,4th and 5th field of all records where 3rd field contains the word Programmer)

$ awk -F"|" '$3~ /Manager/ && $4~ /Nestle/ ' name.lst 0009 | Anindya Das | Manager | Nestle| 30000.00|

(Shows the records where 3rd field contains the string Manager and 4th field contains the string Nestle.

$ awk -F"|" '$3!~ /Programmer|Accountant/' name.lst 0001 | Subhendu Majumdar | Team Lead | Avaya | 25000.00| 0012 | Damini Sen 0007 | Akash Nag 0009 | Anindya Das 0019 | Sougata Das | Operator | Manager | Manager | Nestle | 10000.00| | Nestle| 30000.00| | Avaya | 50000.00| | Salesman | Nestle| 7800.00 |

(Shows all records where 3rd fiels contains strings other than Programmer and Accountant) $ awk -F"|" '$5>=30000 { printf "%20s t t %d n" , $2, $5 }' name.lst Anindya Das 30000 Sougata Das 50000 (Shows the name and salary of persons with salary >=30,000)

$ awk -F"|" '$3~ /Manager/ { > kount = kount + 1 > print kount,$2 } ' name.lst 1 Anindya Das 2 Sougata Das

$ awk -F"|" '$5>=30000 { printf "%20s t t %d n" , $2, $5 }' name.lst Anindya Das 30000 Sougata Das 50000 (Shows the name and salary of persons with salary >=30,000)

$ cat salavg.awk

BEGIN { printf "\n\t\t Salary Report of Managers \n\n" } $3~ /Manager/ { kount++ tot = tot + $5 print kount,$2,$5 } END { print "\n\nTotal managers found is :",kount print "Average salary is :",tot/kount }

$ awk -F"|" -f salavg.awk name.lst

Salary Report of Managers 1 Anindya Das 2 Sougata Das 30000.00 50000.00

Total managers found is : 2 Average salary is : 40000

String Operators
(A) Concatenating Strings x = abcdef y = ghi z=xy # z = abcdefghi # x = abcdef

Built-in String Functions Functions gsub( <reg>,<string>,<target>) Significance Substitutes string <string> in string <target> every time the regular expression <reg> is matched Returns the position of the string <search> in string <string> Returns the length of the string <string> Returns the position in string <string> that matches the expression <reg> Splits string <string> into array elements of <store> based on delimiter <delim> Substitutes string <string> in <target> the first time the regular expression <reg> is matched.

index( <search>,<string> ) length(<string>) match( <string> , <reg>) split(<string>,<store>,<delim>)

sub(<reg>,<string>,<target>)

Built-in String Functions Functions substr(<string>,<pos>,<len>) Significance Extracts the portion of the string <string> starting from position <pos> of length <len> Translates the string <string> rto lower case Translates the string <string> rto upper case

tolower(<string>) toupper(<string>)

Use of Character Functions in awk $ cat awkf.awk $5 >= 30000 { print "Length of the string is : " , length ( $2 ) print "First three characters are : " , substr( $2,1,3 ) print "String in lowercase is : " , tolower( $2 ) print "String in uppercase is : " , toupper( $2 ) nm = substr( $2,2,4 ) print " 2nd to 5th characters are :", nm }

Use of Character Functions in awkContd $ awk -F"|" -f awkf.awk name.lst Length of the string is : 19 First three characters are : An String in lowercase is : anindya das String in uppercase is : ANINDYA DAS 2nd to 5th characters are : Anin Length of the string is : 19 First three characters are : So String in lowercase is : sougata das String in uppercase is : SOUGATA DAS 2nd to 5th characters are : Soug

Use of Character Functions in awkUse of Split function $ cat split.awk BEGIN { FS="|" printf "\n Details of name of the Employees : " print "\n\t Name \t\t\t\t First name \t\t Last name" print "\n\t............................................................" }

$5 > 15000 { split( $2 , a , " " ) print "\n\t", $2, "\t\t", a[1] , "\t\t" , a[2] }

Use of Character Functions in awkUse of Split function $ awk -f split.awk name.lst Details of name of the Employees : Name First name Last name

...........................................................

Subhendu Majumdar Anindya Das Sougata Das

Subhendu Anindya Sougata

Majumdar Das Das

Special String Constants Expression \\ \a \b \f \n \r \t \v \ Significance Backslash Alert or bell character Backspace Formfeed Newline Carriage return Tab character Vertical tab Double quote

Built-in Numerical Functions Expression cos(x) int(x) exp(x) log(x) rand( ) sin(x) sqrt(x) systime() Significance Cosine of x in radians Integer value of x Returns e raised to power of x Natural log of x Returns random number between 0 and 1 Returns the sine of x in radians Returns square root of x Current time in seconds since midnight, Jan 1,1970

Arithmetic Functions Expression x^y x**y x%y x+y x-y x*y x/y x++ ++x y---y Significance Raises x to the power of y Raises x to the power of y Calculates the reminder of division of x by y Adds y to x Subtracts y from x Multiplies y with x Divides x by y Increments x by 1 and then uses it Uses x and then increments it by 1 Decrements y by 1 and then uses it Uses y and then decrements it

Arithmetic Functions Expression x+=y x-=y x*=y x/=y x++=y Significance x = x+y x = x-y x = x*y x = x/y x=x+1+y

Arrays Arrays are normally used to handle more than one related piece of data. One accesses the individual elements within an array by enclosing the subscript within double brackets. In AWK, one does not have to declare a variable to be an array, and does not have to define the maximum no. of elements. When one uses an element for the first time, it is created. In awk arrays, subscript is a string ; viz., tot_sales[Cal] = 10.15 One can use this in conditional flow:If Cal in tot_sales Delete tot_sales[Cal]

Multidimensional Arrays One can use two subscripts to form a multi-dimensional array. Example : tot_sales[India,Calcutta] = 100 And can use this in conditional statements also:If (India,Calcutta) in tot_sales

Examples of Arrays BEGIN { FS = "|" print "\n\t Name \t\t Basic \t\t da \t\t hra \n" print "---------------------------------------------------------------" } $3~ /Manager/ { da = $5 * 0.25; hra = $5 * 0.3; print $2 ,"\t" , $5 , "\t" , da , "\t\t" , hra ; tot[1] += $5; tot[2] += da ; tot[3] += hra ; tot[4] += $5 + da + hra ; } END { print "\n-------------------------------------------------------------" print "Total" , "\t\t\t" , tot[1] , "\t\t" , tot[2] , "\t\t" , tot[3] ; print "---------------------------------------------------------------" print "\n\n Grand Total : ", tot[4] ; }

Examples of Arrayscontd $ awk f salcalc.awk name.lst Name Basic da hra

--------------------------------------------------------------Anindya Das Sougata Das 30000.00 50000.00 7500 12500 9000 15000

------------------------------------------------------------Total 80000 20000 24000 ---------------------------------------------------------------

Multidimensional Arrays-Examples

$ cat pop.lst India|Chennai|West|150000 India|Calcutta|North|234500 India|Calcutta|East|134500 India|Chennai|East|100000 India|Calcutta|South|234508 India|Calcutta|Wast|100500

Multidimensional Arrays-Examplescontd $ cat pop.awk BEGIN { system(" tput clear " ); print "\n\nPopulation in different parts of the cities in India\n\n" FS = "|" } { print $2 , "\t" , "\t" , $3 , "\t" , $4 ; tot_pop[$1,$2]+=$4; } END { print "--------------------------------------------------------" print "Population for Calcutta : " ,tot_pop["India","Calcutta"]; print "Population for Chennai" , tot_pop["India","Chennai"]; }

Multidimensional Arrays-Examplescontd

$ awk f pop.awk pop.lst Population in different parts of the cities in India Chennai Calcutta Calcutta Chennai Calcutta Calcutta West East East West 150000 134500 100000 100500

North 234500

South 234508

-------------------------------------------------------Population for Calcutta : 704008 Population for Chennai 250000

Conditional Flow

{ if .. else if .. else if .. else }

$ cat sal.awk 4 ~/Nestle/ { if ( $5 > 10000 ) print $2 , $5 , "Taxable"; else print $2 , $5 , "Non taxable"; }

$ awk -F"|" -f sal.awk name.lst Damini Sen Akash Nag Anindya Das 10000.00 Non taxable 7800.00 Non taxable 30000.00 Taxable

Conditional Flow To substitute if test anywhere in the code:condition ? True : false

$ cat sal.awk $4 ~/Nestle/ { $5 > 10000 ? taxyn = "Taxable" : taxyn = "Non taxable" ; print $2 , $5 , taxyn ; } $ awk -F"|" -f sal.awk name.lst Damini Sen Akash Nag Anindya Das 10000.00 Non taxable 7800.00 Non taxable 30000.00 Taxable

Loops Loops

Loops can be of three types : Do While For Used to perform some repeatitive jobs.

Do Loop do <statement> While ( <conditions> ) statement in any of the above constructs may be either a simple statement or a series of statements enclosed in { }

Example of Do Loop $ cat doloop.awk BEGIN { linesep = "-"; linedraw = "-"; FS = "|"; } { res = $5 / 1000 ; do { linedraw = linedraw"-" ; res--; } while ( res >= 1 ); print $2 , linedraw ; linedraw = linesep ; }

Example of Do Loopcontd $ awk f doloop.awk name.lst Subhendu Majumdar -------------------------Raghab Das -------------

Sumit kumar Basu ------------Tamal Sen Sharma ------------Ratna Sengupta Raghab Dasgupta Rajib Banerjee Damini Sen Akash Nag Anindya Das Sougata Das -------------------------

--------------------------------------------------------------------------------------------------------------

While Loop While ( <condition> ) statement statement in any of the above constructs may be either a simple statement or a series of statements enclosed in { }

Example of While Loop $ cat while.loop BEGIN { linesep = "-"; linedraw = "-"; FS = "|"; } { res = $5 / 1000 ; while ( res >= 1 ) { linedraw = linedraw linesep ; res--; } print $2 , linedraw ; linedraw = linesep ; }

Example of While Loopcontd $ awk f while.loop name.lst Subhendu Majumdar -------------------------Raghab Das -------------

Sumit kumar Basu ------------Tamal Sen Sharma ------------Ratna Sengupta Raghab Dasgupta Rajib Banerjee Damini Sen Akash Nag Anindya Das Sougata Das -------------------------

--------------------------------------------------------------------------------------------------------------

For Loop
For ( <var> = <value> , var <operator> <value_last> , <var> [++|--] <statements> For ( <subscript> in <array> ) <statement>

statement in any of the above constructs may be either a simple statement or a series of statements enclosed in { }

Example of For Loop $ cat for.loop BEGIN { linesep = "-"; linedraw = "-"; FS = "|"; } { res = $5 / 1000 ; for ( res = res ; res >= 1 ; res -- ) { linedraw = linedraw linesep ; res--; } print $2 , linedraw ; linedraw = linesep ; }

Example of For Loopcontd $ awk f for.loop name.lst Subhendu Majumdar -------------------------Raghab Das -------------

Sumit kumar Basu ------------Tamal Sen Sharma ------------Ratna Sengupta Raghab Dasgupta Rajib Banerjee Damini Sen Akash Nag Anindya Das Sougata Das -------------------------

--------------------------------------------------------------------------------------------------------------

Loop Breaking Statements Statement Break Continue Significance Exits the loop Leaves the current record and continues with the next record in the loop from beginning of the loop processing statements.

Pretty formatting - printf


Syntax : printf( format_specifier, var1, var2,..varn)

Format %c %d %i %e %f %s %g

Meaning Ascii character Integer Integer Floating point number using scientific notation Floating point number(eg., 10.43) String of characters Awk chooses %e or %f display format(whichever is shorter) suppressing non-significant zeroes.

Output to another file Statement Printf( Hello world\n) > datafile Printf( Hello world\n) >> datafile Significance Creates a file called datafile with the output Appends the output at the end of existing file datafile or creates a new file.

A file should be closed after it receives output from an awk program. Syntax : close(filename)

Functions Functions

Block of code, accesses usually in multiple places in the code. When awk reaches the end of the function, it implicitly returns the control to the calling routine. To make an explicit return to the main program, one can explicitly use the return statement. Syntax : function fname(parameter list) { < function_code> }

Example of Functions $ cat subcalc.awk BEGIN { FS = "|" show_heading( "\n\t Name \t\t Basic \t\t da \t\t hra \n" ) ; show_heading( "-------------------------------------------------"); } $3~ /Manager/ { da = calc_comp(0.25,$5) ; hra = calc_comp(0.3,$5) ; print $2 ,"\t" , $5 , "\t" , da , "\t\t" , hra ; } function show_heading(heading) { print heading ; } function calc_comp(rate,val) { val = val * rate ; return val ; }

Example of Functionscontd $ awk f salcalc.awk name.lst Name Anindya Das Sougata Das Basic da 7500 12500 hra 9000 15000

--------------------------------------------------------------30000.00 50000.00

Use of inputs from user in awk program BEGIN { FS="|" printf "%20s%3d\n\n", "Cut-off basic pay : ",cobp } $5 > cobp { kount++ print $2 , $3 , $5 , "\n" } END { print "\n\n The End\n\n" }

Use of inputs from user in awk programContd $ awk -v cobp=15000 -f tkinp.awk name.lst Cut-off basic pay : 15000

Subhendu Majumdar Team Lead 25000.00 Anindya Das Sougata Das Manager Manager 30000.00 50000.00

The End

Controlling Environment

The Unix environment is controlled by a number of pre-defined environment variables. They are usually defined in the file .profile for the user or are defined by user as and when required.

Pre-defined variable
ERRNO LINENO

Significance
Non-zero exit code of last command that failed. Value changes only when a command fails Meaningful only within a shell script. Its value is the line no. of the line in the script currently being executed Value is always the full pathname of the directory where the control resided before coming into current working directory Current working directory Integer number of seconds since one invoked the Korn shell Value of the process id of the parent process of $$ Absolute directory path under which the programs for the commands reside.

OLDPWD

PWD SECONDS PPID PATH

Pre-defined variable
EDITOR CDPATH

Significance
Vi or emacs List of colon separated directory name, which is followed by the system when the user issues a cd command Pathname of the shell script containing commands to be executed when the korn shell is invoked Filename of the korn shell history file Integer number containing maximum number of commands to be retained in history files Pathname of home directory for an user

ENV HISTFILE HISTSIZE HOME

Pre-defined variable
IFS

Significance
Zero/more characters to be treated by the shell as de-limiters when parsing a command line into words or using the read command Display width used by Korn shell edit mode- vi or emacs Integer number representing the number of lines displayed by the terminal. Pathname of a file to be monitored by the shell for a change in its date of last modification. If change is noted, the shell issues the message : You have mail at the next oppurtunity No. of seconds after which the shell should check for a change in the MAIL file Primary shell prompt string.Prompt where commands are issued by the user

COLUMNS LINES MAIL

MAILCHECK PS1

Pre-defined variable
PS2 PS4 SHELL TERM TMOUT

Significance
Secondary prompt screen. Shown when a command pawns more than one line. Debug prompt screen , shown when a shell program is executed using v option. Pathname of the shell the user is using Symbolic alpha numeric string that identifies the type of your terminal Seconds after which automatic logout occurs.

stty
Controls terminal output and sets terminal characteristics Options
stty a stty echoe stty echo stty echo stty intr ^Z stty eof \ ^a stty erase ^H stty quit ^d

Significance
Displays all current settings Enables backspacing remove character from the display Keyboard entry is not echoed Keyboard entry is echoed Sets <Ctrl-Z> as the interrupt key Sets <Ctrl-a> to terminate output and declare end-offile, for eg., while creating file using cat command Typing <Ctrl-H> helps to remove the last character typed Typing <Ctrl-d> enables aborting the current shell

Options
stty susp ^Z stty stop ^S stty start ^Q stty rows 20 stty rows 20 column 80 stty iuclc stty olcuc stty size stty eol ^J stty sane

Significance
Enables suspending a foreground process when <ctrl-Z> is pressed Enables halting the current session by pressing ^S Enables starting the current halted session by pressing ^Q Set 20 rows in display Sets 20 rows and 80 columns in display Maps uppercase alphabets to lowercase Maps lowercase alphabets to uppercase Gives the current screen size in terms of rows and columns Pressing <Ctrl-J> does the job of ending a line Set the terminal characteristics to values that will work for most terminals

Process

This section will give you a brief idea on

Process

Running Jobs

Scheduling Jobs

What is a process?

Process is a program that has its own address space. Every command fired in Unix has a process associated with it. Is born when a program starts execution, and remains alive as long as the program is active.After execution is complete, it dies. Has a name, usually the name of the program being executed.

Kernel controls the process

Kernel is finally responsible for managing the process. Determines time and priorities allocated to processed, enabling multiple processes to share CPU resources. Each one is uniquely identified by PID, Process Identifier, allocated by the Kernel when it is born.

Sh Process

A process is immediately set up by the Kernel when a user logs into Unix system.This os usually a Unix command(sh/ksh/bash etc). Any command executed is actually input to the shell process. Can be known from the value of shell variable $$. This process for the shell remains alive till the user logs out.

Parent and Child process

Every process has a parent process(except the root process). When a command is executed from the shell prompt, that becomes a child process to the parent shell process. A parent process can ave more than one child process Cat name.lst| grep student Both the process : cat and grep have same parent (shell) process

ps process status
Displays the attributes of a process Command
ps

Significance
Selects the process associated with the current terminal.Shows output in four columns: PID, TTY(terminal info), TIME(total cpu time used by the process), CMD(command gererating the process) Displays process ancestry.Produces 8 column output:- UID(userid),PID,PPID(Parent PID), C( amt of CPU time consumed by a process),STIME(time the process started),TIME,CMD

ps f

Command
ps u ems2000 ps a ps x ps e

Significance
Displays processed of user : ems2000 Displays all processes of all users; excepting the system processes Shows command line in extended format Displays system processes

How a process is created in Unix?

Forking : A copy of the the process that invokes a new process is created.Child gets a new PID. Exec : The parent then overwrites its image with a copy of the program to be executed. Done with exec system call. No additional process is created at this stage. Wait: Parent then executes the wait system call to keep waiting for the child process to complete. After the child process is completely executed, a terminal signal is sent to the parent.

Internal and External Commands

External program : Commands like : cat, ls,


grep, sed etc are external programs.Shell creates a process for each of these commands.

Shell Scripts : Shell executes shell scripts by


spawning to another shell, which then becomes the parent of the commands featured in the script.

Internal Commands : These are commands


which are executed directly by the current shell. No additional processes are generated. Example : cd , echo etc

Running jobs in background

Unix is a multi-tasking system , allowing to perform more than one job at a time. One job can be executed in foreground at a time, but many can be executed at background. & operator at the end of the command line interprets the job to be done at background. Shell returns the PID of the background job for tracking.

Job control in Korn and Bash Shells

Korn or bash shell users can use the job control facility of the shell to manipulate jobs.One can put a job in the background, bring it back to foreground, suspend it, run it later, or even kill it.

bg

You are running a job in the foreground. It is taking a lot of time. You want to do some other jobs. Press ^Z. The job running in the foreground will be suspended. Enter the command : bg at the OS prompt and push it to be executed in the background. Unix will return you a PID for the process pushed to background.

jobs

Shows the jobs running in background. Command : jobs l prints PID of each job besides job number. This command is used by the user to push a job from background to foreground.

fg

Used to bring a background job to foreground and execute it. fg [%<job_no>] brings the job from background to foreground and executes it. For example , issuing of the command : fg %2 executes job number 2 (viewed by jobs command) from background to foreground and executes it. Execution of fg command alone from the OS prompt resumes latest created background process to be executed inforeground.

stop Syntax : stop [%<job_no>]


Pauses a job which is being executed in background. One can use the command : bg to resume execution of the stopped job in background or can issue command : fg to bring it to foreground and execute it.

kill

Syntax : kill [options] <PID>

System often requires to control execution of a process. This is done by sending signals to the process. The process, after receiving the signal, may ignore it, terminate itself, or do something else. The command : kill is used to terminate or suspend a process running in background, taking unusual long time.

Example
kill 2905 kill 9 2905 kill s SIGKILL 2905 kill 15 2906 kill s SIGTERM 2906 kill 24 2900 kill 25 2900 kill 26 2900 kill s SIGCONT 2900 kill &!

Significance
Kills the process with Pid : 2905 Surely kills a process with PID 2905 Terminates the process with PID 2906 Pause the process with PID 2900 Start the paused process with PID 2900 Kill the latest background job

nice
Processes in the Unix are usually executed with equal priority. But, high priority jobs must be executed at the earliest. Unix offers : nice command to change the priority of execution of jobs. Ordinary users can only reduce priority; super user can do both. Nice value ranges from 0 to 39 ; commands run with a value of 20 in both. Example : nice n 15 prog1.sh & The above command reduces the pririty value to 35 The nice and priority values of the processes can be displayed with ps l command.

batch
Schedules job for later execution, when the system load permits it. Prevents too many high load jobs to run at the same time. Syntax for running a program : batch < prog1.sh Any job scheduled in this way also goes to the at queue and can be removed using : at r , provided it is fired before the job has been executed.

at
Schedules a program for execution at a specified time. Displays a list of scheduled jobs. Removes jobs from the scheduled list.

Flag
-l -m -r <joblist>

Significance
Displays the list of jobs scheduled by the user Mail a report of successful execution of the job Remove the jobs specified in the joblist from queue

Examples
at 2300 my_job.sh at 11:00 pm my_job.sh at 11:00 P my_job.sh at 2300 today my-job.sh at now + 6 hours njb.sh

Significance
Executes program my_job.sh at 11:00 pm tonight

Schedules execution of njb.sh after 6 hours from now

at 6:30 pm next week njb.sh Schedules execution of program : njb.sh at 6:30 pm next week at now + 1 year njb.sh Schedules execution of program : njb.sh after 1 year from now Schedules execution of program : njb.sh at 3:08 pm tomorrow.

at 3:08 pm + 1 day njb.sh

Examples
at 9 am Mon njb.sh at 9 am tomorrow at now + 5 minutes < njb.sh at f njb.sh now + 5 minutes at 08:15 Jan 12 < njb.sh

Significance
Schedules execution of njb.sh at 9 am coming Monday. Schedules execution of njb.sh at 9 am tomorrow. Schedules execution of njb.sh after 5 minutes from now. Schedule execution of njb.sh at 08:15 am on January 24.

cron
Runs jobs periodically.

Mostly dormant, but wakes every minute and checks for a file in /usr/spool/cron/crontabs directory for programs to be executed at that instant.

A user may be permitted to place a crontab file after his login name in this directory. This file contains a list of commands, along with a schedule for execution.

Sample format for a crontab file

First field indicates number of minutes after the hour when the command is to be executed. The range 00-10 schedules every minute in the first 10 minutes of the hour. Second fields indicates the hour in the 24 hour format for scheduling. 3rd field(1 to 31) controls the day of the month. * means all. 4th field(1-12) indicates the month 5th field(0-6) indicates the day of the week(Sunday with 0) 6th field contains the command

User can modify the file containing his cron commands as follows:Create a file : cron.txt in the format described earlier. Issue the following command from the Os prompt: crontab cron.txt. There will be now a file ems2000( for eg., the name of the user) in /usr/spool/cron/crontabs with the contents of cron.txt. One can see the contents of crontab file issuing the command : crontab -l

System Administration

System Administration

shutdown Shutdown Unix server su Go as you like wall Send message to everybody umask Set Default Permission ulimit Set maxm. Space for user du Show disk usage df Free bytes available finger Show User information crypt Password protect a file

shutdown
Executed from the root user to shut down the system.

Commands
shutdown g2 shutdown y g0 shutdown y g0 i6 shutdown 17:30 shutdown r now shutdown m

Significance
Powers down system after 2 minutes Immediate shutdown Shutdown and reboot Shutdown at 5:30 pm Shutdown and reboot Bring the system down to maintenance(single user mode)

su
Takes to super user mode.

Commands
su

Significance
Takes to super user mode. One can have all the accesses same as the root user. Root password is required after executing the command One logs in as the user : mat. Asks for the password.

su mat

wall
Addresses all the users simultaneously.

$ wall . ^D All the text written will be reflected in the terminals of all the users. $ wall < mesg.txt Contents of the file mesg.txt are sent as the wall message.

umask

Used by System Administrator to set the default permissions to assign to each file to be created by the user. User can modify this default settings. To give permission 751 as a default to newly created files, umask value will be (777 751) = 026. This value is assigned as a command : umask 026

ulimit
Faulty programs or processes can eat up disk space.So, a restriction is to be imposed on the mazimum size of a file that an user is permitted to create. Ordinary user can reduce the default value . Super user can increase / decrease it.

Examples
ulimit a ulimit Ha ulimit t <size> ulimit f <size> ulimit d <size>

Significance
Shows soft limits Shows the hard limits Set the CPU time in seconds Set the maxm. Size in blocks Sets the maxm. size of data blocks in KB

ulimit m <size> Sets the maxm. Size of memory in KB.

du
Shows disk usage amount of space taken by a group of files in units of 512 bytes or KB. Descends all sub-directories from the directory in which the command is fired.

Flag Significance
-a -k -s -r Print entries for each file encountered in the directory hierarchies in additional to the normal output Give the block count in terms of 1024 bytes Give the grand total of disk usage for each of the directories Print messages about directories that cannot be read, files cannot be accessed etc.

df
Displays the number of free 512 byte blocks and free inodes available for file systems by examining the count kept in the superblocks.

Flag Significance
-b -e -f -i -k Reports only the number of KB free Report the number of files free Reports only the actual count of the blocks in the free list Reports the total number of inodes, number of free inodes, number of used inodes,% of inodes in use Reports the allocation in KB

finger
By default, the command finger lists for each user in the system: Login name Full given name Terminal write status( if write permission is denied) Idle time Login time Users home directory and login shell Project on which the user is working Any plan put by user in the file .plan under home directory Office location and telephone number Last time the user received the mail, the last time user read the mail.

Flag Significance
-b -f -h -i -l -p -R -s Suppresses information on home directory and shell Suppresses the header info Suppresses the project information Suppresses the info on idle time Suppresses long output Suppresses plan Print users hostname Forces short output format

Files accessed while showing output File


/etc/utmp /var/admin/utmp /etc/passwd /etc/.plan /etc/.project /var/mail

Significance
Who file Last login file Users name, office etc Users plan Users project info Mail directory

crypt
Used to encode/decode files using personal password

Options
crypt < uncrypted_file > crypted_file crypt passwd < file1 > file2 crypt passwd < file2 > file1 vi x file2

Significance
Crypts file uncryped_file to crypted_file. Asks for a password. Crypts file : file1 to : file2 based on the password : passwd Decrypts file : file2 to : file1 using the password : passwd Helps to view an encrypted file : file2. Asks for a password

Communication

Communication
mailx Send and receive mails

write Send message to all users

ftp Send and receive files

rlogin Remote login

rcp Copy files to remote server

mailx
Used to send/receive mails Syntax for sending mails:echo <body> | mailx s <subject> <remote_address> cat <message file> | mailx s <subject> <remote_address> Syntax for receiving mails:mailx -e mailx [-HL] [-u user] mailx -f [-HL] [filename]

Sending mail Case : 1


$ mailx s Test heading smajum@avaya.com this is a test mail for communication ~.
The job performed above will send a mail to : smajum@avaya.com with subject of the mail as : Test heading and body of the mail as : this is a test mail for communication.

Case : 2
$ mailx smajum@avaya.com Subject : Test mail number 2 this is a test mail for communication ~.
The job performed above will send a mail to : smajum@avaya.com with subject of the mail as : Test mail number 2 and body of the mail as : this is a test mail for communication. If no subject is entered at the command line, a prompt asks for the subject.

Case : 3
$ echo Hello mike|mailx s Heading1 smajum@avaya.com The job performed above will send a mail to : smajum@avaya.com with subject of the mail as : Heading1 and body of the mail as : Hello mike.

Case : 4
$ cat msgfile| mailx s Heading1 smajum@avaya.com The job performed above will send a mail to : smajum@avaya.com with subject of the mail as : Heading1 and body of the mail with the texts written in the file : msgfile.

Case 5- You want to send a mail to some user and cc it to some other users.
$ mailx smajum@avaya.com Subject : Test mail number 2 this is a test mail for communication ~h To: smajum@avaya.com Subject: testing Cc: ddatta@avaya.com Bcc: shampac@avaya.com (continue) testing mail--please ignore ~. EOT .

Viewing received mails


mailx -e mailx [-HL] [-u user] mailx -f [-HL] [filename]

Flag
-e

Significance
Test for presence of mail. mailx prints nothing and exits with a successful return code if there is mail to read.

0 Mail present 1 No mail 2 Other error -f


Read messages from filename instead of the user's system mailbox. If filename is not specified, the secondary mbox is used. Prints header summary only Read user's mailbox.Can be used only if read access to user's mailbox is not read protected.

-H/-L -u user

Options available in viewing received mail


When one executes mailx command for viewing received mails:-

$ mailx
mailx Revision: 1.179.214.2 date: 98/12/01 01:29:55 Type ? for help. "/var/mail/ems2000": 2 messages 2 unread >U 1 smajum@avaya.com Mon Aug 18 04:50 33/1199 RE: 3rd mail U 2 ems2000 schedule ?_ Mon Aug 18 03:55 16/561 Program to be re-

It shows a list of unread mails with a ? prompt below, waiting for commands from the user.

Commands available at ? command prompt

Flag
<n> type quit(q) next reply <n> mail <user>

Significance
Shows message number <n> , the number typed at the first column for every message.
Shows all the messages in one shot, one by one Quit mail utility

Shows next message Reply to message number <n> Opens mail utility to write mail to user <user>

write
Type text into other users terminal for information

Case : 1
$ write ems2000 Hello, how are you ^d
The command flashes the message : Hello, how are you to all persons terminal who have logged in as user : ems2000.

Case : 2
$ write ems2000 pts/tc Hello, how are you ^d
The command flashes the message : Hello, how are you to that persons terminal who is working in terminal pts/tc and logged in as user : ems2000.

Case : 3
$ write ems2000 < fileshow.txt
The command flashes the message written in the file : fileshow.txt to all persons terminal who have logged in as user : ems2000. User must be logged in and mesg should be set to y( mesg=y) for successful write communication.

ftp
Syntax : ftp [-i] [-v] [-n] <remote_host>
Copies file over a network connection between local host to remote host. ftp runs on client server.

Flag Significance
-i -n Disable interactive prompting by multiple file commands.
Disable auto-login. If auto-login is enabled and user issues an open command to establish connection to a remote server, he gets login and passwd prompt. If it is disabled by n, user does not get such prompts. He then writes: user <username> <passwd> in the ftp mode to specify the user and password. -v Enables verbose output. IT shows the actions when files are passed from one server to another.

-v

Commands fired from ftp mode Command


! <command> append <lf> <rf>

Significance
Executes command <command> in the local server Copy file <lf> in the local server to the end of file : <rf> in the remote server. If no such file exists in remote server, a new file is created. Sets the file transfer type to ASCII Sets the file transfer type to binary Close the connection to remote host, if it is open, and exit Remote file names with name containing all letters in uppercase are translated to lowercase when the file is transported from remote server to local. Set the working directory in the remote server to <remdir> Set the current directory of the remote server to the parent of current working directory.

ascii binary bye case

cd <remdir> cdup

Command
chmod <mode> <file> close mdelete <remote_files> mdir <rem_files> <lcl_file> mget <rem_files> get <rem_file> [<lcl_file>] mkdir <dirname> mls <remfiles> <lclfile> modtime <rem_file> mput <local files> delete <rem_file>

Significance
Changes the mode of the file : <file> to mode <mode> Terminate the connection to the remote server Deletes the files in the remote server Writes a list of remote files to local file <lcl_file> Copy remote files to local system Copt the remote file to local file. Creates a directory <dirname> in remote server. Writes an abbreviated listing of remote files to local file <lclfile> Shows the last modification time of remote file <rem_file> Copy files from local server to remote server Delete file <rem_file> from the remote server. The file can be an empty directory.

Command
dir <rem_dir> [<lcl_file>] disconnect beep lcd [local_dir]

Significance
Shows a listing of remote directory <rem_dir> to terminal/ into local file <lcl_file> . Close the connection to remote host Beeps when command is executed Specifies the current working directory of the local server to directory : <local_dir> . If unspecified, users home directory is considered for navigation. get file if remote file <file_name> is newer than local file <file_name> Toggle forcing interactive prompting on multiple commands Toggle store unique for local files. IF turned on, then while receiving files from remote host, if a remote file already exists with a name equal to a local file, a .1 is appended to the name. For another repeatition, a .2 is appended till 99 such cases are found.

newer <file_name> prompt runique

Command
size <remote_file> quit reget <remfile> [<lclfile>]

Significance
Shows the size of the remote file bye Acts like get. If local file <lclfile> exists and is smaller than the remote file <remfile>, local file is assumed to be partially transferred from remote server.Then, the transfer is continued from the apparent point of failure. This command is very useful to transfer very large files over the netwoprk that tend to drop connections. Shows the current status of ftp- informations are shown on remote server name and all the settings(prompt,type,bell,case etc) Toggle store unique for remote files. If turned on, then while transferring files to remote host, if a remote file already exists with a name equal to a local file, a .1 is appended to the name of the file transferred from local to remote server.. For another repeatition, a .2 is appended till 99 such cases are found.

status

sunique

Command
system umask [new mask]

Significance
Shows the type of operating system running in remote server Set the default umask on the remote server to new value. IF nothing is specified, the current umask value of the remote server is displayed. If only help is typed from ftp prompt, it shows a list of all commands that can be used from ftp prompt. If a command is mentioned for help, it shows one line help explaining its significance. toggle metacharacter expansion of local file names. If put on, one can use * , ? etc characters to specify portion / all of the file name.

help [<command>]

glob

rlogin
Syntax : rlogin <rhost> [-l <username>]
Logs into remote host <rhost> as user <username> with/without any password. If the users are same on both remote and local server, then option <username> is not required. Prompting for password depends on the entries in file : /etc/hosts.equiv or .rhosts under remote users home directory of the remote server

rcp
Copies file(s)/directory(ies) from one server to another Copy Single File
rcp [-p] <srcfile1> <rem_user> @<remserver>:<pathname>/<dest_file>

rcp [-p] <srcfile1> <remuser>@<remserver>:<dest_dir> Copy Multiple Files


rcp [-p] <srcfile1> [<file2>]... <remuser>@<remserver>:<dest_dir>

Copy One or More Directory Subtrees


rcp [-p] -r <srcdir1> [<dir2>]... <remuser>@<remserver>:<dest_dir>

Copy Files and Directory Subtrees


rcp [-p] -r file_or_dir1 [file_or_dir2]... <remuser>@<remserver>:<dest_dir>

Copy files from server1 to server2 by server3


rcp <user1>@<server1>:<pathname>/<file> <user2>@<server2>:<pathname>[/<file>]

Shell Programming

List of instructions written within a file. Programming logics can be built in. Executes a set of related instructions, saving time,resource and imparts better control. The file has to be executable to make the thing work.

Invoking Sub-Shell manually


When a script file with execute permission starts running, it runs in a sub-shell under the current shell.

One can invoke a sub-shell manually to execute the scripts :$ sh prog1.sh Or $ sh < prog1.sh where prog1.sh is the name of the program. It may not have execute permission at this point of time.

Execute a script in the current shell


Syntax :

. <progname>

Whenever a shell script is executed, it runs in a sub-shell under the current shell. To make the script run in the current shell, one have to trigger the program as : . <progname>. The program need not have execute permission before executing in this fashion.

Specifying the type of shell

There are different kind of shells with different utilities and syntaxes. A shell script running fine in Korn shell, may not run ok in Bourne shell. To specify the interpreter shell, one can execute the program from OS prompt as :$ ksh < prog1.ksh This will execute program prog1.ksh using Korn shell as the interpretor. It is always a better approach to specify the shell at the first line of the program:#!/bin/ksh

Reading from Keyboard


The most common practice of any programming is to take input from user at runtime and act on that. Echo and read statement in shell scripts help us to achieve that.

$ cat prog1.sh Echo Enter name:\c read p_name Echo Hello $p_name

$ prog1.sh Enter name: Suman Hello Suman

Defining own variables


Defining global and local variables for programming is an indispensable practice. One can declare it at the top of the program /anywhere at the time of usage. Example g_val=20 g_name=two words Significance Declares a variable : g_val and assigns a value 20 to it. Declares a variable : g_name and assigns a value two words to it.Place the value to be assigned within quotes if contains more than one words

Example g_val= g_val= g_val= let g_cnt=20 g_cnt=20 readonly g_val unset g_cnt

Significance Assigns null value to the variable g_val

Declares a variable : g_cnt and assigns a value 20 to it. Makes the value in the variable : g_cnt readonly. Further change in the value is not allowed. Wipes off the value from variable : g_val.

Special parameters used by Shell


Shell uses some special parameters which imparts us different information on a program/command , various arguments passed to it, success/failure of the program etc.
Parameters $0 $1,$2. $* $# $? Significance Name of executed program/command First, second etc arguments passed to a program Complete set of positional parameters as a single string Number of arguments passed in command line Exit status of last command

Parameters $! $$ $_

Significance PID of last background job PID of the current shell Argument passed to the last command

Set Manipulating Positional Parameters


The command : set can be used to manipulate different positional parameters($1,$2) from a string / output of a command $ set Happy birthday to you $ $echo $1 $2 $ Happy birthday $ set `date` $ $echo $1 $3 $2 $6 $ Fri 19 Aug 2003

Shift- Shifting Positional Parameters


At a time, one can have values for 9 positional parameters( $1 to $9). To shift their values one place to the left, this command is used. $ set I know this is not a valid issue to discuss at the meeting before the board of directors $echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $ I know this is not a valid issue to $ shift; echo $1 $2 $3 $4 $5 $6 $7 $8 $9 $ know this is not a valid issue to discuss

Performing Calculations
Expr, echo let and (( )) comes handy cnt_val=`expr $cnt_val + 1` cnt_val=`echo $cnt_val + 1|bc` let cnt_val=cnt_val + max_limit ((z=x+y+2)) cnt=`expr $a \* 2` a=`expr $rt / 2` per=`expr \( $m1 + $m2 \) / 5`

echo
Displays strings , values in terminal or stores them in files.
Examples echo Hello dear Significance String : Hello dear shown in terminal

echo hello `hostname` String : Hello sb1dtl02 shown in terminal a=256; echo $a string=happy; echo $string>del Echo $string>>del The value of variable a is displayed in terminal Creates a file called : del or overwrites an existing file , the content of the file being : happy Appends the contents o the variable $string at the end of the file : del

Escape sequences used with echo to format output in terminal


Sequence Significance \033[0m Normal display \033[1m Bold display \033[4m Underlined display \033[5m Blinking display \033[7m Display in reverse video \07 \b \t \r \c Beep Backspace Tab Carriage return Positioning the cursor after the statement

tput
Syntax : tput [<options>] Formats display
Options clear cup r c bold blink rev smul rmul bel lines ed el Significance Clears the screen Moves cursor to row r and column c Bold display Blinking display Reverse display Starts mode underline Ends mode underline Bells the terminal Echoes no. of lines in the screen Clear to end of display Clear to end of line

Decision making If
Making decisions based on conditions is the most popular practice in every programming language. Shell provides decision building using If statements.
If <conditions> then <actions> fi If <conditions> then <actions> else <actions> fi If <conditions> then <actions> elif <conditions> then <actions> else <actions> fi

echo "Enter a value:\c" read val if test $val -gt 100 then echo Value $val greater than 100 elif test $val -ge 50 -a $val -lt 100 then echo Between 50 and 100 elif test $val -gt 0 -a $val -lt 50 then echo Between 50 and 100 else echo No comments fi

Test
Used to compare numerical values,strings,find properties of different files etc.
Numericl test operators Significance -gt -lt -eq(=) -ne(!=) -ge -le Greater than Lesser than Equal to Not equal to Greater than or equal to Lesser than or equal to

String test operators string1 = string2 string1 != string2 -n string -z string

Significance True if string1 is same as string2 True if string1 is not equal to string2 True if string length >0 True if string length = 0

while [ -z "$userid" ] do tput cup 5 5 echo "Enter userid:\c" read userid done if test $userid = super then tput cup 11 5 echo No such userid allowed exit fi while [ -z "$pswd" ] do tput cup 7 5 echo "Enter password:\c" stty -echo read pswd stty echo done

while [ -z "$npswd" ] do tput cup 9 5 echo "Confirm password:\c" stty -echo read npswd stty echo done if test $pswd = $npswd then tput cup 11 5 echo o.k else echo wrong entry.... fi

And,Or
Operators -a -o Significance and or echo "Enter city and state:\c"

E x a m p l e

read city state if test $city = cal -a $state = wb then echo You stay in Calcutta and in West Bengal elif test $city != cal -a $state = wb then echo You stay in West Bengal, not in Calcutta elif test $state != wb -a \( $city = bom -o $city = del \) then echo you stay in other metros fi

Case
Used to build logic depending on different values of same object. Syntax
case <variable> in <value1>) <actions> ;; <value2>) <actions> ;; *) <actions> esac

echo "Enter an animal:\c" read animal case "$animal" in [Tt]iger|[Ll]ion) echo He is the king;; Crow|Sparrow|parrot) echo They are birds;; [0-9]*) echo Name cannot start with numbers;; ?) echo You entered a single alphabet only;; ??) echo You entered two alphabets only;; [hH]ip*m*) echo Is it hippo?;; *) echo Correct entry , but could not be identified esac

Loop processing

Three common loops used in shell scripts are :1. While 2. Until 3. For

While
Syntax
while <condition is true> do <actions> done while [ -z "$userid" ] do tput cup 5 5 echo "Enter userid:\c" read userid done

Until
Syntax
until <control does not return true> do <actions> done echo "Enter number:\c" read no until [ $no -gt 10 ] do echo $no ((no=no+1)) done # till $no is not greater than 10

For
Syntax
for <variable> in <value1> <value2> <value3> . do <actions> done

for city in bombay delhi calcutta do echo $city is a metro city done

Loop breaking statements


Statement break continue Significance Control comes out of the loop completely Does not proceed with current record further and starts processing the next record from the start of the loop n=0 while test $n -le 10 do ((n=n+1)) if test $n -eq 5 then continue fi echo $n>>del done

n=20 while [ $n -le 100 ] do echo $n ((n=n+1)) if test $n -eq 30 then break fi done

Sleep
Syntax: sleep < number of seconds> Idly waits for number of seconds specified. Mainly used to display an event clearly to user.

Basename
Extracts the base filename from an absolute pathname Example:$ basename /home/ems2000/shell_scripts/ppc1.sh ppc1.sh When used with second argument, it strips off the second argument from the first Example:$ basename ppc1.sh .sh ppc1

The here document (<<)


Shell offers a mechanism of data to be read by a shell script to be read from itself rather than from another file. The here operator(<<) comes handy.

E x a m p l e

echo "Enter city code:\c" read city_code grep "^$city_code" << stcity 01 Calcutta 02 Bombay stcity if [ $? -ne 0 ] ; then echo invalid code fi

Putting all the inputs at a time


A program may ask for different inputs several times and at several points of execution.Instead of putting the inputs when they are asked for by the program, one can put all of them at a time while calling the program for execution.

echo First name:\c" read fname echo Middle name:\c" read mname echo Last name:\c" read lname echo your full name is $fname $mname $lname

$ prog8.sh<<end > soma > sen > gupta > end First name:Middle name:Last name:your full name is soma sen gupta

Set -- : Help Command Substitution


Command set is often required with command substitution. If the output of the command executed begins with -( as with ls l), set interprets it as one of its options ,thus giving error messages. By default, set displays all environment variables. This creates problem when the argument in its command line evaluates to null string ( for eg., in set `grep <srchstring> <filename(s)> ` statements. IF the pattern cannot be located, set will operate with no arguments and will display all the environment variables on the terminal.

To avoid this, use : set -- `command line` option.

echo "Enter city code:\c" read city set --`grep "^$city" <<tillend 01 Calcutta India WestBengal 02 Mumbai India Maharashtra tillend` if [ $? -eq 0 ] ; then echo "\n\tCity code : $1" echo "\n\tCity name : $2" echo "\n\tCountry : $3" echo "\n\tState else echo Invalid code fi : $4"

Command grouping
Group Significance () Executes the commands placed inside ( and ) separated by ; in a sub-shell under the current shell. They have the same PID Same as above; but uses current shell only. The closing curly brace must be on a separate line. To have both the braces on the same line, terminate the last command with a ; $pwd /home/ems2000 $ { cd scripts;pwd; } /home/ems2000/scripts $pwd /home/ems2000/scripts

{ }

$pwd /home/ems2000 $ ( cd scripts;pwd ) /home/ems2000/scripts $pwd /home/ems2000

Conditional Execution
Group cmd1 && cmd2 cmd1 || cmd2 cmd1 || cmd2 && cmd3 Significance Command cmd2 is executed only when command cmd1 succeeds Command cmd2 is executed only when command cmd1 fails Metacharacter coming first gets higher priority

$grep ems2000 prog.sh && echo String found String found $ grep ems99876 prog.sh || echo String not found String not found

String Handling
Syntax $ expr <string> : .* $ expr <string> : \(..\) $expr <string> : [^d]*<char> Significance Finds the length of the string : <string> Extracts the fourth and fifth character from the string Locates position of character <char> in the string <string> Significance Finds the length of the string : Training Extracts the fourth and fifth character from the string Locates position of character l within string : Anamoly

Example $ expr Training : .* 8 $ expr Subhendu : \(..\) he $expr Anamoly : [^d]*l 6

Conditional Parameter Substitution


Syntax : {<variable> : <option> <stg>} Option Significance s + Variable <var> evaluates to <stg> if <var> is defined and a nonnull string is assigned to it. dir1=`ls` echo ${dir1:+This directory is not empty} Variable <var> is evaluated to <stg> if it is undefined or assigned a null string Echo Enter directory to be copied:\c read dirto dir_child=${dirto:-/home/ems2000/scripts} IF dirto is null or not set, dir_child=/home/ems2000/scripts. The value of dirto is still null

Options Significance
= If the variable <var> is null, it is assigned the string <stg>. echo Enter filename:\c read flname grep $pattern ${flname:=emp.lst} IF flname is null, flname=emp.lst Evaluates the parameter if the variable is assigned and nonnull, otherwise it echoes the string following it. The shell is also terminated. This option is useful in terminating a script if the user fails to respond properly to shell directives. echo Enter pattern:\c read pattern ${pattern:? No pattern}

Shell variables($1,$2 etc) can also be used instead of <var>

Merging Streams
Usually, the inputs required for a program are typed in by user in terminal, all the outputs in terms of messages and all the error messages ( shell genarated) are shown in the terminal . But, one can make a program take inputs from a file, store outputs(messages) and / or error messages in a file, instead of displaying them in a terminal. Symbol 0 1 2 Significance Standard input Standard output Standard error

Example $ prog2.sh > sclist

Significance All the output messages and prompts will be taken into file : sclist. However, the inputs to the program will be received from terminal.

$ cat prog2.sh echo "Enter pattern:\c" read pattern echo \nPattern is $pattern"

$ cat outlist Enter pattern: Pattern is Patternfromterminal $

$ prog2.sh > outlist Patternfromterminal $

# Enter pressed # written from terminal

Example $ prog1.sh < namelist $ cat prog1.sh

Significance All the inputs prompted by program prog1.sh are taken from file : namelist

echo "Enter first name:\c" read fname echo \nEnter last name:\c" read lname echo \nWelcome $fname $lname" $ prog1.sh <namelist Enter first name: Enter last name: Welcome Suman Das

$ cat namelist Suman Das

Example $ prog1.sh < namelist > sclist

Significance All the inputs prompted by program prog1.sh are taken from file : namelist and all the output messages are stored in file : sclist $ prog1.sh <namelist >sclist $ cat sclist Enter first name: Enter last name: Welcome Suman Das

$ cat prog1.sh echo "Enter first name:\c" read fname echo \nEnter last name:\c" read lname echo \nWelcome $fname $lname"

$ cat namelist Suman Das

Example $ 1>&2

Significance Send the standard output to the destination of standard error


$prog2.sh > out.1 Enter city:calcutta More(y/n):y Enter city:bombay More(y/n):n

$ cat prog2.sh ans=y while [ "$ans" = "y" -o "$ans" = "Y" ] do echo "\n Enter city:\c" 1>&2 read city echo "$city" echo "\n More(y/n):\c" 1>&2 read ans done

$cat outlist calcutta bombay

Example $ 2>&1

Significance Send the errors to the destination of standard output

$ cat prog3.sh echo "Searching all the .kkl files" ls -ltr *.kkl

$cat out.2 Searching all the .kkl files *.kkl not found

$ prog3.sh Searching all the .kkl files *.kkl not found

$ prog3.sh>out.2 2>&1 $

$prog3.sh > out.2 *.kkl not found

$cat out.2 Searching all the .kkl files

Using subroutines
Using subroutines is one of the most popular practice in any programming language. Codes in a subroutine does a piece of job. It can take inputs from an user, do some jobs and can return some value to the calling program.

Subroutines are used to: Modularize program sections. Perform repeatitive jobs. Do Calculations.

Syntax:<subroutine_name> { <actions> } function <function_name> { <actions> } Inputs to a subroutine are identified within the subroutine as : $1,$2,$3 etc. A subroutine is called from a program as :<subroutine_name> [ <value1>] [ <value2> ] Subroutines are always declared at the top of a program

Passing values to subroutines


$ cat prog0.sh show_name() { case "$1" in [bB]) tput bold echo "$2";; [lL]) tput rev echo "$2";; *) tput rmso echo "$2" esac } show_name b `whoami` show_name l `hostname` show_name n `uname -r` show_name n The End

Subroutine returning values


$ incr_val() { sum=`expr $1 + 1` echo $sum } no=12 no=`incr_val $no` echo value is $no

$ prog2.sh value is 13

$ function show_name { grep "$1" *.ksh if [ $? -eq 0 ] ; then return 1 else return 2 Fi } echo "Enter pattern:\c" read pattern show_name "$pattern" if [ $? -eq 1 ] ; then echo "Pattern found..." else echo "Not found" fi

You might also like