You are on page 1of 41

UNIX Commands

To Create a File:
Syntax: $ cat [option] > <filename>
Ex: $ cat > file1
Hello..
This is my first File
Have a Nice Day
Bye
Ctrl+d (Save)
Ctrl+d Possible Completer
Ctrl+c Cancel foreground Job
Ctrl+z Stop (interrupted) a foreground Job
To Vie a already exi!tin" File:
Syntax: $ cat <filename>
Ex: $ cat file1
To append data to an exi!tin" file
Syntax: $ cat >> <filename>
Ex: $ cat >> file1
To Create a #$ltiple file it% %elp of cat command
Syntax: $ cat <filename& filename' (((filename (n) >
Ex: $ cat >f1 >f2 >f3 >f4
Hello....
This is file !m"er f4
#e create m!lti$le files
%& '(ave)
)i!play t%e record n$m*er to t%e partic$lar file
Syntax: $ cat [option] <filename>
Ex: $ cat * file1
To i"nore t%e *lan+ ,ecord!
Syntax: $ cat [option] <filename>
Ex: $ cat *" file1
K V Subba Raju, Asst. Professor
&
Create a -idden file
Syntax: $ cat [option] <filename>
Ex: $ cat >.file1
Create a )irectory
Syntax: $ m+dir [option] <)irectory name>
Ex: $m+&ir ra,!
Create #$ltiple )irectorie!
Syntax: $ m+dir [option] <)irectory name!>
Ex: $ m+&ir &1 &2 &3 &4 &-
Create #$lti . level )irectorie!
Syntax: $ m+dir [option] <)irectory name!>
Ex: $ m+&ir .$ /&-/&-1/&-2/&-3
Create #$ltiple S$* directorie!
Syntax: $ m+dir [option] <)irectory name!>
Ex: $ m+&ir .$ &3/&31 &4/&41
To )i!play pre!ent or+in" )irectory
Syntax: $ pd
Ex: $ $0&
To C%an"e t%e )irectory
Syntax: $ cd <)irectory name>
Ex: $ c& ra,!
To c%an"e t%e directory forard
Syntax: $ cd((
Ex: $ c&..
To #ove parent )irectory (root)
Syntax: $ cd /
Ex: $ c& /
To Create a -idden )irectory
Syntax: $ m+dir [option] <)irectory name>
Ex: $ m+&ir .ve+at
K V Subba Raju, Asst. Professor
'
,0#1V0 C1##23)S:
To ,emove a file
Syntax4 $ rm [option] <filename>
Ex: $ rm +ira
To ,emove #$ltiple File!
Syntax: $ rm [option] <filename! >
Ex: $ rm f1 f2 f3 f4
To remove file! Forci*ly
Syntax: $ rm [option] <filename>
Ex: $ rm *f +ira
To ,emove 5nteractive #ode
Syntax: $ rm [option] <filename>
Ex: $ rm *i +ira
To ,emove )irectory
Syntax: $ rm [option] <)irectory 3ame>
Ex: $ rm sa1ar
To ,emove all )irectorie! and !$*directorie!
Syntax: $ rm [option] <)irectory 3ame>
Ex: $ rm *r sa1ar
To ,emove )irectorie! Forci*ly
Syntax4 $ rm [option] <)irectory 3ame>
Ex: $ rm *rf sa1ar
Ex: $ rm *rf sa1ar $ava amar
To ,emove )irectorie! 5nteractively
Syntax4 $ rm [option] <)irectory 3ame>
Ex: $ rm *i sa1ar
Ex: $ rm *i sa1ar $ava amar
K V Subba Raju, Asst. Professor
6
75ST C1##23)S
$ ls : 2t is a comma& to list the files a& &irectories i
the $reset 0or+i1 Directory
$ ls - a : 2t is a comma& to &is$lay all files a&
Directories icl!&i1 hi&&e files a& Directories
$ls * : 3ist iformatio a"o!t the Files 'the c!rret
&irectory "y &efa!lt). (ort etries al$ha"etically
$ls ~ : 2t list the all Bac+!$ files
$ls @ : 2t list the all li+e& files a& Directories
$ls -d : 2t Dis$lays the $reset 0or+i1 Directory.
$ls -i : 2t Dis$lays the io&e !m"ers of files a&
Directories
$ls -s : 2t Dis$lays the si4es i "loc+s 'Files 5
Directories)
$ls -l : 2t Dis$lays the lo1 listi1 files a&
&irectories i $reset 0or+i1 &irectory
Listing directory contents:
$ ls list a &irectory
$ ls -l list a &irectory i lo1 '&etaile&) format
for exam$le:
$ ls -l
&r0xr*xr*x 4 vi,ay !ser 1624 7! 18 69:46 #:2T;<N=E:;N2N>(
*r0*r**r** 1 +ira !ser ?@?392 7! @ 14:28 scali".tar.14
% % % % % % % % % % %
A A A A A A A A A A A
A A A A A o0er 1ro!$ si4e &ate time ame
A A A A !m"er of li+s to file or &irectory cotets
A A A $ermissios for 0orl&'others)
A A $ermissios for mem"ers of 1ro!$
A $ermissios for o0er of file: r B rea&C 0 B 0riteC x B exec!te *Bo
$ermissio
K V Subba Raju, Asst. Professor
8
ty$e of file: * B ormal fileC &B&irectoryC l B sym"olic
li+C a& others...
ls -ld * 3ist all the file a& &irectory ames i the
c!rret &irectory !si1 lo1 format. #itho!t the D&D o$tioC
ls 0o!l& list the cotets of ay s!"*&irectory of the
c!rret. #ith the D&D o$tioC ls ,!st lists them li+e re1!lar
files.
$ ls -al : 2t Dis$lays icl!&i1 hi&&e a& lo1
listi1 files a& Directories
$ ls -m : 2t Dis$lays all files a& Directories 0ith
se$arate& "y comma 'C)
$ ls -ls : 2t Dis$lays all lo1 listi1 Directories
$ ls --full-time : 2t Dis$lays files a& Directories
0ith total iformatio &ate a& time
$ ls -nl : 2t Dis$lays the lo1 listi1 files a&
Directories accor&i1 to mo&ificatio Time
$ ls -rtl : 2t Dis$lays the file a& Directories
0ith reverse or&er
$ ls -R : 2t Dis$lays the all files a& Directories
;e1ressively 'or&er "y or&er)
$ ls -l : 2t Dis$lays the files a& Directories i a
si1le col!m 'vertical)
$ ls -x : 2t Dis$lays the files a& Directories 0ith
m!lti$le col!ms
COPY COMMANDS
Syntax: $cp [option] <!o$rce file> <)e!tination file>
&( File to a File
'( File to a )ictionary
6( )irectory to )irectory
File to File
K V Subba Raju, Asst. Professor
9
Syntax : $cp <!o$rce file> <)e!tination file>
Ex : $c$ ve+at amar
..... Ne0 file
Ex : $c$ ve+at ,oh
Existi1 file
3ote: T%e content of :o%n file overrite it% ven+at
Ex E $c$ *f ve+at +!mar
3ote : 5t i! copy t%e file forci*ly ven+at to +$mar it%o$t any permi!!ion
Ex : $c$ *i ve+at 1a&hi
3ote : 5t i! copy t%e file interactive mode ven+at to +$mar it% permi!!ion!
2. File to Directory
Syntax 4 $ cp <!o$rce file> <)e!tination )irectory>
Ex E $ c$ ve+at a,ay
.... a,ay is Directory
Ex : $ c$ *" ve+at satya
3ote : 5t copy! t%e ven+at file to Satya )irectory it% *ac+$p
Ex : $ c$ *"f ve+at +ira
3ote 4 5t copy! t%e ven+at file to ;iran )irectory it% forci*ly *ac+$p
Ex : $ c$ *i"f ve+at y!s!f
3ote : 5t copy! t%e ven+at file to <$!$f )irectory it% *ac+$p interactively and forci*ly
Ex : $ c$ file1 file2 file3 Directory
3ote : 5t copie! t%e no of file to )irectory
3. Directory to Directory
Syntax : $ cp <So$rce )irectory> <)e!tination )irectory>
Ex : $ c$ Dravi& >a1!ly
Ex : $ c$ *r Fo&y Ge+at
3ote : 5t copie! t%e )irectory to )irectory (incl$din" all file! and )irectorie!)
Ex : $ c$ *ir HDirectory 1> HDirectory 2>
K V Subba Raju, Asst. Professor
=
3ote : 5t copie! t%e )irectory to )irectory (incl$din" it% all file! and directorie! it%
interactive mode)
Ex : $ c$ *rf HDir 1 > HDir 2> HDir 3> HDestiatio
Directory>
3ote : 5t copie! t%e m$ltiple )irectorie! it% forci*ly and rece!!ively mode
MOVE COMMANDS
T%i! command i! $!ed to move t%e file! and directorie! one place to anot%er place
Syntax: $ mv [option] <So$rce file/)irectory> <Tar"et file/)irectory>
T%e!e are *a!ically 6 type!
&( File to File
'( File to )irectory
6( )irectory to )irectory
1. File to File
Syntax: $ mv [option] <So$rce file> <Tar"et file>
Ex: $ mv file1 file2
Ex: $mv *" file1 file2
3ote: T%e file& move to file' it% *ac+$p
Ex: $ mv *f file1 file2
3ote: T%e file& move to file' it% forci*ly
Ex: $ mv *if file1 file2
3ote: T%e file& move to file' it% interactive and forci*ly mode
2. File to Directory
Syntax: $ mv [option] <So$rce file> <Tar"et )irectory>
Ex: $ mv file1 Dir1
3ote: T%e file& move! to )irectory ()ir&)
Ex: $ mv *" file1 Dir1
3ote: T%e file& move to )ir& it% *ac+$p mode
Ex: $mv *f file1 Dir1
3ote: T%e file& move to )ir& it% forci*ly mode
K V Subba Raju, Asst. Professor
>
Ex: $mv *if file1 Dir1
3ote: T%e file& move to )ir& it% interactive and forci*ly mode(
3. Directory to Directory
Syntax: $ mv [option] <So$rce )irectory> <Tar"et )irectory>
Ex: $ mv Dir1 Dir2
3ote: T%e content of )ir& move! to )ir'
Ex: $ mv *" Dir1 Dir2
3ote: T%e content! of )ir& move to )ir' it% *ac+$p
Ex: $ mv *f Dir1 Dir2
3ote: T%e content! of )ir& move! to )ir' it% forci*ly
Ex: $ mv *if Dir1 Dir2
3ote: T%e content! of )ir& move! to )ir' it% interactive and forci*ly mode(
PATH IN UNIX
&( 2*!ol$te pat%
'( ,elative pat%
&( 2*!ol$te pat%: 5t i! an independent on pre!ent or+in" directory
'( ,elative pat%: 5t i! a pat% mention from root directory
Absolute at! "#amle:
Ex: $ c& /"1/"2/"3/"4/"-
: $ c$ /!sr/$aramesh/a1/a2/a3/file HDirectory>
$elati%e &at! "#amle:
Ex: $c& /"1/"2/"3/"4/"-
: $mv ../../../../../c1/c2/c3/c4/c-
FILE ACCESS PERMISSIONS (FAP)
?rx??????
drxr??r??
?rx????
K V Subba Raju, Asst. Professor
@
d ? )irectory
? Arire
r ? ,ead
x ? 0xec$ta*le
3ote: Ait%o$t rite permi!!ion! e canBt copyC removeC modifyC moveC crate
drxrxrx
> > >
)irectory . >>>
File . ===
)efa$lt )irectory Dermi!!ion! . >99
)efa$lt File Dermi!!ion! . =88
,ead . 8
Arite . '
0xec$ta*le . &
Total Dermi!!ion! >
TO CHANGE AND MODIFY THE PERMISSIONS
&( 3$meric #et%od
'( Sym*olic #et%od
1. Numeric 'et!od:
Syntax: $ c%mod [permi!!ion!] <file/)irectory>
Ex: $ chmo& ?66 Dir2
$ chmo& @46 file1
To c%ec+ t%e c%an"ed permi!!ion $!e t%e command: l! ? l
To c%an"e t%e permi!!ion! and c%ec+ t%e permi!!ion $!in" folloin" command
Ex: $ chmo& *c ?-6 Dir2
To c%an"e t%e permi!!ion it% )irectorie! and !$*directorie! it% re"re!!ively
Ex: $ chmo& *; ?-- Dir2
To C%an"e t%e pre!ent or+in" )irectory Dermi!!ion!
Ex: $ chmo& * ; ?-6
3ote : DlE donBtF try t%i! command on t%e !y!tem
K V Subba Raju, Asst. Professor
G
2. (ymbolic 'et!od
H!er . $
Iro$p . "
1t%er! . o
2ll . a
,ead . r
Arite .
0xec$te . x
Syntax: $ c%mod [option!] <File/)irectory>
To 2dd Arite permi!!ion to Iro$p and 1t%er!
Ex: $ chmo& 1I0C oI0 Dir1
To ,emove t%e Arite Dermi!!ion to H!er
Ex: $ chmo& !*0 Dir1
To ,emove t%e Arite and exec$te permi!!ion! to Iro$p and 1t%er!
Ex: $chmo& 1*rC 1*xC o*rC o*x Dir1
2ppend t%e Arite permi!!ion! to all
Ex: $chmo& !I0C 1I0C oI0 Dir1
'or)
$chmo& aI0 Dir1
H#2S; (H!er File Creation #a!+)
? c$tin"C removeC %idden
Jy )efa$lt $ma!+ val$e i! K''
)irectory File
>>> ===
K'' K''
>99 =88
)ILD CA$D(
L * Carrot or Cap
M ? Sin"le C%aracter
N ? #$ltiple C%aracter!
[ ] ? ,an"e of C%aracter!
Ex: ls J * This comma& is !se& list 0ith si1le character
files
K V Subba Raju, Asst. Professor
&K
ls JJ * This comma& is !se& to list 0ith &o!"le character
files
ls J K6*9L . This comma& is !se& to list the files starti1
0ith al$ha"etically a& 0ith !meric
ls M * This comma& is !se& to &is$lay the all the files a&
&irectories a& files (!"&irectories
ls aM * This comma& is !se& to &is$lay the files 0ith
starti1 letter 0ith NaN Oharacter
Remove Commands
Syntax: rm [option] <file/directory name>
Ex: rm M * This comma& is !se& to &elete all the files i
c!rret &irectories
Note: $l4 &oNt !se this comma& 0itho!t $ermissio
Ex: rm J * To &elete si1le character files
Ex: rm JJ * To &elete &o!"le character files
Ex: rm aM * To &elete the files starti1 0ith
character NaN
Ln!s
Call t%e lin+ f$nction to create a lin+ to a file(
Call t%e lin+ f$nction to create a lin+ named F570' to an exi!tin" F570&
&( -ard lin+
'( Soft 7in+
&( +ard Lin, : -ard lin+ can *e *$ild !in"le file !y!tem
-ard lin+ can reco"niEed it lin+! !ame permi!!ion! and !ame !iEeC !ame inon
n$m*er
5f t%e data i! !o$rce lo!! e "ot from %ard lin+
inon n$m*er i! "iven !pace allocated
inon n$m*er! can reco"niEed different lo"ical name!
Syntax : ln <!o$rce file> <tar"et file>
Ex : l *f va+at +!mar ' f . forci"ly)
Ex : l *i ve+at +!mar 'i . iteractively)
K V Subba Raju, Asst. Professor
&&
'( (o-t Lin, : 5t can *e *$ilt acro!! t%e file !y!tem
5t t%e !o$rce file i! delete e canFt retrieve from tar"et file
Syntax : ln ?! <!o$rce file> <tar"et file>
Ex : l s& Dir1 Dir2 '& . Directories)
Ex : l *sf File1 File2 'f . forci"ly)
Ex : l *si File1 File2 's . io&e !m"er)
FIND COMMANDS
5t !earc% for file! in a directory %ierarc%y
Syntax: find [pat%(((] [expre!!ion]
/ ? root
O ? -ome )irectory
( ? Dre!ent or+in" directory
Ex: $ fi& /"i .ty$e f
2t searches the files a& &irectories a"sol!te $ath of the
root.
Ex: $ fi& P *ty$e &
2t searches the &irectories a& s!"&irectories i the Home
&irectory
Ex: $ fi& P *ty$e l
2t searches the li+e& files
Ex: $ fi& P *ty$e f *ame Qfile1R
2t searches the a"sol!te $ath of the fileame
Ex: $ fi& P *ty$e & *ame Qra,R
2t searches the a"sol!te $ath of the Directories
Ex: $ fi& P *ty$e f *ame Qfile1R exce catSTUE
2t searches the a"sol!te $ath of the fileame a& &is$lays
the cotets of file.
Ex: $ fi& P *ty$e f *ame Qfile1R exce cat STU exce rmSTUE
2t searches the a"sol!te $ath of the fileame a& &is$lays
the cotet of fileC a& removes at the same time.
Ex: $ fi& P *ty$e f *$erm @44
K V Subba Raju, Asst. Professor
&'
2t searches the file !&er 0ith @44 $ermissio
Ex: $ fi& P *ty$e f *$erm @44 *exce chmo& @46 STUE
2t searches the fileame !&er 0ith $ermissio @44 a& cha1e
the file $ermissios to @46.
Ex: $ fi& P *ty$e f si4e 6E
2t (earches the Vero "yte files
Linux: 5! b"tes bloc#
Ex: $ fi& P *ty$e f si4e 166c 'Exactly 166 "ytes file)
$ fi& P *ty$e f si4e I166c 'a"ove 166 "ytes file)
$ fi& P *ty$e f si4e *166c '"elo0 166 "ytes file)
Ex: $ fi& P *ty$e & si4e 8"
2t searches the 8"ytes Directories
Ex: $ fi& P *ty$e f *i !m Q1698R
2t searches the file 0ith io&e !m"er
Ex: $ fi& P ty$e & . i!m Q1624R
2t searches the &irectories 0ith io&e !m"er
$ime
?a . 2cce!!
?c . C%an"ed
?m . #odified
Ex: $ fi& P *ty$e f .a mi 36
2t searches the file "efore 36 mi access
Ex: $ fi& P *ty$e f .a time .//I
2t searches the file "efore fe0 &ays
V Ed"o# (Vs$a% Ed"o#)
Vi i! a text editor t%at i! $pard! compati*le to Vi( 5t can *e $!ed to edit all +ind! of plain
text( 5t i! e!pecially $!ef$l for editin" pro"ram!(
&( 0!c mode
'( 5n!ert mode
6( Colon mode
K V Subba Raju, Asst. Professor
&6
:C a
2C i
<C o
( #ith the hel$ of these +eys move esc mo&e to iert mo&e
(hift: 0ith the hel$ of shift: move to Esc mo&e to colo mo&e
Esc Fo&e Oomma&s + h l ,
By $ress
N3N * The c!rsor moves ri1ht &irectio
NhN * The c!rsor moves oe character left
N+N * The c!rsor moves oe character !$
N,N * The c!rsor moves oe character &o0
N0N * The c!rsor moves ext lie first
-0 * The c!rsor moves - 0or&s for0ar&
0 * The c!rsor moves 0or&s for0ar&
" * The c!rsor moves $revio!s 0or&
-" * The c!rsor moves - 0or&s "efore
" * The c!rsor moves 0or&s "efore
e * The c!rsor moves ext 0or& last character
$ * The c!rsor moves the e& of the recor&
% * The c!rsor moves the starti1 of the recor&
11 * The c!rsor moves the first recor&
1-11 * The c!rsor moves the 1-
th
recor& of the file
11 * The c!rsor moves the th recor& of the file
> * The c!rsor moves the last recor& of the file
-> * The c!rsor moves the -
th
recor& of the file
H * The c!rsor moves the starti1 recor& of the
file'0i&o0)
3 * The c!rsor moves the last recor& of the file
'0i&o0)
F * The c!rsor moves the mi&&le recor& of the
file'0i&o0)
Otrl I f * To scroll the $a1e for0ar&
Otrl I " * To scroll the $a1e "ac+0ar&
Otrl I & * To scroll the half $a1e for0ar&
Otrl I ! * To scroll the half $a1e "ac+0ar&
K V Subba Raju, Asst. Professor
&8
Esc Mode (Delete Command)
x * To &elete $reset c!rsor $ositio character
W * To &elete $revio!s character
&& * To &elete a recor&
&& * To &elete NN recor&s
&0 * To &elete a 0or&
-&0 * To &elete - 0or&s
&" * To &elete a $revio!s 0or&
-&" * To &elete a - $revio!s 0or&s
&" * To &elete a $revio!s 0or&s
&$ * To &elete $reset c!rsor to the e& of the recor&
&% * To &elete $reset c!rsor $ositio to starti1
of the recor&
.an,ing Commands /coying0
y * To ya+ a si1le character
y% * To ya+ $reset c!rsor $ositio to starti1 of
the recor&
y$ * To ya+ $reset c!rsor $ositio to e&i1 of
the recor&
&11 * To ya+ $reset c!rsor $ositio to starti1 of
the recor&
Ex : '1 . 26) if c!rsor i 16
th
$ositio
yy * To ya+ the recor& 'co$y)
-yy * To ya+ - recor&s $reset c!rsor $ositio
yy * To ya+ recor&s $reset c!rsor $ositio
11 * To ya+ $reset c!rsor $ositio to the first $ositio
y> * To ya+ $reset c!rsor $ositio to the last $ositio
y0 * To ya+ the 0or+
-y0 * To ya+ - 0or&s
y" * To ya+ $revio!s 0or&
-y" * To ya+ $revio!s - 0or&s
y" * To ya+ $revio!s 0or&s
&aste Commands
$ * Xaste the ri1ht si&e of the c!rsor $ositio
X * Xaste the left si&e of the c!rsor $ositio
$ * Xaste "elo0 the c!rsor $ositio
X * Xaste a"ove the c!rsor $ositio
K V Subba Raju, Asst. Professor
&9
$elace Commands
r * To re$lace $reset c!rsor $ositio character
; * To re$lace the etire recor&
cc * To recreate the ;ecor&
c0 * To re$lace the 0or&
cv * To re$lace the $revio!s 0or&
(ecial Commands
! * To !&o the $revio!s actio
. * To re&o the $revio!s actio
7 * To ,oi the recor&s
P * To cha1e to lo0ercase to !$$er case letter !$$er case
to lo0ercase letters
(earc! Command
/ <pattren> ? To !earc% for partic$lar pattern (ord)
Ex : /!ix
* c!rsor moves to the ext acc!race
N * c!rsor moves to the $revio!s acc!race
Insert 'ode Commands
: * :$$e& the letter e& of the lie
a * :$$e& the letter after the c!rsor
i * :$$e& the letter "efore the c!rsor
2 * :$$e& the letter "e1ii1 of the lie
o * o$e a lie "elo0 the c!rsor
< * o$e a lie a"ove the c!rsor
s * Delete the $reset c!rsor $ositio a& isert
the character
Colon 'ode Commands :
:0 * To save a file
:Y * To Y!it a file 0itho!t save
:0YZ * To save a& Y!it a file forci"ly
:se! * To &is$lay recor& !m"ers
:se o! * To remove &is$lay recor& !m"ers
:/H$atter> * To search to$ to "ottom
:JH$atter> * To search "ottom to to$
K V Subba Raju, Asst. Professor
&=
:se ic * 2t i1ores the cases a& search the recor&s
:se oic * 2t &isa"le $artic!lar a"ove actio
:se ai * 2t sets a e0 lie 'a!to i&et)
:se oai * 2t &isa"les $artic!lar actio
:&$ * To &elete a last recor&
:[& * To &elete all the recor&s i the file
:16C260 He0 fileame> * Oo$y to aother e0 file
:16C2-0Z Htar1et file> * Oo$y to tar1et file forci"ly
:2-C360 >> Hfileame> * :$$e& existi1 file
:16 co -6 * Oo$y 16
th
recor& after the -6
th
;ecor&
:16 mo -6 * Fove 16
th
recor& to -6
th
recor&
:-C1- mo -6 * Fove -C 1- recor&s after -6
th
recor&
:s/!ix/li!x * :ll recor&s re$lace
$vi Hfile1> Hfile2> Hfile3> * F!lti$le file E&itor
:Hfile3> * to s0itch file3
: * istea& of file1 to file3
:N * s0itchi1 from "ac+0ar& file
,edirection
Ex: $ cat H file1 >file2
2t re&irects the file1 to file2
Ex: $ cat > file1 Hfile2
2t re&irects the file2 to file1
Ex: $ cat >file1 Hfile2 file3
file2 file3 re&irects to file1
Ex: $ cat file1 file2 file3 2>>error
K V Subba Raju, Asst. Professor
&>
FILTERS
Filter i! a command or pro"ramC %ic% ta+e! t%e inp$t from t%e !tandard inp$t and t%en
proce!! data to "ive reP$ired o$tp$t
Simple Filter! 2dvance Filter!
Fore 1. 1re$
less 2. se&
$1 3. a0+
hea&
tail
0c
tee
tr
sort
c!t
!iY
1. 'ore : #ore i! a filter to di!play t%e pa"in" t%ro$"% text pa"e *y pa"e
Syntax: $ more <filename>
f ? To move next pa"e
* ? To move previo$! pa"e
Space*ar . To !croll t%e pa"e
0nter ;ey . To !croll t%e pa"e line *y line
P ? Q$it
Ex: $ cat 2mor4 A more
2. less : 7e!! i! a pro"ram !imilar to more (&)C *$t %ic% allo! *ac+ard movement in t%e
file a! ell a! forard movement( 2l!oC le!! doe! not %ave to read t%e entire inp$t file
*efore !tartin"C !o it% lar"e inp$t file! it !tart! $p fa!ter t%an text editor! li+e vi
Syntax: $ le!! <filename>
f ? To move next pa"e
* ? To move previo$! pa"e
Space*ar . To !croll t%e pa"e
0nter ;ey . To !croll t%e pa"e line *y line
P . Q$it
K V Subba Raju, Asst. Professor
&@
Ex: $ cat file1 A less
3ote: T%i! command or+! on 7in$x only
3. &g: 5t di!play! pa"e *y pa"e
Syntax: $ p" <filename>
f ? To move next pa"e
* ? To move previo$! pa"e
Space*ar . To !croll t%e pa"e
0nter ;ey . To !croll t%e pa"e line *y line
P . Q$it
Ex: cat ve+at A $1
3ote: T%i! command donFt or+! in 7in$x
1. !ead : 5t di!play! t%e fir!t &K line! of t%e file (pa"e) . defa$lt
Syntax: $ %ead <filename>
Ex: $ hea& *1- ve+at
Ex: $ cat Hfileame> A hea& 26 * 2t &is$lay first 26 lies
2. tail : 5t di!play! la!t &K record! of t%e file
Syntax: tail <filename>
: tail ?'K <filename>
Ex: cat Hfileame> A tail *26
Ex: tail I2- Hfileame> * 2t &is$lays last 2- recor&s
In&'e"(een #e)o#ds
,ecord! from &9 to 'K
Ex: $ hea& *16 Hfileame> A tail *- ';ecor&s 1- to 26)
Ex: $ tail I36 Hfileame> A hea& *- ';ecor&s 36 to 3-)
I* $ (an" "o #ed#e)" "o +e#manen"
Ex: $ hea& *26 Hfileame> Atail *- > file1 'file1 is e0file)
Ex: $ tail I36 Hfileame> Ahea& *- > file1 'file1 is e0file)
3. 4c : T%i! filter i! $!ed to ord co$nt
K V Subba Raju, Asst. Professor
&G
Syntax: $ c <filename>
Ex: $ 0c +ira
no o* #e)o#ds "o displa"
Ex: $ 0c *l Hfileame>
no, o* (o#ds "o ds+%a-
Ex: $ 0c *0 Hfileame>
no, o* ).a#a)"e#s "o ds+%a-
Ex: $ 0c *c Hfileame>
ma/ 0 %en1". 0 ).a#a)"e#s "o ds+%- n a *%e
Ex: $ 0c *3 Hfileame>
5. tee : 5t di!play! and redirect! t%e !ame time
read from !tandard inp$t and rite to !tandard o$tp$t and file!
Ex: $ ls *l Hfileame>
$ ls *l A tee Hfileame>
$ ls **f!ll*time A tee *a Hfileame> * a$$e&i1
existi1 file
3ote: it%o$t R (pipe !ym*ol) tee filter donBt or+!
6. tr : Tran!late c%aracter! *y c%aracter!
Tran!late or delete c%aracter!
Syntax: $ tr [option] <filename>
Ex: $ tr QMRC Q\R Hfileame
Ex: $ tr Q a"c&RC Qxy4R Hfileame
%uplicate c&aracters a'oid (it& se)uential order
Ex: $ tr *s QrR H fileame
%elete t&e c&aracters
Ex: $ tr *& QrR H fileame
7. (ort: !ort line! of text file! accordin" to t%e fir!t c%aracter
Syntax: !ort [option] <filename>
0x : !ort !t$dent
K V Subba Raju, Asst. Professor
'K
student -ilename
K& vi:ay =K GK
&' anil >K >9
6' !$:an =9 8K
K8 %ari >K 89
'' ra:$ @9 9K
Sortin" priority
&( Jlan+ !pace
'( Special c%aracter
6( 3$meric
8( Hpperca!e
9( 7oerca!e
*umeric:
0x: $ sort * Hfileame>
$ (ort *r Hfileame> * N!meric a& reverse
+f u (ant permanent
Ex: $ sort *r Hfileame> > e0 file
$ !ort ?t S:T ? field !eparator
,ccording to t&e second fields
$ (ort *&t Q:R *+2 * &irectory sorti1
$ ls *l A sort *" . +- * it ta+es the "la+ a"o!t Fiel&
se$arator
18. Cut: ,emove !ection! from eac% line of file!(
2, C.a#a)"e# )$""n1
Ex: $ c!t *c Hfileame>
First character of the all lies
Ex: $ c!t *c1 Hfileame>
To c!t the first Oharacter <f the all recor&s
K V Subba Raju, Asst. Professor
'&
Ex: $ c!t *c8 Hfileame>
To c!t the 8
th
character i each lie
Ex: $ c!t *c4C 8 Hfileame>
To c!t 4
th
a& 8
th
Oharacter 2 each lie
Ex: $ c!t *c4 *8 Hfileame>
To c!t 4
th
to 8
th
Oharacters ra1e
3, Fe%d )$""n1
?d field !eparator
Ex: $ c!t *& Q:R *f2 Hfileame>
O!t 2
&
fiel& i all recor&s
Ex: $ c!t *& Q:R *f2C - Hfileame>
O!t 2
&
a& -
th
recor&s
Ex: $ c!t *& Q:R *f2C *- Hfileame>
O!t 2
&
to -
th
recor&s
Ex: $ ls *l A tr *s Q Q
(eY!ece s$ace
Ex: $ ls *l A tr *s Q Q A c!t *& Q Q *f-
(eY!ece a& c!t
11. Uni9: ,emove d$plicate line! from a !orted file
Syntax: $ $niP [option] <filename>
Ex: $ !iY *& Hfileame >
Dis$lays oly the D!$licate recor&s
Ex: $ !iY *D Hfileame>
Xrit all &!$licate ;ecor&s a& &emeliti1 is &oe 0ith "la+
lies
Ex: $ !iY .c Hfileame>
Xrefix lies "y the N!m"er of occ!rreces
Ad%ance Filters
K V Subba Raju, Asst. Professor
''
1. :re
T%e "rep $tilitie! are a family of Hnix tool!C incl$din" "repC e"repC and f"repC t%at perform
repetitive !earc%in" ta!+!( T%e tool! in t%e "rep family are very !imilarC and all are $!ed for
!earc%in" t%e content! of file! for information t%at matc%e! partic$lar criteria( For mo!t
p$rpo!e!C yo$Fll ant to $!e f"repC !ince itF! "enerally t%e fa!te!t
T%e "eneral !yntax of t%e "rep command! i!:
Syntax: "rep [?option!] pattern [filename]
<o$ can $!e f"rep to find all t%e line! of a file t%at contain a partic$lar ord( For exampleC
to li!t all t%e line! of a file named my file in t%e c$rrent directory t%at contain t%e ord
Udo"UC enter at t%e Hnix prompt:
Ex: f1re$ &o1 myfile
T%i! ill al!o ret$rn line! %ere Udo"U i! em*edded in lar"er ord!C !$c% a! Udo"maU or
Udo""edU( <o$ can $!e t%e ? option it% t%e "rep command to ret$rn only line! %ere
Udo"U i! incl$ded a! a !eparate ord:
Ex: 1re$ *0 &o1 myfile
To !earc% for !everal ord! !eparated *y !pace!C enclo!e t%e %ole !earc% !trin" in P$ote!C
for example:
Ex: f1re$ D&o1 ame& Ohec+ersD myfile
T%e f"rep command i! ca!e !en!itive4 !pecifyin" Udo"U ill not matc% U)o"U or U)1IU(
<o$ can $!e t%e ?i option it% t%e "rep command to matc% *ot% $pper? and loerca!e
letter!:
Ex: 1re$ *i &o1 myfile
To li!t t%e line! of myfile t%at do not contain Udo"UC $!e t%e ?v option:
Ex: f1re$ *v &o1 myfile
5f yo$ ant to !earc% for line! t%at contain any of !everal different ord!C yo$ can create a
!econd file (named !econd file in t%e folloin" example) t%at contain! t%o!e ord!C and
t%en $!e t%e ?f option:
Ex: f1re$ *f seco& file my file
K V Subba Raju, Asst. Professor
'6
<o$ can al!o $!e ildcard! to in!tr$ct f"rep to !earc% any file! t%at matc% a partic$lar
pattern( For exampleC if yo$ anted to find line! containin" Udo"U in any of t%e file! in
yo$r directory it% name! *e"innin" it% UmyUC yo$ co$ld enter:
Ex: f1re$ &o1 myM
T%i! command o$ld !earc% file! it% name! !$c% a! my fileC my(%&C and my !t$ff in t%e
c$rrent directory( 0ac% line ret$rned o$ld *e prefaced it% t%e name of t%e file %ere t%e
matc% a! fo$nd(
Jy $!in" pipe! and/or redirectionC yo$ can $!e t%e o$tp$t from any of t%e!e command! it%
ot%er Hnix tool!C !$c% a! moreC !ortC and c$t( For exampleC to print t%e fift% ord of every
line of my file containin" Udo"UC !ort t%e ord! alp%a*eticallyC and t%en filter t%e o$tp$t
t%ro$"% t%e more command for ea!y readin"C yo$ o$ld enter at t%e Hnix prompt:

Ex: f1re$ &o1 myfile A c!t *f- *&D D A sort A more
5f yo$ ant to !ave t%e o$tp$t in a file in t%e c$rrent directory named ne fileC enter:
Ex: f1re$ &o1 my file A c!t *f- *&D D A sort > e0 file
$ 1re$ . Hfile> * To &is$lay recor& !m"er
$ 1re$ *iHfile> * To i1ore recor&
$ 1re$ .c Hfile> * To co!t i ho0 may recor&s ex$ressio
$ 1re$ .E Hfile> * To search for m!lti$le ex$ressio
$ 1re$ .3 Hfile> * 2t 1ive the file ame a& 0hich the
re1!lar ex$ressio
$ 1re$ .r Hfile> * To search re1ressively a& $reset 0or+i1
&irectory
$ 1re$ .0 Hfile> * To search for the exact match for the 0or&
$ 1re$ .s Hfile> * To s!$$ress errors
$ 1re$ Q,aiR Hfile> * 2t $rits the ex$ressive recor& 0hich
have 1ot the Q,aiR
$ 1re$ . Q,aiR Hfile> * 2t &is$lay the recor& !m"er a&
ex$ressio 0hat 0e 1ive Q,aiR
$ 1re$ .i Q,ayR Hfile> * 2t i1ore a& $rits the recor&
K V Subba Raju, Asst. Professor
'8
Q7ayR
$ 1re$ .E Q,ay $remR Hfile> * 2t search for m!lti$le
ex$ressios a& $rits the recor&
$ 1re$ .l Q,ayRM * 2t &is$lays the fileame 0hich the
Q7ayR ex$ressio is
2. ("D
Sed i! a !tream editor( 2 !tream editor i! $!ed to perform *a!ic text tran!formation! on an
inp$t !tream (a file or inp$t from a pipeline)( A%ile in !ome ay! !imilar to an editor
%ic% permit! !cripted edit! (!$c% a! ed)C !ed or+! *y ma+in" only one pa!! over t%e
inp$t(!)C and i! con!eP$ently more efficient( J$t it i! !edF! a*ility to filter text in a pipelineC
%ic% partic$larly di!tin"$i!%e! it from ot%er type! of editor!(
Sed or+! a! follo!: it read! from t%e !tandard inp$tC one line at a time( for eac% lineC it
exec$te! a !erie! of editin" command!C t%en t%e line i! ritten to ST)1HT( 2n example
t%at !%o! %o it or+!: e $!e t%e s command( s mean! U!$*!tit$teU or !earc% and
replace( T%e format i!
s/re1!lar*ex$ressio/re$lacemet text/Sfla1sT
Ae onFt di!c$!! all t%e fla"! yet( T%e one e $!e *elo i! 1C %ic% mean!C Ureplace all
matc%e!U
>cat file
2 have three &o1s a& t0o cats
>se& *e Ns/&o1/cat/1N *e Ns/cat/ele$hat/1N file
2 have three ele$hats a& t0o ele$hats
>
1;( So %at %appenedM Fir!tlyC sed read in t%e line of t%e file and exec$ted
s/&o1/cat/1
A%ic% prod$ced t%e folloin" text:
2 have three cats a& t0o cats
and t%en t%e !econd command a! performed on t%e edited line and t%e re!$lt a!
2 have three ele$hats a& t0o ele$hats
K V Subba Raju, Asst. Professor
'9
Ae act$ally %ave a name for t%e Uc$rrent textU: it i! called t%e pattern space( So a preci!e
definition of %at sed doe! i! a! follo!:
sed reads t&e standard input into t&e pattern space- performs a se)uence of editing
commands on t&e pattern space- t&en (rites t&e pattern space to S$%./$0
Syntax: !ed [1DT513]((( VScript?only?if?no?ot%er?!criptW [5np$t?file](((
1,
Syntax: !ed [option] Xaddre!!/actionB <file>
1ption!: ?n To !$ppre!! inp$t/o$tp$t
?e #$ltiple 0xpre!!ion!
?d )elete
?p Drint
?! S$*!tit$te
Fir!tlyC t%e ay yo$ $!$ally $!e sed i! a! follo!:
>se& *e Ncomma&1N *e Ncomma&2N *e Ncomma&3N file
>Sshell comma&TAse& *e Ncomma&1N *e Ncomma&2N
>se& *f se&scri$t.se& file
>Sshell comma&TAse& *f se&scri$t.se&
!o sed can read from a file or ST)53C and t%e command! can *e !pecified in a file or on t%e
command line( 3ote t%e folloin":
T%at if t%e command! are read from a fileC trailin" %ite !pace can *e fatalC in
partic$larC it ill ca$!e !cript! to fail for no apparent rea!on( 5 recommend editin"
!ed !cript! it% an editor !$c% a! vimC %ic% can !%o end of line c%aracter! !o t%at
yo$ can U!eeU trailin" %ite !pace at t%e end of line(
Substitute
T%e format for t%e !$*!tit$te command i! a! follo!:
[address[ Caddress!]]s1pattern1replacement12flags3
T%e fla"! can *e any of t%e folloin"
n replace nt% in!tance of pattern it% replacement
p rite pattern !pace to ST)1HT if a !$cce!!f$l !$*!tit$tion ta+e! place
file Arite t%e pattern !pace to file if a !$cce!!f$l !$*!tit$tion ta+e! place
K V Subba Raju, Asst. Professor
'=
%elete
T%e delete command i! very !imple in it! !yntax: it "oe! li+e t%i!
Kaddress1K C address2 L L&
2nd it delete! t%e content of t%e pattern !pace( 2ll folloin" command! are !+ipped
(after allC t%ereF! very little yo$ can do it% an empty pattern !pace)C and a ne line
i! read into t%e pattern !pace(
"#amle 1
>cat file
htt$://000.foo.com/my$a1e.html
>se& *e Ns]htt$://000.foo.com]htt$://000."ar.et]N
file
htt$://000."ar.et/my$a1e.html
3ote t%at e $!ed a different delimiterC Y for t%e !$*!tit$tion command( Sed permit!
!everal delimiter! for t%e ! command incl$din" YZC4: t%e!e alternative delimiter! are "ood
for !$*!tit$tion! %ic% incl$de !trin"! !$c% a! filename!C a! it ma+e! yo$r !ed code m$c%
more reada*le(
"#amle 2
>cat file
the "lac+ cat 0as chase& "y the "ro0 &o1
>se& *e Ns/"lac+/0hite/1N file
the 0hite cat 0as chase& "y the "ro0 &o1
T%at a! pretty !trai"%tforard( 3o e move on to !omet%in" more intere!tin"(
"#amle 3
>cat file
the "lac+ cat 0as chase& "y the "ro0 &o1.
the "lac+ cat 0as ot chase& "y the "ro0 &o1
K V Subba Raju, Asst. Professor
'>
>se& *e N/ot/s/"lac+/0hite/1N file
the "lac+ cat 0as chase& "y the "ro0 &o1.
the 0hite cat 0as ot chase& "y the "ro0 &o1.
5n t%i! in!tanceC t%e !$*!tit$tion i! only applied to line! matc%in" t%e re"$lar expre!!ion
ot( -ence it i! not applied to t%e fir!t line(
"#amle 1
>cat file
lie 1 'oe)
lie 2 't0o)
lie 3 'three)
"#amle 1a
>se& *e N1C2&N file
lie 3 'three)
"#amle 1b
>se& *e N3&N file
lie 1 'oe)
lie 2 't0o)
"#amle 1c
>se& *e N1C2s/lie/32NE/N file
32NE 1 'oe)
32NE 2 't0o)
lie 3 'three)
"#amle 1d
>se& *e N/%lie.Moe/s/lie/32NE/N *e N/lie/&N
file
32NE 1 'oe)
6a : T%i! a! pretty !imple: e :$!t deleted line! & to '(
6* : T%i! a! al!o pretty !imple( Ae deleted line 6(
6c : 5n t%i! exampleC e performed a !$*!tit$tion on line! &?'(
6d : no t%i! i! more intere!tin"C and de!erve! !ome explanation( Fir!tlyC it i! clear t%at line
' and 6 "et deleted( J$t letF! loo+ clo!ely at %at %appen! to line &(
K V Subba Raju, Asst. Professor
'@
Fir!tC line & i! read into t%e pattern !pace( 5t matc%e! t%e re"$lar expre!!ion %lie.Moe
So t%e !$*!tit$tion i! carried o$tC and t%e re!$ltin" pattern !pace loo+! li+e t%i!:
32NE 1 'oe)
So no t%e !econd command i! exec$tedC *$t !ince t%e pattern !pace doe! not matc% t%e
re"$lar expre!!ion lieC t%e delete command i! not exec$ted(
"#amle 2
>cat file
hello
this text is 0i$e& o!t
#i$e& o!t
hello 'also 0i$e& o!t)
#iXe& o!t T<<Z
1oo&"ye
'1) This text is ot &elete&
'2) either is this ... ' 1oo&"ye )
'3) either is this
hello
"!t this is
a& so is this
a& !less 0e fi& aother 1MM&"ye
every lie to the e& of the file 1ets &elete&
>se& *e N/hello/C/1oo&"ye/&N file
'1) This text is ot &elete&
'2) either is this ... '1oo&"ye)
'3) either is this
T%i! ill$!trate! %o t%e addre!!in" or+! %en to pattern addre!!e! are !pecified( !ed
find! t%e fir!t matc% of t%e expre!!ion U%elloUC deletin" every line read into t%e pattern
!pace $ntil it "et! to t%e fir!t line after t%e expre!!ion U"ood*yeU( 5t doe!nFt apply t%e delete
command to any more addre!!e! $ntil it come! acro!! t%e expre!!ion U%elloU a"ain( Since
t%e expre!!ion U"ood*yeU i! not on any !$*!eP$ent lineC t%e delete command i! applied to
all remainin" line!(
K V Subba Raju, Asst. Professor
'G
A);
:#^ i! a !imple and ele"ant pattern !cannin" and proce!!in" lan"$a"e
2A; i! al!o t%e mo!t porta*le !criptin" lan"$a"e
5t a! created in late >Kt% of t%e la!t cent$ry( T%e name a! compo!ed from t%e initial
letter! of t%ree ori"inal a$t%or! 2lfred V( 2%oC Jrian A( ;erni"%anC and Deter [(
Aein*er"er( 5t i! commonly $!ed a! a command?line filter in pipe! to reformat t%e o$tp$t
of ot%er command!( 5tF! t%e prec$r!or and t%e main in!piration of Derl( 2lt%o$"% ori"inated
in Hnix it i! availa*le and idely $!ed in Aindo! environment too(
2A; ta+e! to inp$t!: data file and command file( T%e command file can *e a*!ent and
nece!!ary command! can *e pa!!ed a! a$"ment!( 2! ,onald D( 7o$i aptly noted a(! s
ve#- $nde# a++#e)a"ed %an1$a1e4
T%e main advanta"e of 2A; i! t%at $nli+e Derl and ot%er U!criptin" mon!ter!U t%at it i!
very !lim it%o$t feat$re creep !o c%aracteri!tic of Derl and t%$! it can *e very efficiently
$!ed it% pipe!( 2l!o it %a! rat%er !impleC clean !yntax and li+e m$c% %eavier TC7 can *e
$!ed it% C for Ud$al?lan"$a"eU implementation!(
a+F! favor compared to perl:
? a+ i! !impler (e!pecially important if decidin" %ic% to learn fir!t)
? a+ !yntax i! far more re"$lar (anot%er advanta"e for t%e
*e"innerC even it%o$t con!iderin" !yntax?%i"%li"%tin" editor!)
? yo$ may already +no a+ ell eno$"% for t%e ta!+ at %and
? yo$ may %ave only a+ in!talled
? a+ can *e !mallerC t%$! m$c% P$ic+er to exec$te for !mall pro"ram!
? a+ varia*le! donFt %ave \$F in front of t%em :?)
? clear perl code i! *etter t%an $nclear a+ code4 *$t 31T-53I come!
clo!e to $nclear perl code
$&e basic function of a(# is to searc& files for lines (or ot&er units of text) t&at contain
certain patterns0 4&en a line matc&es one of t&e patterns- a(# performs specified actions
on t&at line0 a(# #eeps processing input lines in t&is (a" until it reac&es t&e end of t&e
input files
Syntax : a+ [option] X!election criteria VactionWB <file>
1ption! : ?F ? To !pecify t%e field !eparator
?f ? To invo+e t%e !o$rce code
VactionW ? 1nly t%e print action
predefined 'ariables in a(#
K V Subba Raju, Asst. Professor
6K
all predefined varia*le! are in $pper ca!e!
F( * 2$!t fiel& se$arator
<F( * <!$!t fiel& se$arator
NF * N!m"er of fiel&s
N; * ;ecor& !m"ers or No. of recor&s
$ * Fiel&s i a0+
Comparation .perator in a(#
> * >rater tha
>B * >rater tha eY!al
H * 3ess tha
HB * 3ess tha eY!al
BB * EY!al to
ZB * Not eY!al
P * Fatchi1
ZP * ot matchi1
7o"ical 1perator
]] ? 23)
RR ? 1,
a+ %a! "ot 6 !ection!
&( J5I53
'( #5))70
6( 03)
Je"in i! +eyord for t%e *e"in !ection t%e varia*le can *e a!!i"n in *e"in !ection
2ll t%e operator in t%e middle !ection!C #iddle i! not +eyord for t%e middle !ection
A%at ever $ print every t%in" in t%e !ectionC 0nd i! t%e +eyord for t%e end !ection
$ a0+ _/a,ay/S$ritT`Hfile>
2t $rits the all the recor&s
$ a0+ _/a,ayAram!/S$ritT`Hfile>
To search for m!lti$le ex$ressios a& $rit
$ a0+ _N;BB4S$ritT` Hfile>
To $rit s$ecific recor&
K V Subba Raju, Asst. Professor
6&
$ a0+ _N;BB3CN;BB?S$ritT`Hfile>
To $rit ra1e of recor&s
$ a0+ _N;>4S$ritT` Hfile>
To $rit all the recor&s 0hich are >4
$ a0+ _N;>BS$ritT` Hfile>
$ a0+ _N;H4S$ritT` Hfile>
To $rit all the recor&s 0hich are H4
$ a0+ _N;HB4S$ritT` Hfile>
if $ ant print only !pecific field!
$ a0+ .F Q:R _N;BB4 S$rit $1C $3C $4T` Hfile>
!imple a0+ pro"ram em$late! t%e cat $tility4 it copie! %atever yo$ type on t%e
+ey*oard to it! !tandard o$tp$t (%y t%i! or+! i! explained !%ortly)(
$ a0+ NS $rit TN
No0 is the time for all 1oo& me
*A No0 is the time for all 1oo& me
to come to the ai& of their co!try.
*A to come to the ai& of their co!try.
Fo!r score a& seve years a1oC ...
*A Fo!r score a& seve years a1oC ...
#hatC me 0orryJ
*A #hatC me 0orryJ
Otrl*&
Drint t%e len"t% of t%e lon"e!t inp$t line:
a0+ NSif'le1th'$6)> max)max B le1th'$6) T
END S $rit max TN &ata
Drint every line t%at i! lon"er t%an @K c%aracter!:
a0+ Nle1th'$6) > 86N &ata
T%e !ole r$le %a! a relational expre!!ion a! it! pattern and it %a! no action^!o t%e
defa$lt actionC printin" t%e recordC i! $!ed(

Drint t%e len"t% of t%e lon"e!t line in data:
ex$a& &ata A a0+ NSif x H le1th')) x Ble1th')T
END S$rit Dmaxim!m lie le1th is D xTN
K V Subba Raju, Asst. Professor
6'

T%e inp$t i! proce!!ed *y t%e expand $tility to c%an"e ta*! into !pace!C !o t%e
idt%! compared are act$ally t%e ri"%t?mar"in col$mn!(
Drint every line t%at %a! at lea!t one field:
a0+ NNF > 6N &ata

T%i! i! an ea!y ay to delete *lan+ line! from a file (or rat%erC to create a ne file
!imilar to t%e old file *$t from %ic% t%e *lan+ line! %ave *een removed)(
Drint !even random n$m*er! from K to &KKC incl$!ive:
a0+ NBE>2N Sfor'iB1EiHB?EiII)$rit it'161Mra&'))TN
Drint t%e total n$m*er of *yte! $!ed *y files:
ls *l files A a0+ NS x IB $- T
END S $rit Dtotal "ytes: D x TN
Drint t%e total n$m*er of +ilo*yte! $!ed *y files:
ls *l files A a0+ NS x IB $- T
END S $rit Dtotal ^*"ytes: D x I 1623)/1624 TN
Drint a !orted li!t of t%e lo"in name! of all $!er!:
a0+ *F: NS $rit $1 TN /etc/$ass0& A sort
Co$nt t%e line! in a file:
a0+ NEND S $rit N; TN &ata
Drint t%e even?n$m*ered line! in t%e data file:
a0+ NN; [ 2 BB 6N &ata

5f yo$ $!e t%e expre!!ion \3, Z ' __ &F in!teadC t%e pro"ram o$ld print t%e odd?
n$m*ered line!
EXAMPE!
\ is the commet character for a0+. Nfiel&N meas
Ncol!mN
\ Xrit first t0o fiel&s i o$$osite or&er:
a0+ NS $rit $2C $1 TN file
K V Subba Raju, Asst. Professor
66
\ Xrit lies lo1er tha ?2 characters:
a0+ Nle1th > ?2N file

\ Xrit le1th of stri1 i 2& col!m
a0+ NS$rit le1th'$2)TN file
\ :&& !$ first col!mC $rit s!m a& avera1e:
S s IB $1 T
END S $rit Ds!m isDC sC D avera1e isDC s/N; T
\ Xrit fiel&s i reverse or&er:
a0+ NSfor i B NFE i > 6E **i)$rit $i TN file
\ Xrit the last lie
Slie B $6T
END S$rit lieT
\ Xrit the total !m"er of lies that
cotai the 0or& Xat
/Xat/ Slies B lies I 1T
END S$rit liesT
\ Xrit all lies "et0ee start/sto$ $airs:
a0+ N/start/C /sto$/N file
\ Xrit all lies 0hose first fiel& is
&ifferet from $revio!s oe:
a0+ N$1 ZB $rev S $ritE $rev B $1 TN file
\ Xrit col!m 3 if col!m 1 > col!m 2:
a0+ N$1 > $2 S$rit $3TN file

\ Xrit lie if col!m 3 > col!m 2:
a0+ N$3 > $2N file
K V Subba Raju, Asst. Professor
68
\ Oo!t !m"er of lies 0here col3 >col 1
a0+ N$3 > $1 S$rit i I D1DE iIITN file
\ Xrit seY!ece !m"er a& the col!m 1 of file:
a0+ NS$rit N;C $1TN file
\ Xrit every lie after erasi1 the 2& fiel&
a0+ NS$2 B DDE $ritTN file
\ Xrit hi 28 times
yes A hea& *28 A a0+ NS $rit DhiD TN
\ Xrit hi.6616 to hi.6699 'N<TE 2;:F a(E;(Z)
yes A hea& *96 A a0+ NS$ritf'Dhi66[2.6f UDC
N;I9)TN
\ ;e$lace every fiel& "y its a"sol!te val!e
S for 'i B 1E i HB NFE iBiI1) if '$i H 6)
$i B *$i $ritT
\ 2f yo! have aother character that &elimits
fiel&sC !se the *F o$tio
\ For exam$leC to $rit o!t the $hoe !m"er for
7oes i the follo0i1 fileC
\ 666962ABeavisATheo&oreA333*242*2222A149692
\ 666961A7oesABillA-32*382*6342A234623
\ ...
\ ty$e
a0+ *FDAD N$2BBD7oesDS$rit $4TN fileame
\ (ome loo$i1 for $rito!ts
BE>2NS
for 'iB8?-Ei>833Ei**)S
$ritf Dl$rm *Xl0 [&UDC i
T exit
T
K V Subba Raju, Asst. Professor
69
Formatte& $rito!ts are of the form
$ritf' DformatUDC val!e1C val!e2C ... val!eN)
e.1. $ritf'Dho0&y [*8s #hat it is "ro.
[.2fUDC $1C $2M$3)
[s B stri1
[*8s B 8 character stri1 left ,!stifie&
[.2f B !m"er 0ith 2 $laces after .
[@.2f B fiel& @ chars 0ith 2 chars after .
U is e0lie
Ut is a ta"
\ Xrit freY!ecy histo1ram of col!m of !m"ers
$2 HB 6.1 SaBaI1T
'$2 > 6.1) 55 '$2 HB 6.2) S" B "I1T
'$2 > 6.2) 55 '$2 HB 6.3) Sc B cI1T
'$2 > 6.3) 55 '$2 HB 6.4) S& B &I1T
'$2 > 6.4) 55 '$2 HB 6.-) Se B eI1T
'$2 > 6.-) 55 '$2 HB 6.@) Sf B fI1T
'$2 > 6.@) 55 '$2 HB 6.?) S1 B 1I1T
'$2 > 6.?) 55 '$2 HB 6.8) Sh B hI1T
'$2 > 6.8) 55 '$2 HB 6.9) Si B iI1T
'$2 > 6.9) S, B ,I1T
END S$rit aC "C cC &C eC fC 1C hC iC ,C
N;T
\ Fi& maxim!m a& miim!m val!es $reset i
col!m 1
N; BB 1 SmB$1 E $B$1T
$1 >B m Sm B $1T
$1 HB $ S$ B $1T
END S $rit DFax B D mC D Fi B D $ T
\ Exam$le of &efii1 varia"lesC m!lti$le
comma&s o oe lie
N; BB 1 S$revB$4E $reva B $1E $rev" B $2E B6E
s!mB6T
$4 ZB $rev S$rit $revaC $rev"C $revC s!m/E B6E
s!mB6E $rev B $4E $reva B $1E $rev" B $2T
$4 BB $rev SIIE s!mBs!mI$-/$@T
END S$rit $revaC $rev"C $revC s!m/T
\ Exam$le of !si1 s!"stri1s
\ s!"str'$2C9C?) $ic+s o!t characters 9 thr! 1- of
col!m 2
K V Subba Raju, Asst. Professor
6=
S$rit DimarithDC s!"str'$2C1C?) D * D $3C
Do!t.Ds!"str'$2C-C3)T
S$rit DimarithDC s!"str'$2C9C?) D * D $3C
Do!t.Ds!"str'$2C13C3)T
S$rit DimarithDC s!"str'$2C1?C?) D * D $3C
Do!t.Ds!"str'$2C21C3)T
$rit DimarithDC s!"str'$2C2-C?) D * D $3C
Do!t.Ds!"str'$2C29C3)T
1. $enaming 4it!in t!e name:
ls *1 Mol&M A a0+ NS$rit Dmv D$1D D$1TN A se&
s/ol&/e0/2 A sh
(alt%o$"% in !ome ca!e! it ill failC a! in file`old`and`old)
2. $emo%e only -iles:
ls *l M A 1re$ *v &r0x A a0+ NS$rit Drm D$9TN A sh
or it% a+ alone:
ls *lAa0+ N$1ZP/%&r0x/S$rit $9TNAxar1s rm
Je caref$l %en tryin" t%i! o$t in yo$r %ome directory( Ae remove file!a
3. $emo%e only directories
ls *l A 1re$ N%&N A a0+ NS$rit Drm *r D$9TN A sh
or
ls *$ A 1re$ /$ A 0+ NS$rit Drm *r D$1TN
or it% a+ alone:
ls *lAa0+ N$1P/%&.Mx/S$rit $9TNAxar1s rm *r
Je caref$l %en tryin" t%i! o$t in yo$r %ome directory( Ae remove t%in"!a
1. ;illing rocesses by name (in t%i! example e +ill t%e proce!! called net!cape):
+ill b$s a!x00 A 1re$ etsca$e A e1re$ *v 1re$ A a0+
NS$rit $2TNb
"n%ironment Control
Command Descrition
c& & Oha1e to &irectory &
K V Subba Raju, Asst. Professor
6>
m+&ir & Oreate e0 &irectory &
rm&ir & ;emove &irectory &
mv f1 Kf2...L & Fove file f to &irectory &
mv &1 &2 ;eame &irectory &1 as &2
$ass0& Oha1e $ass0or&
alias ame1 ame2 Oreate comma& alias 'csh/tcsh)
alias ame1BDame2D Oreate comma& alias '+sh/"ash)
!alias ame1Ka2...L ;emove comma& alias a
ssh & 3o1i sec!rely to remote o&e
exit E& termial sessio
setev ame v (et ev var to val!e v 'csh/tcsh)
ex$ort ameBDvD (et eviromet varia"le to val!e
v'+sh/"ash)
<utut= Communication= > +el
Command Descrition
l$r *X $riter f
or
l$ *& $riter f <!t$!t file f to lie $riter
scri$t KfL (ave termial sessio to f
exit (to$ savi1 termial sessio
mailx !serame (e& mail to !ser
ma ame aix ma!al etry for ame
&rocess Control
Command Descrition
OT;3/c M 2terr!$t $rocesses
K V Subba Raju, Asst. Professor
6@
OT;3/s M (to$ scree scrolli1
OT;3/Y M ;es!me scree o!t$!t
slee$ (lee$ for seco&s
,o"s Xrit list of ,o"s
+ill [ ^ill ,o"
$s Xrit $rocess stat!s stats

+ill *9 ;emove $rocess
OT;3/4 M (!s$e& c!rret $rocess
sto$ [ (!s$e& "ac+1ro!& ,o"
cmm&5 ;! cmm& i "ac+1ro!&
"1 K[L ;es!me "ac+1ro!& ,o"
f1 K[L ;es!me fore1ro!& ,o"
exit Exit from shell
"n%ironment (tatus
Command Descrition
ls K&L Kf...L 3ist files i &irectory
ls *1 Kf...L 3ist files i &etail
alias KameL Dis$lay comma& aliases
$ritev KameL Xrit eviromet val!es
Y!ota Dis$lay &is+ Y!ota
&ate Xrit &ate 5 time
0ho 3ist lo11e& i !sers
0hoami Dis$lay c!rret !ser
K V Subba Raju, Asst. Professor
6G
fi1er K!serameL <!t$!t !ser iformatio
chf Oha1e fi1er iformatio
$0& Xrit 0or+i1 &irectory
history Dis$lay recet comma&s
Z (!"mit recet comma&
File 'aniulation
Command Descrition
vi KfL Gi f!ll scree e&itor
emacs KfL Emacs f!ll scree e&itor
e& KfL Text e&itor
0c f 3ieC 0or&C 5 char co!t
cat f 3ist cotets of file
more f 3ist file cotets "y scree
cat f1 f2 >f3 Oocateates f1 5 f2 ito f3
chmo& mo&e f Oha1e $rotectio mo&e of f
cm$ f1 f2 Oom$are t0o files
c$ f1 f2 Oo$y file f1 ito f2
sort f :l$ha"etically sort f
s$lit K*L f ($lit f ito *lie $ieces
mv f1 f2 ;eame file f1 as f2
rm f Delete 'remove) file f
1re$ N$tN f <!t$!ts lies that match $t
&iff f1 f2 3ists file &iffereces
K V Subba Raju, Asst. Professor
8K
hea& f <!t$!t "e1ii1 of f
tail f <!t$!t e& of f
Comiler
Command Descrition
cc K*o f1L f2 O com$iler
lit f Ohec+ O co&e for errors
f?? K*o f1L f2 Fortra?? com$iler
$c K*o f1L f2 Xascal com$iler
Abbre%iations used in t!is document
OT;3/x hol& &o0 cotrol +ey a& $ress x
& &irectory
ev eviromet
f fileame
!m"er
& com$!ter o&e
$rtr $riter
$t $atter
var varia"le
Ky/L yes or o
K V Subba Raju, Asst. Professor
8&

You might also like