+ All Categories
Home > Documents > Unix Reference Manual

Unix Reference Manual

Date post: 04-Jun-2018
Category:
Upload: mohammad-zaheer
View: 223 times
Download: 1 times
Share this document with a friend

of 101

Transcript
  • 8/13/2019 Unix Reference Manual

    1/101

    Unix User Guide

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    2/101

    Table of Contents

    Introduction to Unix..............................................................................................................................................1

    What is Unix?.....................................................................................................................................................1Features of Unix..................................................................................................................................................1Evolution of Unix...............................................................................................................................................1

    Getting Started.......................................................................................................................................................3

    Unix users and superusers..................................................................................................................................3Signing on to Unix..............................................................................................................................................3Unix system prompt............................................................................................................................................4Introdution to Some U!I" ommands.............................................................................................................4

    date: Displaying Date and Time...................................................................................................................4al# $isplaying the alendar...............................................................................................................................4logname# %ells the login diretory ....................................................................................................................&ls# 'isting out diretory ontents........................................................................................................................&at# $isplaying ontents of files.........................................................................................................................&Stopping( )alting and *esuming a ommand ..................................................................................................+

    The vi Editor..........................................................................................................................................................8

    What is vi?..........................................................................................................................................................,Editing a File......................................................................................................................................................,%hree -odes of vi........................................................................................................................................... ....,

    The Command Mode......................................................................................................................................8The Insert Mode.............................................................................................................................................9The ex Mode..................................................................................................................................................9

    !avigating in a $oument..................................................................................................................................Inserting %ext....................................................................................................................................................11

    *eplaing %ext..................................................................................................................................................11Saving Wor/ and Exiting vi.............................................................................................................................10Searhing for a pattern.....................................................................................................................................10Searh and *eplae...........................................................................................................................................10reating a !e2 File..........................................................................................................................................13$eleting %ext....................................................................................................................................................13an/ing opying5 %ext....................................................................................................................................13hanging %ext..................................................................................................................................................146asting %ext......................................................................................................................................................14Users $efined 7uffers.......................................................................................................................................14-ar/ing %ext....................................................................................................................................................1&899reviating %ext Input ..................................................................................................................................1+-apping :eys of the :ey9oard..................................................................................................................... ...1+

    SE% options.......................................................................................................................................................1+ustomi;ing vi..................................................................................................................................................1,Exerises...........................................................................................................................................................1

    ..............................................................................................................................................................................19

    UI! "ile Structure............................................................................................................................................#$

    Features of the File Struture...........................................................................................................................0 alting and esu)ing a co))and

    %he premature termination of a ommand an 9e ahieved 9y pressing ?ctrl@cA. I.e. supposesometimes the ommand runs for an unexpeted period of time( it an 9e interrupted in 9et2een 9y?ctrl@cA.

    If the display of a ommand is srolling too fast for you to see on the terminal( you an halt theoutput temporarily 9y pressing ?ctrl@sA.

    Unix User uide -ohammad Jaheer 8hmed +

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    12/101

    %he temporarily halted output an 9e resumed 9y pressing ?ctrl@BA.

    Unix User uide -ohammad Jaheer 8hmed

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    13/101

    The vi Editor

    %hat is vivi is a very po2erful text editor availa9le in Unix. It is one of the most important and nota9leontri9utions made 9y 7S$ to Unix( 2hih 2as originally developed 9y a graduate student( WilliamAoy( from the University of alifornia( 7er/ley.

    vi is a fullsreen editor that ma/es full use of the /ey9oard. 8lmost every /ey in vi has somefuntion assoiated 2ith it.

    =n the other hand( vi ontains rypti ommands for editing 2or/( 2hih are a 9it diffiult toremem9er in the 9eginning. )o2ever( one you have overome the initial diffiulties of 2or/ing 2ith

    vi( you 2ill find it to 9e a very po2erful text editor.

    Editing a "ile

    ou an open an existing file for editing in vi 9y giving the filename along 2ith the vi ommand. Forexample( to open a file alled msgB. in your urrent 2or/ing diretory( you 2ill give the follo2ingommand#

    i m'g)c

    %he vi editor displays the first 04 lines of the opened file on the sreen. %he last line on the sreen isreserved for some ommands that you an enter to at on the text( as 2ell as for displaying someinformation messages 9y vi.

    Three 6odes of vi

    %he vi editor 2or/s in three modes( /no2n as the ommand mode( input mode( and the ex mode.

    %he ommand -ode

    In the ommand mode( typing a /ey on the /ey9oard usually results in some ation( and not ininsertion of the typed harater in the file. %here are speifi ommands assoiated 2ith most of the/eys on the /ey9oard. For example( pressing the /ey RHR on the /ey9oard auses the ursor to moveone line do2n( and pressing the /ey R/R auses the ursor to move one line up. Similarly( pressingthe /ey RlR ell( not one5 auses the ursor to move one harater right( and pressing the /ey RhRauses it to move one harater left.

    When you start vi( 9y default you are plaed in the ommand mode( 2hih an then 9e hanged toany of the other modes.

    Unix User uide -ohammad Jaheer 8hmed ,

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    14/101

    %he Insert -ode

    When you 2ant to type some text in a file at some position( you need to 9e in the insert mode. %hereare various ommands for entering the insert mode( 2ith differenes regarding 2here the ne2 text isto 9e inserted. =ne of the most ommon 2ays to enter the insert mode is to press the RiR /ey. When

    you press this /ey( the vi editor ta/es you to the insert mode( and thereafter any /eys pressed 9y youon the /ey9oard result in the orresponding harater 9eing typed in the file at the ursor loation.

    ou an return from the insert mode to the ommand mode 9y pressing the ES86E /ey on the/ey9oard.

    %he ex -ode

    Some of the vi ommands are longer than one harater( and ta/e you to the ex mode. Suhommands usually 9egin 2ith the R#R /ey. 8s you type this and the su9seBuent haraters of aommand( they appear on the last line of the sreen. 8ll the ex mode ommands must 9e typed fromthe ommand mode.

    For example( you an exit vi 2ithout saving the urrent file 9y typing the follo2ing haraters on the/ey9oard( 2hen you are in the ommand mode#

    :8

    =ne useful ommand that you may try no2 is the follo2ing#

    :'et '-o5mode

    %his auses the vi editor to sho2 in the last line 2hether you are in the insert mode or the ommandmode.

    avigating in a (ocu)ent

    8ll the navigation ommands disussed in this setion are to 9e given from the ommand mode only.

    %he follo2ing /eys an 9e used for moving the ursor 2ithin a doument#

    - Moe' cur'or left 3y one c-aracterl Moe' cur'or rig-t 3y one c-aracterk Moe' cur'or u( 3y one line Moe' cur'or do5n 3y one line

    %hese( as 2ell as other navigation ommands disussed later an 9e given a repeat fator 9efore theommand. For example( typing ,H moves the ursor do2n 9y eight lines.

    %hen there are some ommands for using a 2ord as a navigation unit( instead of a harater#

    5 Moe' to t-e 3eginning of t-e ne7t 5ord

    Unix User uide -ohammad Jaheer 8hmed

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    15/101

    3 Moe' to t-e 3eginning of t-e current . (reiou' 5orde Moe' to t-e end of t-e current . ne7t 5ord

    8 2ord is defined as a string of alphanumeri haraters and the undersore P5 harater. 8 repeatfator an 9e om9ined 2ith these ommands. For example( 42 ta/es the ursor for2ard 9y four

    2ords.

    %he ommands W( 7( and E 2or/ li/e the their lo2erase ounterparts desri9ed a9ove( exept thatpuntuation mar/s are s/ipped.

    %he follo2ing are other useful ommands for plaing the ursor#

    Moe' to t-e 3eginning of t-e current line, #ame a' )$ Moe' to t-e end of t-e current line)

    ou an use the repeat fator 2ith the RR harater to move to a speified position in a line. Forexample( 0& moves ursor to the t2entyfifth position in the line.

    ou an move vertially in units larger than one line too. %he follo2ing ommands an 9e used forthis purpose#

    Ctrl-f S croll' full (age for5ardCtrl-b #croll' full (age 3ack5ardCtrl-d #croll' -alf (age fo5ardCtrl-u #croll' -alf (age 3ack5ard

    If( for some reason( the sreen gets gar9led( you an use the follo2ing ommand no repeat fator

    2ith this ommand5#

    Ctrl-l Redra5' t-e 'creen)

    ou an find out the line num9er of your urrent ursor position( 9y the follo2ing ommand#

    Ctrl-g 9i'(lay' current line num3er along 5it- filename andtotal line count)

    ou an move to any line 2ithin a doument as follo2s#

    !; Moe' to t-e fift- line)1; Moe' to t-e fir't line); Moe' to t-e la't line) ;o to fir't line on t-e 'creen)L ;o to la't line on t-e 'creen)

    ou an as/ vi to display line num9ers in the doument( 9y typing the follo2ing ex mode ommand#

    :'et num3er

    or can 3e a33reiated a'

    Unix User uide -ohammad Jaheer 8hmed 1

  • 8/13/2019 Unix Reference Manual

    16/101

    :'et nu

    %o turn off the line num9er display( you an give the follo2ing ommand#

    :'et nonum3eror:'et nonu

    Inserting Text

    %o insert text in a file( you first need to plae the ursor 2here the text is to 9e inserted. %hen( youan give the follo2ing ommand#

    i "n'ert' te7t at cur'or (o'ition '-ifting e7i'ting te7t to

    rig-t

    %he RiR harater does not sho2 on the sreen( 9ut ta/es the editor from the ommand mode to theinsert mode. If you have earlier given the R:'et '-o5modeR ommand( then it 2ill ause the 2ords

    RI!SE*% -=$ER or something else to that effet5 at the 9ottom right hand orner of the sreen.When you are through 2ith insertion of text( and 2ant to return to the ommand mode( pressES86E.

    %he follo2ing other ommands an also 9e used for inserting text in a file#

    a 6((end' te7t to t-e rig-t of t-e cur'or (o'ition)

    " "n'ert' te7t at t-e 3eginning of t-e current line)6 6((end te7t at t-e end of t-e current line)o =(en' a ne5 line 3elo5 t-e current line)= =(en' a ne5 line a3oe t-e current line) oin ne7t line to t-e end of current line)

    e+lacing Text

    ou an replae one harater 9y another( you an use the RrR ommand( follo2ed 9y the haraterthat replaes the original harater. ou do not need to press the ES86E /ey after using thisommand.

    ou an replae multiple haraters 9y using the R*R ommand( that replaes text as the ursormoves to the right( till you press ES86E or reah the end of the line.

    %hese and other ommands for replaing text are summari;ed 9elo2#

    r Re(lace' one c-aracter)R Re(lace' multi(le c-aracter' a' cur'or moe' oer te7t to

    t-e rig-t)' Re(lace' a 'ingle c-aracter 5it- ne5 te7t irre'(ectie of

    it' lengt-)

    Unix User uide -ohammad Jaheer 8hmed 11

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    17/101

    # Re(lace' t-e entire line irre'(ectie of t-e cur'or(o'ition)

    Saving %or& and Exiting vi

    8ny time 2hile editing a file( you an save the 2or/ 9y using the R:5R ommand. 8fter saving the

    file( vi /eeps the file open for you to ontinue editing it.

    :5 #ae' t-e file and continue' editing it

    ou an save the file to another name 9y speifying a file name after R#2R( for example#

    :5 'omeot-erfile

    Some other ommands for saving your 2or/ and K or exiting vi are#

    : E7it i 5it-out 'aing 5ork ?5ill not 5ork if any c-ange'-ae 3een made@

    :8 E7it i di'carding c-ange' to t-e file:5 #ae c-ange' and e7it i:7 #ame a' A:5A

    Searching for a +attern

    'ines ontaining the string an 9e loated 9y prefixing the searh string 2ith a for2ard slash K 5.

    6ressing of LK L is ehoed in the last line of the sreen. Enter the pattern and press NenterO. %oloate the first ourrene of string LBueueM in the file msgB.( simply enter

    .ueue B in t-e command mode

    It 2ill searh for2ard for the string LBueueM in the file from the urrent ursor position.'i/e2ise( the seBuene

    Cueue

    2ill searh for LBueueM 9a/2ard to the urrent ursor position.

    n To re(eat a 'earc- in t-e direction in 5-ic- t-e (reiou''earc- 5a' made)

    N To re(eat a 'earc- in t-e 3ack5ard direction in 5-ic- t-e(reiou' 'earc- 5a' made)

    Search and e+lace

    @i offers yet another po2erful feature( that is of searhing and replaing a pattern.

    Unix User uide -ohammad Jaheer 8hmed 10

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    18/101

    :'.one.t5o re(lace occurrence of AoneA 5it- At5oA in currentline:'.i'.are.g re(lace all occurrence' of Ai'A 5it- AareA in currentline:1$'.i'.are re(lace all occurrence' of Ai'A 5it- AareA in t-efile:1$'.i'.are.c re(lace all occurence' of Di'D 5it- areD in t-e file5it- confirmation

    Creating a e' "ile

    ou an reate a ne2 file 9y Hust typing vi at the ommand prompt#

    i

    %his opens an empty file for you( in 2hih text an 9e typed and su9seBuently edited. ou an alsogive a name to a ne2 file 2hile opening it( suh as#

    i ne5file

    (eleting Text

    %he simplest 2ay to delete one harater in the file is to plae the ursor at the harater that is to 9edeleted( and then give the follo2ing ommand#

    7 9elete' a 'ingle c-aracter at t-e cur'or (o'ition)

    ou an also use the repeat fator 2ith this and other ommands in this setion. For example( typing&x 2ill delete five haraters starting from the ursor position.

    Some of the other useful ommands for deletion of text are#

    F 9elete' c-aracter 3efore cur'or)dd 9elete' current line)d5 9elete' current 5ord from cur'or (o'ition on5ard')d$ 9elete' te7t from cur'or (o'ition till t-e end of line)d 9elete' te7t from t-e 3eginning of t-e line till t-e cur'or

    (o'ition)u To undo t-e la't c-ange)) To redo t-e la't command)

    an&ing Dco+ing< Text

    %he simplest 2ay to yan/ opy5 one line in the file is to plae the ursor at any position on the linethat is to 9e yan/ed( and then give the follo2ing ommand#

    yy Gank' a 'ingle line at t-e cur'or (o'ition)

    Unix User uide -ohammad Jaheer 8hmed 13

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    19/101

    ou an also use the repeat fator 2ith this and other ommands in this setion. For example( typing&yy 2ill yan/ five lines starting from the ursor position.

    Some of the other useful ommands for yan/ing of text are#

    y5 Gank' current 5ord from cur'or (o'ition on5ard')y$ Gank' te7t from cur'or (o'ition till t-e end of line)y Gank' te7t from t-e 3eginning of t-e line till t-e cur'or

    (o'ition)

    Changing Text

    %ext an 9e hanged 2ith the change5 operator. %he simplest 2ay to hange the urrent line in thefile is to plae the ursor any2here in the line that is to 9e hanged( and then give the follo2ing

    ommand#

    cc H-ange' a 'ingle line at t-e cur'or (o'ition)

    ou an also use the repeat fator 2ith this. For example( typing & 2ill delete five lines startingfrom the ursor position.

    Some of the other useful ommands for hange of text are#

    c5 H-ange' current 5ord from cur'or (o'ition on5ard')c$ H-ange' te7t from cur'or (o'ition till t-e end of line)

    H H-ange' te7t from cur'or (o'ition till t-e end of line) c H-ange' te7t from t-e 3eginning of t-e line till t-e cur'or(o'ition)

    %his is the only operator( 2hih funtions( in the input mode. %he hange operation thus has to 9eterminated 2ith NESO.

    *asting Text

    %he deleted or yan/ed text an 9e opied at the ne2 loation using the p paste5 operator. %o simply

    paste the last deleted or yan/ed line the follo2ing ommand an 9e used#

    ( Ia'te' 3elo5 t-e current line)I Ia'te' a3oe t-e current line)

    If a part of the line or a 2ord is deleted or yan/ed( then p operator 2ill paste the yan/ed or deletedtext Hust after the urrent ursor position.

    Users (efined 0uffers

    Unix User uide -ohammad Jaheer 8hmed 14

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    20/101

    We Hust sa2 ho2 text is moved or opied from original loation at ne2 loation. %his is done9asially 9y storing the text in unnamed 9uffer( and restoring it later. We have several limitations2ith these unnamed 9uffers i.e. first( only one 9uffer an 9e used at a time. Seond( 2hen movingtext from one files to another the ontents of the unnamed 9uffer is lost.

    %hus vi provides us 2ith a feature that allo2s to store 9lo/s of text in a num9er of speial 9uffers(2hih an 9e named after the haraters of the alpha9et. %o do so( Hust preede the operation 2ith aL dou9le Buote5 and the 9uffer name.

    E.g. to yan/ 4 lines from urrent ursor position 2e an give the ommand(

    a4yy Gank' 4 line' from current line in 3uffer aD)

    Some of the other useful ommands are#

    a( (a'te' t-e content' of 3uffer aD 3elo5 t-e current

    line) 64yy 6((end' 4 line' to t-e e7i'ting 3uffer aD)

    Using these named 9uffers 2e an move text 9et2een one or more files opened in the single visession. =ne the vi session is losed the named 9uffers ontents are also lost.

    We sa2 that 2ith u> 2e an undo only the last hange. 7ut the situation may arise 2here 2e 2ant torestore a num9er of reent deletions. %hus vi also provides us 2ith num9ered 9uffers( 2hih anrestore up to the last nine 9lo/s of deleted lines. %he most reent deletion is stored in num9ered

    9uffer 1. %hese 9uffers are also aessed 9y prefixing the 9uffer num9er 9y L dou9le Buotes5. =nly2hen entire lines are deleted is stored in these 9uffers. %o restore the most reent deletion( follo2ingommand an 9e given(

    1(

    6ar&ing Text

    8s you use a 9oo/mar/ to loate a partiular page later. Similarly vi allo2s us to mar/ a partiularline of a file and position the ursor at any mar/ed loation anytime later. %he ) ommand along2ith a harater is used for mar/ing a line and T 2ith the same harater to loate the mar/ed text.%o mar/ any portion of a file 2ith harater LaM( move the ursor to the reBuired loation and press

    ma

    an invisi9le mar/ is left at the orresponding point in the file. %o ome to the mar/ed line later Husttype in#

    Ja

    In this 2ay 2e an mar/ 0+ loations in the file.

    Unix User uide -ohammad Jaheer 8hmed 1&

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    21/101

    5bbreviating Text In+ut

    @i allo2s us to a99reviate short strings to long 2ords. %he syntax for a99reviating a string is as#

    :a3 ('(l Iragati #oft5are Iriate Limited

    %his implies that 2henever you use the string LpsplM in your vi session it 2ill expand to L6ragatiSoft2are 6rivate 'imitedM.Important point to note here is that the definition of the a99reviation remains only for the session in2hih it is defined.

    6a++ing es of the eboard

    %he )a+ ommand lets you assign the undefined /eys or reassign the defined /eys so that 2hen they

    are pressed( it expands into a ommand seBuene. %he ommand# the /ey( 2hih is to 9e mapped(and the /ey seBuene( 2hih is mapped( follo2 map.

    For instane( yan/ing into a 9uffered named LaM an 9e mapped to a /eystro/e LvM

    : ma( ayy

    %hus no2 to yan/ the urrent line 2e an Hust press LvM. Funtion /eys of the /ey9oard an also 9emapped as#

    : ma( B1 : 7KM

    %his 2ill map the F1 funtion /ey to save and exit the file. )ere - signifies NE!%E*O /ey( 2hihvi understands as NtrlVmO. %his harater is entered 9y first pressing NtrlPvO and then NtrlVmO.

    SET o+tions

    %here are several options that user an set to ustomi;e the environment of vi and a through/no2ledge of set option 2ill help the user ma/e the vi mode friendlier and onvenient to 2or/ 2ith.%he options are numerous( as al2ays. %o see all the option availa9le 2ith set( use the ommand

    :'et all

    Several olumns of options 2ill 9e displayed to you. -ost of the names are selfexplanatory. We 2illloo/ at some of them( 2hih are most useful for our 2or/. %o turn on any of the options you anuse

    :'et o(tionname

    8nd to turn off the option the ommand is

    :'et noo(tionname

    Unix User uide -ohammad Jaheer 8hmed 1+

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    22/101

    1. 7set nu)ber 4 7set nu

    auses all lines in the file to 9e num9ered.

    #. 7set sho')ode

    $isplays the mode in 2hih you are urrently 2or/ing. For example if you are in LI!SE*%-=$EM then same 2ill appear at ommand line.

    3. 7set auto'rite 4 7set a'

    %his option auses the ontents of the 9uffer to 9e automatially 2ritten to the urrent file( if youmodified it 2hen you gave a next n5 or re2ind re25 ommand. %his is partiularly useful 2henyou are editing multiple files at same time.

    ,. 7set autoindent 4 7set ai

    If you are typing programs in vi then this 2ould 9e a handy option to use. 6rogram ode is morelegi9le and understanda9le 2hen it is suita9ly indented. With the set option( the ursor returns tothe position 9elo2 the start of the line a9ove. %his failitates indenting of file.

    . 7set ignorecase 4 7set ic

    %his option is useful 2hen you are searhing for a pattern then all the upperase haraters aremapped onto lo2erase and thus pattern searhing 9eome muh easier.

    /. 7set 'ra+)argin F 4 7set ') F

    $efines the right margin of the sreen( 2ith the a9ove setting no2 the sreen 2ould 2rap after the& olumns instead of the default ,< olumns.

    -. 7set 'indo' F #$

    $efines the num9er of lines to 9e displayed in the 2indo2. $efault 2indo2 si;e 2ill 9e 04 lines persreen.

    8. 7set errorbells 4 7set eb

    vi al2ays 9eeps to alert users of the possi9le errors. If possi9le( vi displays anerror message ininverse video instead of 9eeping.

    9. 7set )esg

    %his option is onerned 2ith the 2rite permissions of your terminal. =ther users may 9e sendingmessages via 'riteommand to your terminal. If your terminal has no message permission( thenthe message sent through 'rite ommand 2ill not 9e flashed on your sreen 2hile you are2or/ing on some important file.

    Unix User uide -ohammad Jaheer 8hmed 1

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    23/101

    1$. 7set 'arn

    Warn if there has 9een L!o 2rite sine last hangeXM 9efore a shell esape ommand.

    Custo)iing vi

    -any of the harateristis of vi an 9e altered 9y user. %he priniples underlying this type ofustomi;ation 2ill 9e explored in the follo2ing

    %he .exrc file

    Every time you invo/e vi> a file alled .exrc is searhed for in your home diretory. If it exists thenit is read and interpreted and vi ustomi;e itself aording to the ontents of the .exrcfile.

    8ll options( li/e )a+ abbr> setmay 9e speified in this file and they 2ill 9eome permanent in all

    your vi sessions. 'etQs ma/e a.exrc file 2ith ontents asset nuset sho2modeset 2rapmargin Y &set 2indo2 Y 0 the name of the file( and its identifiation num9erinum9er5. If a diretory has supposed five files( there 2ill 9e five suh entries in the diretory file.

    (evice "iles

    %he definition of a file has 9een 9roadened 9y Unix to onsider even physial devies as files. %hesedevies inlude printers( tapes( floppy drives( hard dis/s and terminals. It is an advantage you donQtneed any physialommand or funtion to use suh file. %he devie file is speial in the sense thatany output direted to it 2ill 9e refleted on speifi IK= devie assoiated 2ith filename. )ene(2hen you enter a ommand to print a file( you are direting the file output to the file assoiated 2iththe printer. %he /ernel ta/es are of this mapping speial filenames to their devies.

    "ile a)ing Conventions

    )ere are the file naming onventions( 2hih should 9e ta/en are of 2hile naming a file.

    8 filename an onsist of up to 0&& haraters.

    Filenames may or may not have extensions.

    Filenames an onsist of any 8SII harater exept the K.

    Even ontrol haraters or nonprinta9le haraters an 9e inluded in a filename.

    File names are ase sensitive. So files FI'E1 and file1 are different files.

    Speial haraters li/e [( ^(?(G(C has speial treatment so avoid using suh haraters.

    It is advisa9le not to use >hyphen5 in the 9eginning of the filename.

    Files 2ith a . dot5 in the 9eginning are hidden files.

    Unix User uide -ohammad Jaheer 8hmed 00

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    28/101

    %or&ing 'ith (irectories

    8ll the files in U!I" are related to one another. %he file system in U!I" is a olletion of all theserelated files organi;ed in a hierarhial struture.

    %he impliit feature in every U!I" file system is that( the parentage diretory is the rootdiretory.rootis atually a diretory file( and it has a num9er of su9 diretories under it. %hese su9 diretories(may have more su9 diretories and other files under them. %hus in this parenthild relationships( the

    parent is al2ays a diretory.

    Chec&ing our current director

    When 2e log in( 2e are plaed in a speifi diretory of the file system. %his diretory is /no2n asour urrent diretory. We an move around from one diretory to another( 9ut at any point of time(

    only one diretory is urrent.

    8t any time( to /no2 the urrent 2or/ing diretory +'dommand an 9e used.

    $(5d.-ome.u'er1

    What 2e see is the pathname( 2hih is simply a seBuene of diretory names separated 9y slashes.

    %his pathname sho2s the loation 2ith referene to the root diretory.

    Changing (irectories

    We an move around in the U!I" file system 9y using the cdhange diretory5 ommand. When cdommand used 2ith an argument( it hanges the urrent diretory to the diretory speified as theargument.

    For instane( if suppose you are in the login diretory and you 2ant to move to the +roHectdiretory2e an do so 9y#

    $cd (roect$ (5d

    .-ome.u'er1.(roect

    %hus 2ith the a9ove example 2e sa2 that 2hen 2e are hanging the 2or/ing diretory to proHetand then give the +'dommand( 2e see the present 2or/ing diretory as proHet.

    %hus 2e an say that cdommand is used to hange the present 2or/ing diretory. )ere 2e sa2 d2ith an argument( 9ut 2hen d is used 2ithout an argument it ta/es us to the login diretoryirrespetive of the level of diretory 2e are 2or/ing in.

    For instane( suppose 2e are presently 2or/ing in 3home31ser%3proect3boo-s diretory and 2e2ant to shift to the login diretory( 2e an do so 9y(

    Unix User uide -ohammad Jaheer 8hmed 03

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    29/101

    $ (5d.-ome.u'er1.(roect.3ook'$ cd.-ome.u'er1

    2isting "iles

    We already sa2 that( ls ommand is used to o9tain a list of all the files in the urrent diretory. lsalso provides us 2ith many options to have a listing of files in different interative 2ays.

    When you have many files( it 2ould 9e 9etter to have the filenames displayed in multiple olumns.'inux does it 9y default( 9ut S= U!I" needsxoption to produe multi olumnar output#

    $ l' 7calculator)'- color1 color2 count(at)'-

    cron cron1 cut1 cut2de(t)l't de(t)l't de(t)l't1 de(t)l't2de(t)l't+ de(t)l't1 ed)-u( em()l'tem()l't1 em()l't2 em()l't+ em(1%4!$

    %o list various file attri9utes of the files in the diretory ls have l long5 option. It gives the longlisting( stating the permissions( lin/s( o2nername( groupname( si;e of the file and last modifiationdetails of the files in the urrent diretory.

    $l' Olr5rr 1 ruc-i training 122 6(r + 1+:42 calculator)'-

    r5r5r 1 ruc-i training 2+ 6(r 2% 12:+% color1r5r5r 1 ruc-i training 20 6(r 2% 12:+% color2r5rr 1 ruc-i training 101 6(r + :1 count(at)'-r5rr 1 root root 2+ 6(r 2% 14:+0 cronr5rr 1 ruc-i training 2+ 6(r 2% 14:42 cron1r5r5r 1 ruc-i training 1!4 6(r 2% 12:+% cut1r5r5r 1 ruc-i training 214 6(r 2% 12:+% cut2r5r5r 1 ruc-i training 124 6(r 2% 12:+% de(t)l't

    )ere( the first olumn states various permissions to various user levels 2hih 2e an understand alittle later( the next olumn explains the num9er of lin/s assoiated 2ith a file. at present lin/ an 9eeunderstood as the synonym to the num9er of filenames of that file in the system. In more detail andatual usage of lin/s 2ill 9e seen later. %he third and the fourth olumn( displays the user and groupof the file. the next olumn sho2s the si;e of the file in 9ytes( i.e.( the amount of data it ontains. 8nimportant thing to note is that it sho2s only the harater ount of the file and not the si;e of thedis/ spae it oupies. %he next olumn displays the last modifiation time of the file to the nearestseond. %he last olumn then displays the names of the files in alpha9eti order.

    ls normally does not sho2 all the files in the diretory( normally the hidden files that may 9e reated9y the user or some files in the login diretory. %o vie2 all files in your diretory( inluding even thehidden files there isa all5 option#

    Unix User uide -ohammad Jaheer 8hmed 04

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    30/101

    $ l' Oa). cut1 em(1%4!)). cut2 em(1%!!)3a'-&-i'tory de(t)l't file&te't)e7rc de(t)l't find)le'' de(t)l't1 menu)'-)le''rc de(t)l't2)(rofile de(t)l't+

    %here are many more important options of ls( 2hih are disussed 9elo2#

    4+tion (escri+tion

    x $isplays multi olumnar output.F -ar/s exeuta9les 2ith G and diretories 2ith K.r Sorts files in reverse order.a $isplays all files( inluding the hidden files.* *eursive listing of all files in the su9 diretories.

    l 'ong listing of files sho2ing various file attri9utes.d Fores listing of diretories.i Sho2s inode num9er of the file.

    6a&ing (irectories

    Using m/dir ommand follo2ed 9y the name of the diretory to 9e reated an reate diretories( inUnix.

    $ mkdir account'

    %o he/ 2hether the diretory has 9een reated you an use the ls ommand.

    $l' calculator)'-cronde(t)l't

    9oo/sards

    accounts

    8 num9er of su9diretories an 9e reated 2ith one m/dir ommand#

    $mkdir account' 'ale' marketing

    %his 2ill reate three different su9 diretories in the urrent diretory in one stro/e. Unix systemalso allo2s reating diretory hain 2ith a single m/dir ommand#

    $ mkdir (roect' (roect'.li3rary (roect'.-otel

    %he a9ove ommand reates a diretory hain( i.e.( it reates a diretory proHets and under proHets

    Unix User uide -ohammad Jaheer 8hmed 0&

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    31/101

    it reates t2o su9diretories as li9rary and hotel.

    e)oving (irectories

    %he r)dirremove diretory5 ommand removes diretories.

    $ rmdir account'

    %o he/ the removal of the diretory( ls ommand an 9e used#

    $ l'calculator)'-cronde(t)l't3ook'

    card'

    'i/e( m/dir many diretories an 9e removed together in one shot#

    $ rmdir account' 'ale' marketing

    %his 2ill remove the three diretories aounts sales and mar/eting at one shot.

    rmdir( li/e m/dir also helps to remove a diretory hain #

    $ rmdir (roect'.li3rary (roect'.-otel (roect'

    When you delete a diretory hain( a reverse logi has to 9e applied.

    =ne point to note here is that the diretory gets deleted only if the diretory is empty. %o forefullyremove the diretory even if it is not empty( 2e have to use#

    $ rm Or (roect'

    %he )&dirand r)dir ommands 2or/ only if those diretories o2ned 9y the user.

    "ile T+es

    Even though there are 9asially three types of files ordinary( diretory and devie5( you may oftenreBuire /no2ing more a9out these files. For instane( it is possi9le that t2o files may 9e of the sametype e.g. ordinary5( 9ut one may ontain text( 2hile other ontains exeuta9le ode. U!I" providesthe file ommand to determine the type of file( espeially of an ordinary file. %he ommand is simpleto use( and you an use it 2ith one or more filenames#

    $ file de(t)l't de(t)l't: a'cii te7t

    When the ommand is used to apply to all in the su9Het diretory( you 2ill see an informative list

    Unix User uide -ohammad Jaheer 8hmed 0+

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    32/101

  • 8/13/2019 Unix Reference Manual

    33/101

    %or&ing 'ith "iles

    %ill no2 2e sa2 ho2 cat ommand an 9e used to display the ontents of the file. 7ut( there 2as adra29a/ in using catto vie2 the ontents of the files. If the file is a multipage one( then cat2illonly sho2 the last sreen full of page 2hile the previous pages 2ill sroll do2n. %o vie2 a file page2ise 2e have )oreommand in Unix.

    *ausing the 4ut+ut

    If a file is too large for its ontents to fit in one sreen( it 2ill sroll off your sreen 2hen you vie2 it2ith cat.%his happens so fast that( 9efore you hit 5ctrl 6 s7 to stop it( Buite a 9it of the output2ould have srolled off.

    )ore ommand allo2s a user to vie2 a file( one sreen at a time. %o vie2 a file )sgB> enterthe ommand 2ith the filename#

    $ more m'g

    ou 2ill no2 9e a9le to vie2 the ontent of file )sgB> one sreen at a time. 8t the 9ottom of thesreen( you 2ill also see the filename and the perentage of the file that has 9een vie2ed

    m'g ?0P@

    ou an use the NE!%E*O and the NS68E78*O /ey to move to the next pages. %o Buit the)oreprogram( simply press B Buit5

    -ore also 2or/s 2ith multiple filenames#

    $ more c-a(1 c-a(2 c-a(+

    %he other options on the more ommand#

    4+tion

    (escri+tion

    7n S2ithes to the next file

    7+ S2ithes to the previous file

    7f $etermines the filename as 2ell as the linenum9er.

    Changing "ile *er)issions

    Issuing ls l ommand one again to list files of the urrent diretory along 2ith various fileattri9utes. We disussed that the first olumn sho2s the permissions on the file. =9serve that these

    permissions ta/e the general form

    Unix User uide -ohammad Jaheer 8hmed 0,

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    34/101

    Jr'xr'xr'x

    r indiates read permissions i.e. the file an 9e read 9y using say( at ommand.' indiates 2rite permissions i.e. you an modify the file.x indiates exeute permissions i.e. the file an 9e exeuted as a program.

    chmod co))and

    ch)od ommand is used to set the three permissions for all the three ategories of users of files. Itan 9e used only 9y the o2ner of the file and uses the follo2ing syntax#

    ch)odNcategoryO NoperationO NpermissionO N*ilenames5O

    %he ommand ta/es as its argument an expression( 2hih om9ines the ategory( the operation andthe type of permission( 9y ma/ing use of suita9le a99reviations. %he a99reviations used for the threeelements of the expression are sho2n in the follo2ing ta9le#

    Categor 4+eration 5ttribute

    u > user V assign permission r > read permissions

    g > group remove permission 2 > 2rite permissions

    o > others Y assign a9solute permission x > exeute permissions

    a > all

    4'nerKuser %he user 2ho reates a file.%he o2ner of a file an perform any operation on that file e.g. opying the file(deleting the file( editing the file( et.

    Grou+ -em9ers of a group 2ho need to aess eah otherQs files.%o 9e a mem9er of a group an entry has to 9e made in the KetKgroup file.

    4thers Users 2ho are neither o2ners of a file nor mem9ers of a group to 2hih the o2ner9elongs.

    ch)od allo2s us to set permissions in t2o 2ays( i.e.(

    S)bolic notation

    4ctal notation

    %he Sym9oli !otation

    For instane( is you 2ant to ma/e a file exeuta9le then#

    $ c-mod uQ7 note'

    )ere( the ommand assigns V5 exeuta9le x5 permission for the user x5. !o2 if you 2ant everyoneto have permission to exeute file notes( then #

    $ c-mod ugoQ7 note'

    Unix User uide -ohammad Jaheer 8hmed 0

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    35/101

    %his tas/ may also have 9een aomplished 9y Hust #

    $ c-mod aQ7 note'=R

    $ c-mod Q7 note'

    Where( a all5 is the om9ination of u(g(o ategories ta/en all together and if the ategory is notspeified it is all 9y default.

    ch)od also aepts more than one filename in the ommand line. If you need to assign the same setof permissions to a num9er of files then multiple files an 9e given together as(

    $ c-mod uQ7 note'1 note'2 note'+

    *evo/ing of permissions is done 9y > operator. %o remove the read permission from others#

    $ c-mod r note'1

    8s 2e an grant rights to multiple ategories of users in one shot( 2e an even revo/e them. We aneven om9ine the grant and revo/e option in a single ommand line delimited 9y ( omma5.

    $ c-mod u7goQr note'1

    ch)od even allo2s to grant or revo/e multiple permissions together.

    $ c-mod Q7r note'1

    With ch)od 2e an even grant permissions a9solutely 9y Y eBual5 operator.

    $ c-mod u7 note'1

    %he =tal !otation

    In this notation( ch)odta/es a numeri argument that desri9es 9oth the ategory( as 2ell as thepermission. %he notation ta/es the form of an otal representation of the permissions( and is assignedli/e this#

    When there is more than one permission assoiated 2ith a partiular ategory( the respetivenum9ers are added.

    For instane( if a file has read and 2rite permissions for the o2ner( the otal representation of theo2nerQs permission 2ill 9e 11

  • 8/13/2019 Unix Reference Manual

    36/101

    only exeuta9le permission to the others.

    $ c-mod /1 note'1

    %hus no2( it is easy to understand that the expression signifies all permissions for all ategories(2hile ooo indiates a9sene of all permissions for all ategories.

    $iretory 6ermissions

    %ill no2 2e sa2 file permissions( 2hih 2as restrited only to the ordinary files. )o2ever(diretories also have their o2n permissions( and the signifiane of these permissions differ from thatof ordinary files. It is possi9le that a file anQt 9e read and 2rite even though it has read permission(and an 9e removed even 2hen it is 2rite proteted. 8tually( read and 2rite aess to an ordinaryfiles set 2ith ch)odare also influened 9y the permissions of the diretory( they are ontained in.

    'et us first see the default diretory permissions#

    $ l' Ol Od (roect'dr57r7r7 2 ruc-i training 124 May 1 1:!% (roect'.

    Every set of diretory permissions has the das the first harater of the three tiered seBuene. Wesee that the o2ner has all the three permissions( and group and others have read and exeuta9le

    permissions only.

    'et us no2 try to understand 2hat does these permissions really mean for a diretory. %he readpermission for a diretory means that the list of filenames stored in that diretory is aessi9le. I.e.(for instane if the read permission from a diretory is removed( lsommand 2ill not sho2 the listingof the files in the diretory. It 2ill rather prompt something that Lpermission denied to aess the

    diretoryM. )o2ever( that does not mean that the file annot 9e read. It an al2ays 9e read if you/no2 the filename.

    ou sa2 that o2ner has 2rite permission for a diretory. %hat does not although mean that you anedit the diretory file. If that 2as possi9le then any user ould destroy the integrity of the Unix filesystem. %he a9senes of 2rite permission for a diretory implies that you are not permitted to reateor remove files in it. I.e.( remove the 2rite permission from the diretory and then try to reate orremove a file in that diretory. System 2ill prompt 2ith Lpermission deniedM.

    Exeution privilege of a diretory means for a diretory means that a user an Lpass throughM thediretory in searhing for su9diretories. For instane( to issue the ommand

    $ cat .u'r.kumar.(roect'.em()'-

    ou need to have exeuta9le permission for eah of the diretories involved in the ompletepathname. $enial of exeuta9le permission for any of these diretories means that it annot 9esearhed for the name of the next diretory. It also means that you anQt even s2ith to thatdiretory 2ith the cd ommand.

    'i/e for regular files( diretory permissions are extremely important 9eause system seurity isdependent on them.

    Unix User uide -ohammad Jaheer 8hmed 31

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    37/101

    Creating "iles using catEarlier 2e sa2 ho2 an 2e display the ontents of the fileusing cat. cat is also useful for reating afile. %o reate a file using at( enter the at ommand( follo2ed 9y the O the right hevron5

    harater and the filename.

    $ cat > file1

    When the ommand is terminated 9y pressing NE!%E*O /ey( at 2aits to ta/e input from the user.Enter the text reBuired follo2ed 9y NE!%E*O to move to next line( and finally press NtrlVdO tosignify the end of input to the system.

    $ cat > file1 T-i' i' my fir't file)T-i' i' t-e te't file for cat)

    $&

    %o vie2 the ontents of the file reated you an no2 use at#

    $ cat file1T-i' i' my fir't file)T-i' i' t-e te't file for cat)$&

    Co+ing a file%he c+ opy5 ommand opies a file or a group of files. It reates an exat image of the file on thedis/ 2ith a different name. %he syntax reBuires atleast t2o filenames to 9e speified in the ommandline( so that the first is opied to the seond. %o opy the file hap1 to file1#

    $ c( c-a(1 file1

    If the destination file does not exist( it 2ill 9e reated 9efore opying ta/es plae and if it exists it2ill simply 9e over2ritten 2ithout any onfirmation. Aust he/ 2ith ls ommand 2hether or not thefile exists.

    If there is only one file to 9e opied( the destination an 9e either an ordinary or a diretory file. Forexample( to opy the file cha+1to the diretory subHect#

    $ c( c-a(1 'u3ect

    If there is a group of files to 9e opied( they all an 9e speified in the ommand line( 9ut reBuiresthe destination file to 9e a diretory. For instane( to opy various hapters to this text to the su9Hetdiretory provided it exists5 #

    Unix User uide -ohammad Jaheer 8hmed 30

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    38/101

    $c( c-a(1 c-a(2 c-a(+ 'u3ect

    8ll the files after opying 2ill retain the original names. It is not possi9le to speify target filenamesfor eah file( and opying ta/es plae only if the su9Het diretory exists. *emem9er that if these filesare already resident in su9Het( then they 2ill 9e simply over2ritten

    Useful options of the opy ommand#

    Ji onfirms from the user 9efore over2riting an existing file.

    Jr *eursively opies all files and su9diretories to a ne2 diretory

    6oving or ena)ing files

    %he )vmove5 ommand simply renames a file or a group of files. It does not reate a opy 9utmerely renames it. %he syntax of the )vommand is similar to that of c+ommand exept5. %o

    rename hap1 to man1#

    $ m c-a(1 man1

    If the destination file does not exist it 2ill 9e reated. )o2ever( if it exists the destination file isover2ritten.

    'i/e p( a group of files an 9e moved 9ut only to a diretory#

    $ m c-a(1 c-a(2 c-a(+ (rog'

    %here isi option of mv( 2hih 9ehaves similarly as that of p. %his option onfirms 9efore renaming

    a file.

    e)oving files

    Files an 9e deleted 2ith r)remove5 ommand. It normally operates silently and has( therefore( to9e used 2ith aution. When invo/ed 2ithout options( it deletes the files speified in the ommandline. %he follo2ing ommand deletes three files in one shot#

    $ rm c-a(1 c-a(2 c-a(+

    rm 2ill not normally remove a diretory( 9ut it an remove files from one. ou an even remove filesfrom a diretory hain 2ithout atually hanging the diretory to the one#

    $ rm 'u3ect. c-a(1 'u3ect. c-a(2

    Some useful options of the r)remove5 ommand#

    Ji 6rompts the user 9efore removing for onfirmation.

    Jr $eletes the diretory irrespetive of that it is empty or not.

    L $eletes all the files a9ruptly 2ithout as/ing the user

    Unix User uide -ohammad Jaheer 8hmed 33

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    39/101

    Counting 2ines> 'ords and characters

    U!I" features a universal 'ord counting D'c

  • 8/13/2019 Unix Reference Manual

    40/101

    Ho9 has 9een su9mitted. %he l+ommand notifies the Ho9 num9er( 2hih you an later aess 2ithother ommands. %he file is not atually printed at the time ommand is invo/ed( 9ut later(depending upon the num9er of Ho9s lined up in the Bueue. Several users an print their files in this2ay 2ithout onflit. %here is a demon a proess 2hih runs periodially5( 2hih monitors thisBueue and prints eah Ho9 in turn. %he output from the printer 2ill 9e hard opy of the atual file(

    preeded 9y a title page mentioning the user name( reBuestid and date.

    ou an print more than one opy 2ithn num9er5 option. %he follo2ing seBuene prints threeco+iesof the file hap1

    $ l( On+ c-a(1Reue't id i' +2!

    %here are t2o related ommands( 2hih are generally used 2ith this ommand. %he l+statommand( 2hen used 2ithout options( sho2s you status of all the Ho9s su9mitted for spooling. 8ndif you 2ish to anel any of the su9mitted Ho9s( you an cancel2ith the Ho9 num9er as argument.

    %hus( to anel the printing of file cha+1( 2hih you Hust su9mitted for printing three opies( youshould simply use cancel 3#,. !ote you an see all the Ho9s in the print BueueZ you an only anelthe Ho9s su9mitted or o2ned5 9y you.

    If there are multiple printers in the system( you an use d option 2ith the printer name to speifythe partiular printer.

    $ l( Odla'er file

    If you 2ant to print a partiular title header5 on eah page of the printing file( l+allo2s this 2ithtoption follo2ed 9y the title then#

    $ l( OtDSir't H-a(terD c-a(1

    Co)+aring "iles

    %here are some ommands( 2hih help us in omparing files and diretories. %hese ommands aremp( omm( and the diff ommand. %hey have 9een explained in detail 9elo2.

    %he cmpommand

    FreBuently( you may reBuire /no2ing 2hether t2o files are idential in all respets so that one ofthem an 9e deleted. %he c)+ompare5 ommand is used to ahieve this tas/. When used 2ithoutoptions( it uses t2o filenames as arguments( and displays the differenes on the terminal#

    $ cm( c-a(1 c-a(2

    hap1 hap0 differ# har (line1

    %he t2o files are ompared 9yte to 9yte( and the loation of the first mismath is ehoed to thesreen( 2hen invo/ed 2ithout options doesnQt 9other a9out su9seBuent possi9le mismathes( 9utsimply a9orts 2ith the message sho2n. In the a9ove ase( the first mismath 2as ninth harater of

    Unix User uide -ohammad Jaheer 8hmed 3&

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    41/101

    the first line.

    If the t2o files are idential( then mp displays no message( 9ut simply returns the [ prompt.

    %he l list5 option gives a detailed list of the 9yte num9er and the different 9ytes in otal for eah

    harater that differs in 9oth the files. 7efore omparing the t2o files temp1 and temp0. 'et see2hat they ontain#

    [ at temp1pBrsxy;

    [ at temp0pBts2xy[

    [ mp >l temp1 temp03 1+0 1+4+ 1< 1+ 11 1)ail> el) and+ine.

    Unix User uide -ohammad Jaheer 8hmed 4

  • 8/13/2019 Unix Reference Manual

    46/101

    General *ur+ose Utilities

    date7 (is+la Sste) (ate8s 2e already sa2 that the date ommand provides us 2ith urrent date and time of the system.

    [ date%ue -ay 1 1#11#0, IS% 0

  • 8/13/2019 Unix Reference Manual

    47/101

    %his produes a three olumnar output. %he first olumns sho2s the login names of the usersurrently logged in. the seond olumn sho2s the devie names of their respetive terminals and thethird olumn sho2s the date and time of logging in.

    We an display the olumn headers 2ith the >) option. %he 2ho ommand( 2hen used 2ith the

    arguments LamM and LiM( displays a single line of output only( i.e.( the login details pertaining to theuser 2ho invo/ed this ommand#

    $ 5-o am iu'er2 tty(1 May 1 1+:44

    passwd7 Change our *ass'ord

    %he +ass'd ommand is used to hange pass2ords.+ass'd 2hen invo/ed 9y an ordinary user( as/sfor the old pass2ord( after 2hih it demands the ne2 pass2ord t2ie.

    $ (a''5dH-anging (a''5ord for u'er1=ld (a''5ord:Enter t-e ne5 (a''5ord ?minimum of ! ma7imum of 0 c-aracter'@Ilea'e u'e a com3ination of u((er and lo5er ca'e letter' andnum3er')Ne5 (a''5ord:Reenter ne5 (a''5ord:Ia''5ord c-anged)

    For seurity reasons( the system does not allo2 you to hoose the old pass2ord( nor an thepass2ord 9e 2holly alpha9eti or 2holly numeri. Unix system prefers for the most unmeaningpass2ord( so that others annot guess it. Unix system does not aept any ditionary 2ord as thepass2ord.

    When you enter the pass2ord( the string is enrypted 9y the system( and the enryption is stored inthe file KetcKshado'.

    Unix User uide -ohammad Jaheer 8hmed 40

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    48/101

    The Shell

    %he shell is the interfae 9et2een the user and the Unix system. 'i/e the ommand proessors ofother operating systems( the shell proesses the instrutions that you issue to the mahine. It is a

    program that starts 2hen you log in( and terminates 2hen you logout. 7ut unli/e other U!I"ommands( it is there all the time( indiating its presene 9y the familiar [ prompt. %he point has to

    9e ta/en seriously 9eause the shell program starts exeuting the moment a user logs in. It resem9lesthe role played 9y co))and.co)in -S$=S( though the apa9ilities of the shell far out2eigh thoseof co))and.co). %he uniBue thing a9out the shell is that( 2hile it is tehnially a U!I" ommanditself( its main Ho9 is to aept and interpret user reBuests( i.e. other U!I" ommands. %he shell evenhas a programming apa9ility.

    =ne of the funtions of the shell is to 2ait for input from the user. 8nything /eyed in through the/ey9oard( and at the [ prompt( is atually the input to the shprogram. With this input( it performs aseries of proessing tas/s( interating 2ith the /ernel 2hen neessary. 8fter the Ho9 is omplete( itreturns to its 2aiting role( to start the next LyleM. %he shell typially performs the follo2ingativities in eah yle#

    It issues the [ prompt( and 2aits for you to enter a ommand.

    8fter a ommand has 9een entered( the shell sans the ommand line for some speial haraters andthen re9uilding the ommand line after proessing is omplete.%he ommand is then passed on the/ernel for exeution( and the shell 2aits for its ompletion.%he [ prompt appears( and the shell 2aitsfor you to enter the next ommand.

    When there is no input from the user( the shell is to 9e sleeping. %his it indiates 9y the [ prompt(meaning that you are no2 free to enter any text. It 2a/es up 2henever a user enters some haratersthrough the /ey9oard( and presses the NEnterO /ey sleeping( 2aiting and 2a/ing are aepted termsin U!I" parlane5. %he produtive 2or/ of the shell 9egins after it hasaepted the haraters thatare /eyed in.

    edirection8ll ommands ta/e up some input either from a file or standard input i.e.( /ey9oard5 and send theoutput to the standard output i.e.( terminal5. %hese ommands an operate effetively on a harater

    stream( 2ithout really /no2ing the soure stream. Similarly( they also 2rite the output in the form ofa harater stream( 2ithout /no2ing the destination.

    8 ommand remains ignorant a9out the soure and the destination of these streams( 9eause theshell sees that the streams are handled properly. %he default soure of the stream is the /ey9oard.%his stream is alled as the standard input to the ommand. It is the tas/ of the shell to set up theonnetion 9et2een the ommand and the /ey9oard.

    %he default destination of the output of the harater stream is the terminal. %his is alled thestandard output of the ommand. )ere( again it is the tas/ of the shell to set onnetion 9et2een theommand and the terminal so that the output is displayed on the sreen.

    Unix User uide -ohammad Jaheer 8hmed 43

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    49/101

    Standard In+ut

    Sine the ommand onsiders its o2n input as a stream( it is neessary to /no2 the possi9le soures

    of this stream. %his stream an ome from#

    %he standard input( i.e. the /ey9oard

    8 file

    We already sa2 that the ommands li/e at and 2 used filenames as the arguments. )ere( thus theinput 2as provided from a file. 8lthough( 9oth of these ommands have 9uilt in feature of ta/ingstandard input too. For instane( 'c ommand 2hen invo/e 2ithout any argument( i.e.( filenamethen it 2aits for input from standard input and evaluate the ommand on the 9asis of the text insertedfrom the /ey9oard.

    $5c" am a 5c command" count c-aracter' 5ord' and line'Wit- o(tion' " can al'o make a 'electie count

    + 2 1+

    8 rediretion feature is availa9le in the shell so that you an hoose to ta/e the standard input from afile rather than the /ey9oard. )ere( then the shell sets the onnetion using the metaharater N lefthevron5#

    $ 5c < infile+ 2 1+

    In 9oth the a9ove ases( the input 2as from the standard input. In the first ase( the shell assignedthe default file( i.e.( the /ey9oard( as the soure stream 2hile in seond ase it redireted the streamto ome from a dis/ file.

    Important point to note that 2hen 2e give filename as argument to the ommand 'c> the output issimilar to the a9ove ase 9ut not idential#

    $ 5c infile

    + 2 1+ infile

    )ere( the output remains the same( exept that in the a9ove ase filename is also displayed 2hile inthe former ase filename 2as not displayed. %he reason 9ehind this differene lies in the fat that inthe former ase( the file 2as opened 9y the shell so the ommand does not /no2 the plae from2here the input stream is oming from( rather than ommand in the latter ase 2here ommands 'cis 2ell a2are of the input file.

    Unix User uide -ohammad Jaheer 8hmed 44

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    50/101

    Standard 4ut+ut

    'i/e standard input( the standard output stream also has similar destinations( i.e.(

    %he standard output( i.e. the terminal

    8 file

    %ill no2( 2e sa2 that the output of the ommands li/e 'c or cator any other ommand 2asdisplayed on the terminal( i.e.( the standard output.

    $ 5c infile+ 2 1+ infile

    !o2 there may arise a reBuirement 2hen 2e do not 2ant the output to 9e displayed on the sreen(rather it an 9e stored in a file and vie2ed later. 8lso may 9e a ase if 2e 2ant to vie2 the output ofsome ommand many times( rather than firing that ommand every time( 2e an store the output ofthat ommand in a file for further usage. In that ase Unix provides us a feature to rediret theoutput to a file from the default standard output.

    ou an replae the default destination 9y any file 9y using the A right hevron5 metaharater(follo2ed 9y the filename#

    $ 5c infile > ne5file$ cat ne5file

    + 2 1+ infile

    %his sends the 2ord ount of infile to ne2file. !othing appears on the sreen exept the return of theprompt. If the output file does not exist( shell 2ill first reate it 9efore exeuting the ommand.

    )ere( even though the ommand 'copened the file infile for reading 9ut it 2as the shell that openedthe destination file to 2rite the output.

    ou an also append to a file( instead of over2riting( 9y using the OO metaharater. So( if forinstane you 2ant to append the output of 'hoommand to ne2file( 2hih already ontains theoutput of the 'c ommand.

    $ 5-o >> ne5file

    Connecting Co))ands 'ith +i+es

    Standard input and standard output onstitutes t2o separate streams that an 9e individuallymanipulated 9y the shell.

    ou /no2 the 'ho ommand produes a list of users. If you 2ant to ount the num9er of users then2hat you 2ill do is( store the output of 'hoommand in a file say 'hofileand then apply 'c lommand to ount the num9er of lines.

    %he pipe feature of Unix an aomplish this t2ostep 2or/. It 9asially+ helps in onneting t2o or

    Unix User uide -ohammad Jaheer 8hmed 4&

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    51/101

    more ommands effetively( i.e.( output of one ommand an effetively 9e the input for other andvie versa.

    So the a9ove Buery an 9e easily sorted 9y#

    $ 5-o , 5c Ol

    )ere the output of ,ho has 9een passed diretly to the input of ,c( and ,hois said to 9e piped to,c.When a seBuene of ommands is om9ined together in this 2ay( apipelineis said to 9e formed.It is the shell that sets up this interonnetion( and the ommands have no /no2ledge of it.

    In a pipeline( all programs run simultaneously. !o2( you an say that the standard input anoriginate from the /ey9oard( a file and a pipe. Similarly( the standard output an 9e redireted to afile( as 2ell as fed to a pipe.

    The teeCo))and

    Unix provides a feature 9y 2hih you an save the standard output in a file( as 2ell as display it onthe terminal. it is made possi9le 9y the teeommand. %he additional feature it possess( apart fromthe other t2o disussed is that it 9rea/s up the input into t2o omponentsZ one is saved in a file andthe other is onneted to the standard output.

    ou an use tee ommand to save the output of the 'ho ommand in a file( as 2ell as display it onthe sreen#

    $ 5-o , tee u'er)l'troot tty1 May 1 2:

    u'er1 tty( May 1 2:1u'er2 tty(1 May 1 2:10

    =ne hannel is saved in user.lst( 2hile the other is displayed on the standard output.

    Unix User uide -ohammad Jaheer 8hmed 4+

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    52/101

    "ilters

    %his hapter inludes a group of ommands( eah of 2hih aepts some data as input( performssome manipulation on it( and produes some output. Sine they perform some filtering ation on thedata( they are appropriately alled*ilters.

    *aginating "iles

    %he+r ommand prepares a file for printing 9y adding suita9le headers( footers and formatted text.%he ommand is invo/ed 2ith filename as the argument#

    $ (r em()l't

    1!2 2:1 em()l't Iage 1

    22++,a)k '-ukla,g)m,'ale',12.12.!2,/%0/,ai '-arma,director,(roduction,+.12.!,2+/!,3arun 'engu(ta,director,(er'onnel,!.11.4,01/,c-anc-al 'ing-i,director,'ale',+.%.+0,/12/!,')n) da'gu(ta,manager,'ale',12.%./+,!/24/,anil aggar5al,manager,'ale',1.!.!%,!/!21,lalit c-o5dury,director,marketing,2/.%.4!,02+!/4,'ud-ir 6gar5al,e7ecutie,(er'onnel,./.4,!11,)k) agra5al,g)m,marketing,12.+1.4,%

    !/0,'umit c-akra3orty,d)g)m,marketing,4.1%.4+,/!42+,n)k) gu(ta,c-airman,admin,!.+.4,!4/21+,karuna ganguly,g)m,account',!././2,/+42%,ayant c-oud-ury,e7ecutie,(roduction,.%.!,/+212,'-yam 'ak'ena,d)g)m),account',12.12.!!,/2+4!,)3) 'a7ena,g)m,marketing,12.+.4!,0

    +r adds five lines of margin at the top and five at the 9ottom. %he header sho2s the date and time oflast modifiation of the file( along 2ith the filename and page num9er. 8s +r formats its input 9yadding margins and a header( it is often used 9efore printing.

    7y default( the page si;e is ++ lines( 2hih an 9e hanged 2ith the loption along 2ith the numeri

    argument #

    $ (r Ol 2 em()l't

    %his 2ill set the page si;e to 0 lines. When printing is done of very large files( you an instrut pr tostart printing from a speifi page. %his is done 2ith the 6-option( 2here -is an integer value #

    $ (r Q1 c-a(1

    %his 2ill start printing from page 1

  • 8/13/2019 Unix Reference Manual

    53/101

    -indiates the num9er of olumns.

    (is+laing the 0eginning of a "ile

    %he head ommand( as the name implies( displays the top of the file. ou an speify the part of thefile you 2ant to vie2( 9ut if it is used 2ithout the option( the first ten reords are displayed#

    $ -ead em()l't

    ou an speify a lineount and display( say( the first three lines of the file#

    $ -ead O+ em()l't

    0033a./ shu/lag.msales10K10K&0+

  • 8/13/2019 Unix Reference Manual

    54/101

    S+litting a "ile Merticall

    While head and tail are used to filter a file hori;ontally( you an even filter a file vertially 2ith thecut ommand. cut identifies 9oth olumns and fields.

    cut an 9e used to extrat speifi olumns from a file( say those signifying the name and designationin the file emp.lst #

    $ cat em()l't22++,a)k '-ukla ,g)m ,'ale' ,12.12.!2,/%0/,ai '-arma ,director ,(roduction,+.12.!,2+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,01/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!/!21,lalit c-o5dury ,director ,marketing ,2/.%.4!,02

    +!/4,'ud-ir 6gar5al ,e7ecutie,(er'onnel ,./.4,!11,)k) agra5al ,g)m ,marketing ,12.+1.4,%!/0,'umit c-akra3orty,d)g)m ,marketing ,4.1%.4+,/!42+,n)k) gu(ta ,c-airman ,admin ,!.+.4,!4/21+,karuna ganguly ,g)m ,account' ,!././2,/+42%,ayant c-oud-ury ,e7ecutie,(roduction,.%.!,/+212,'-yam 'ak'ena ,d)g)m) ,account' ,12.12.!!,/2+4!,)3) 'a7ena ,g)m ,marketing ,12.+.4!,0

    )ere( 2e see that the name starts from olumn num9er + and goes up to olumn num9er 00( 2hilethe designation data oupies olumns 04 through 30. cut provides 2ith c option for uttingolumns.

    $ cut Oc /2224+2 em()l'ta)k '-ukla g)mai '-arma director3arun 'engu(ta directorc-anc-al 'ing-i director')n) da'gu(ta manageranil aggar5al managerlalit c-o5dury director'ud-ir 6gar5al e7ecutie)k) agra5al g)m'umit c-akra3ortyd)g)m

    n)k) gu(ta c-airmankaruna ganguly g)mayant c-oud-ury e7ecutie'-yam 'ak'ena d)g)m))3) 'a7ena g)m

    cutuses a speial form for seleting a olumn from the 9eginning and upto the end of a line #

    $ cut Oc O+/2224+2!! em()l't

    %he expression && indiates olumn num9er && to the end of the line( and >3 is the same as 13.

    Unix User uide -ohammad Jaheer 8hmed 4

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    55/101

    cutalso provides us 2ith a feature to ut a file on fields rather then olumns. %his option is moreuseful in a sense that( a file may not neessarily ontain reords of same length. %o ut a file on fieldsyou need to use t2o options( i.e.( Jddelimiter5 for the field delimiter( andf field5 for speifyingthe field list. For instane if you 2ant to ut 0ndand 3rdfield then#

    $ cut OdD,D Of2+ em()l'ta)k '-ukla ,g)mai '-arma ,director3arun 'engu(ta ,directorc-anc-al 'ing-i ,director')n) da'gu(ta ,manageranil aggar5al ,managerlalit c-o5dury ,director'ud-ir 6gar5al ,e7ecutie)k) agra5al ,g)m'umit c-akra3orty,d)g)mn)k) gu(ta ,c-airman

    karuna ganguly ,g)mayant c-oud-ury ,e7ecutie'-yam 'ak'ena ,d)g)m))3) 'a7ena ,g)m

    *asting "iles

    What you LutM in the previous ommand an 9e pasted 9a/ 2ith the paste ommand. It is a speialtype of ommand that onatenates vertially( rather than hori;ontally.

    %o see the effet of paste( let us prepare t2o files having some portions of the same file. Forinstane( reate a file cut1 that ontains the name and designation field of file emp.lst and another filecut#>2hih ontain fields department and salary of the file emp.lst.

    $ cut OdD,D Of2+ em()l't > cut1$ cut OdD,D Of4/ em()l't > cut2

    'et us no2 paste the t2o files ut1 and ut0#

    $ (a'te cut1 cut2a)k '-ukla ,g)m 'ale' ,/ai '-arma ,director (roduction,3arun 'engu(ta ,director (er'onnel ,0c-anc-al 'ing-i ,director 'ale' ,/')n) da'gu(ta ,manager 'ale' ,!/anil aggar5al ,manager 'ale' ,!lalit c-o5dury ,director marketing ,02'ud-ir 6gar5al ,e7ecutie (er'onnel ,!)k) agra5al ,g)m marketing ,%'umit c-akra3orty,d)g)m marketing ,/n)k) gu(ta ,c-airman admin ,!4karuna ganguly ,g)m account' ,/+ayant c-oud-ury ,e7ecutie (roduction,/

    Unix User uide -ohammad Jaheer 8hmed &

  • 8/13/2019 Unix Reference Manual

    56/101

    '-yam 'ak'ena ,d)g)m) account' ,/)3) 'a7ena ,g)m marketing ,0

    )ere( it an 9e o9served that the t2o files have 9een pasted 9ut their ontents are separated 9y ta9 asthe default delimiter. $elimiter of your hoie an 9e seleted using thed delimiter5 option#

    $ (a'te OdD,D cut1 cut2a)k '-ukla ,g)m ,'ale' ,/ai '-arma ,director ,(roduction,3arun 'engu(ta ,director ,(er'onnel ,0c-anc-al 'ing-i ,director ,'ale' ,/')n) da'gu(ta ,manager ,'ale' ,!/anil aggar5al ,manager ,'ale' ,!lalit c-o5dury ,director ,marketing ,02'ud-ir 6gar5al ,e7ecutie,(er'onnel ,!)k) agra5al ,g)m ,marketing ,%'umit c-akra3orty,d)g)m ,marketing ,/

    n)k) gu(ta ,c-airman ,admin ,!4karuna ganguly ,g)m ,account' ,/+ayant c-oud-ury ,e7ecutie,(roduction,/'-yam 'ak'ena ,d)g)m) ,account' ,/)3) 'a7ena ,g)m ,marketing ,0

    Noining "iles

    %he Hoin ommand an 9e used to merge t2o merge t2o files. Eah line in a file is merged on the9asis of a field that has the same value in 9oth input files to reate one line in the output file. %he

    prereBuisite for Hoining t2o fields is that the field to 9e Hoined on must 9e sorted.

    Hoin ommand provides 2ith a num9er of flags. For instane if you 2ant to Hoin t2o files in 2hihthe ommon fields are at separate positions( then JHflag of Hoin an 9e used and aompanied 9y thedelimiter flag Jtto speify the ommon delimiter in the t2o files#

    $ oin OtD,D O1 4 O2 2 em()l't de(t)l't

    With the a9ove ommand you 2ill notie that all the fields of 9oth the files are Hoined. %o extratonly the seleted fields from 9oth files( 2e an use theo flag ofHoin7

    $ oin OtD,D O1 4 O2 2 O 1)2 1)4 2)1 em()l't de(t)l't

    %his 2ill display the 0ndand the 4thfield of emp.lst and 1stfield of dept.lst.

    Sorting Text "iles

    Unix sortperforms its usual sorting funtions( and 2hen invo/ed 2ithout options( the entire line issorted #

    Unix User uide -ohammad Jaheer 8hmed &1

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    57/101

    $ 'ort em()l't11,)k) agra5al ,g)m ,marketing ,12.+1.4,%1/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/22++,a)k '-ukla ,g)m ,'ale' ,12.12.!2,/2+4!,)3) 'a7ena ,g)m ,marketing ,12.+.4!,02+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,024/,anil aggar5al ,manager ,'ale' ,1.!.!%,!+212,'-yam 'ak'ena ,d)g)m) ,account' ,12.12.!!,/+!/4,'ud-ir 6gar5al ,e7ecutie,(er'onnel ,./.4,!

    Sorting starts 2ith the first harater of eah line( and proeeds to the next harater only 2hen theharaters in t2o lines are idential. sort( reorders a line in 8SII seBuene starting from the

    9eginning of the line.

    Sort also 2or/s on fields( and 9y default field separator is the spae harater.%he t option (follo2ed 9y the delimiter (overrides the default.%his lets you sort the file on any field.

    $ 'ort OtD,D Q1 em()l't22++,a)k '-ukla ,g)m ,'ale' ,12.12.!2,/24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!2+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,01/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/2+4!,)3) 'a7ena ,g)m ,marketing ,12.+.4!,012/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/+212,'-yam 'ak'ena ,d)g)m) ,account' ,12.12.!!,/+!/4,'ud-ir 6gar5al ,e7ecutie,(er'onnel ,./.4,!11,)k) agra5al ,g)m ,marketing ,12.+1.4,%

    %he argument V1 indiates that the sorting starts after the first field.%he delimiter is speified usingthe >t option.%o sort on the fifth field (you should use

    $ 'ort OtD,D Q4 em()l't

    %he sort order an 9e reversed using ther option.%he follo2ing seBuene reverses a previoussorting order #

    $ 'ort Or OtD,D Q1 em()l't11,)k) agra5al ,g)m ,marketing ,12.+1.4,%+!/4,'ud-ir 6gar5al ,e7ecutie,(er'onnel ,./.4,!

    +212,'-yam 'ak'ena ,d)g)m) ,account' ,12.12.!!,/12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/2+4!,)3) 'a7ena ,g)m ,marketing ,12.+.4!,01/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/2+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,024/,anil aggar5al ,manager ,'ale' ,1.!.!%,!22++,a)k '-ukla ,g)m ,'ale' ,12.12.!2,/

    Sort is also a fiter.%he sorted output an 9e redireted to a file using the o option.%he input nameand the output filenames an 9e the same.

    Unix User uide -ohammad Jaheer 8hmed &0

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    58/101

    $ 'ort Oo 'orted&em( Q2 em()l't

    %he a9ove ommand seBuene 2ill store the sorted output to a file named TsortedPempQ .

    $ 'ort Oo em()l't em()l't

    %he a9ove ommand seBuene 2ill store the sorted output to in the same file.

    ou an also /no2 2hether the file is sorted or not using thecoption.

    $ 'ort Oc em()l't$ &

    %he ommand 2ill return to the prompt silently if the file is sorted.Suppose you 2ant to he/ if thefile is sorted on the third field.

    $ 'ort OtD,D Q2 Oc em()l't

    If the file is sorted then it 2ill display an error message li/e

    'ort:di'order:2+/!,3arun 'engu(ta,director,(er'onnel,!.11.4,0

    Sorting on Secondar &e

    Sorting an 9e done on more than one field(i.e( you an provide a seondary /ey to sort.If the primary /ey is the third field(and the seondary /ey the seond field(you an use

    $ 'ort OtD,D Q2 O+ Q1 em()l't+212,'-yam 'ak'ena ,d)g)m) ,account' ,12.12.!!,/2+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,01/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/+!/4,'ud-ir 6gar5al ,e7ecutie,(er'onnel ,./.4,!22++,a)k '-ukla ,g)m ,'ale' ,12.12.!2,/2+4!,)3) 'a7ena ,g)m ,marketing ,12.+.4!,011,)k) agra5al ,g)m ,marketing ,12.+1.4,%24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/

    %his sorts the file 9y designation and 2ithin designation 9y name.J3 indiates stoppage of sorting

    after the third field(and V1 indiates its resumption after the first field.

    Sorting on Colu)ns

    ou an also speify a harater position 2ithin a field to 9e the 9eginning of sort.If you 2anted tosort the file aording to the yaer of 9irth( then

    $ 'ort OtD,D Q4)/ O4)0 em()l't1/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/11,)k) agra5al ,g)m ,marketing ,12.+1.4,%2+4!,)3) 'a7ena ,g)m ,marketing ,12.+.4!,0

    Unix User uide -ohammad Jaheer 8hmed &3

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    59/101

    +!/4,'ud-ir 6gar5al ,e7ecutie,(er'onnel ,./.4,!2+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,022++,a)k '-ukla ,g)m ,'ale' ,12.12.!2,/+212,'-yam 'ak'ena ,d)g)m) ,account' ,12.12.!!,/24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/

    @,./ signifies the starting sort postionthe seventh olumn of the fifth field.Similarly(,.8 impliesthat sorting should stop after the eigth olumn of the same field.

    u)eric Sort

    When you sort a file ontaining only num9ers( the result is strange.

    $ 'ort On numfile

    121222+

    %o override this pro9lem use thenoption .

    $ 'ort On numfile2+12

    122

    e)oving (u+licate 2ines

    %heu option lets you purge dupliate reords from a file.If you 2anted to find out the uniBuedesignation that our in the file#

    $ cut OdD,D f+ em()l't , 'ort Ou , tee de'ig)l'tc-airman

    d)g)mdirectore7ecutieg)mmanager

    2ocating e+eated 2ines7uniq

    Unix offers a speial tool >the 1ni ommand to handle dupliate reords. 1ni simply fethes one

    Unix User uide -ohammad Jaheer 8hmed &4

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    60/101

    opy of eah reord and 2rites it to the standard output. 1nireBuires a sorted file as input.

    onsider a sorted file dept.lst having dupliate reords.

    $ cat de(t)l't

    1,account' ,2+4+1,account' ,2+4+2,marketing ,24+/+,(er'onnael,2%!!+,(er'onnael,2%!!4,'ale' ,2114,'ale' ,211

    %o feth only one opy of eah reord (you an use (

    $ 'ort de(t)l't , uni1,account' ,2+4+

    2,marketing ,24+/+,(er'onnael,2%!!4,'ale' ,211

    uniB also allo2s to feth only the nonrepeated lines using theuoption.

    $ 'ort de(t)l't , uni Ou2,marketing ,24+/

    ou an also find out the no. of ourenes of the repeating lines using thec option.

    $ 'ort de(t)l't , uni Oc2 1,account' ,2+4+1 2,marketing ,24+/2 +,(er'onnael,2%!!1 4,'ale' ,211

    Transalting Characters 7 tr

    %he trommand is one Unix filter that manipulatesindi2id1al charactersin a file.It translatesharaters using one or t2o ompat expressions.

    Syntax#

    tr o(tion' e7(1 e7(2 'tandard in(ut)

    tr ta/es input only from the standard input Z doesnot ta/e a filename as an argument.7y default(ittranslates eah haraterin exp%to its mapped ounterpart in exp.%he first harater in the firstexpression is replaed 9y the first harater in the seond expression( and similarly for the otherharaters.

    ou an hange the delimiter in the emp.lst file from to _ #

    Unix User uide -ohammad Jaheer 8hmed &&

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    61/101

    $ tr ,D BD < em()l't1/Bc-anc-al 'ing-i Bdirector B'ale' B+.%.+0B/11B)k) agra5al Bg)m Bmarketing B12.+1.4B%2+4!B)3) 'a7ena Bg)m Bmarketing B12.+.4!B0+!/4B'ud-ir 6gar5al Be7ecutieB(er'onnel B./.4B!2+/!B3arun 'engu(ta Bdirector B(er'onnel B!.11.4B022++Ba)k '-ukla Bg)m B'ale' B12.12.!2B/+212B'-yam 'ak'ena Bd)g)m) Baccount' B12.12.!!B/24/Banil aggar5al Bmanager B'ale' B1.!.!%B!12/!B')n) da'gu(ta Bmanager B'ale' B12.%./+B!/

    %o hange the ase of all the reords of the emp.lst file #

    $ tr aUVD 6VD < em()l't1/,H6NH6L #"N;X" ,9"REHT=R ,#6LE# ,+.%.+0,/11,X)Y) 6;R6W6L ,;)M ,M6RYET"N; ,12.+1.4,%

    2+4!,)Z) #6FEN6 ,;)M ,M6RYET"N; ,12.+.4!,0+!/4,#[9"R 6;6RW6L ,EFEH[T"XE,IER#=NNEL ,./.4,!2+/!,Z6R[N #EN;[IT6 ,9"REHT=R ,IER#=NNEL ,!.11.4,022++,6)Y #[YL6 ,;)M ,#6LE# ,12.12.!2,/+212,#G6M #6Y#EN6 ,9);)M) ,6HH=[NT# ,12.12.!!,/24/,6N"L 6;;6RW6L ,M6N6;ER ,#6LE# ,1.!.!%,!12/!,#)N) 96#;[IT6 ,M6N6;ER ,#6LE# ,12.%./+,!/

    Unix User uide -ohammad Jaheer 8hmed &+

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    62/101

    The *rocess

    8 proess is simply an instane of a running program. 8 proess is said to 9e born2hen the programstarts exeution( and remains alive as long as the program is ative. 8fter the exeution is omplete(the proess is said to die. %he name of the program 9een exeuted is the name given to the proess.

    Sine U!I" is a multitas/ing system( more than one proess an run at a time. Eah proess isuniBuely identified 9y the 9y a num9er alled the$IDproess identifier5 that is allotted 9y the /ernel2hen it is 9orn. %his num9er is 9et2een < and 30(+. In ase the upper limit is enountered(

    proesses are renum9ered from

  • 8/13/2019 Unix Reference Manual

    63/101

    the U!I" system( 2hih has /no2ledge of the /ernel( 9uilt into it. It reads through the /ernel datastruture or proess ta9le to feth the attri9utes of a proess.

    $ ('I"9 TTG #T6T T"ME H=MM6N9

    +4 ( # :1 3a'-22+ ( R : ('

    %heps ommand( 9y default doesnQt provide details a9out the anestry of a proess. %o get those(you have to use the Jf option

    $ (' OfI"9 TTG #T6T T"ME H=MM6N9 +4 ( # :1 3a'-22/ ( R : \& (' f

    %he Juoption lets you /no2 the ativities of any user at any time. %his option has to 9e follo2ed 9ythe loginid

    $ (' u u'ername

    %o see information a9out the proess of all users Jaoption is used.

    $ (' Oa %% 1 # : 3a'- 1 2 # : .'3in.agetty +04 tty2 linu7 11 + # : .'3in.agetty +04 tty+ linu7

    +4 ( # :2 3a'-20 ( R : (' a24/ (1 # : 3a'-

    6ulti+le Nobs In 0ac&ground 7 &and the nohup Co))and

    8 multitas/ing system lets a user do more than one Ho9 at a time. Sine there an 9e only one Ho9 inthe foreground( the rest of the Ho9s have to run in the 9a/ground. %he &operator is provided 9y theshell to run a proess( and not 2ait for its death. 8fter the ommand line is terminated 2ith an &( theommand 2ill run in the 9a/ground. =n invo/ing a ommand 2ith the & affixed to it( the shell

    returns a num9er the 6I$ num9er of this proess. %he prompt is returned and the shell is ready toaept another ommand.

    $ 'ort o flname flname ]1V 20+2

    %he sort ommand runs in 9a/ground. 6I$ num9er is displayed in next line.

    7a/ground Ho9s ease to run( ho2ever( 2hen a user logs out. %hat happens 9eause his shell dies.8nd 2hen the parent dies( its hild also dies.

    Unix User uide -ohammad Jaheer 8hmed &,

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    64/101

    %he U!I" system permits a variation in this default 9ehavior. It provides the nohup (no hangup)ommand to permits exeution of a proess even 2hen the user has logged off. %his ommand isused 2ithout any option( and must preede any ommand that has to 9e run in the 9a/ground.

    $ no-u( 'ort flname ]1V 2041$ no-u(: a((ending out(ut to ^no-u()out_$

    %he 6I$ num9er and a message is displayed.Sine no output filename 2as speified 2ith sortommand( noh1psends the default output to a file Tnohup.outQ.

    $ no-u( gre( Jdirector` em()l't , 'ort ]

    In this ase sort ommand 2ill ease to exist( and only the output of grep 2ill go to Tnohup.outQ. Ifyou run more than one ommand in a pipeline( then you should use the nohup ommand at the

    9eginning of eah ommand in the pipeline.%hus( you should phrase the previous ommand in this2ay(

    $ no-u( gre( Jdirector` em()l't ] , no-u( 'ort ]

    Tnohup.outQ is reated in the urrent

    TE waitco))and

    7a/ground exeution 9eome meaningful if more than one Ho9 has to 9e exeuted onurrently. It ispointless to exeute a Ho9 in the 9a/ground and then remain idle in the foreground. 7ut someappliation may reBuire you to run one final program( 2hih uses some files reated 9y 9a/ground

    Ho9s. In suh an event( you an exeute this program only after all the 9a/ground Ho9s haveompleted exeution. %he wait ommand( 9y default( he/s 2hether all 9a/ground proesses have

    9een ompleted.

    Suppose there are three 9a/ground Ho9s running at the same time( as follo2s

    $ cut Oc14 flname > $tem()1121

    $ cut Oc/0 flname > $tem()2122

    $ cut Oc22+ flname > $tem()+12+

    %here are three 9a/ground Ho9s indiated 9y the 6I$ num9er. ou must have use paste ommand9efore pasting 9a/ all three temporary piees( so that next ommand is exeuted only after all the

    Unix User uide -ohammad Jaheer 8hmed &

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    65/101

    six 9a/ground proesses have 9een ompleted.

    $ 5ait$ (a'te Od BD $tem()CC

    When this ommand is exeuted( no proess is spa2ned. It sends the shell into Wait State so that itan a/no2ledge the death of all the hildren. It an also 9e used 2ith an argument. %hus(

    $ 5ait 1+0

    Waits for 9a/ground Ho9 9earing the 6I$ num9er 13,. It doesnQt 2ait for all other 9a/ground Ho9s(2hih are running at that time in 9a/ground.

    TE &ill co))and

    ou an terminate a proess 9y using /ill ommand. %he ommand uses one or more 6I$ num9ersas its arguments. %hus(

    $ kill 1!

    %erminates Ho9 2ith 6I$ num9er 1

  • 8/13/2019 Unix Reference Manual

    66/101

    9eing around 0 re()l't Htrl Q d o3 /!!+0/0)a at Sri No 1 1/:1: 1%%0$

    Unix User uide -ohammad Jaheer 8hmed +1

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    67/101

    The atOueue

    %he at Bueue is displayed 2ith Jl list option

    $ at l

    /!!+0/0)a Sri No 1 1/:1: 1%%0/!!+!/1)a #at No + 1/:1: 1%%/!!++442)a #un 9ec 1 1/:1: 1%%0$

    %his listing sho2s only the Ho9s sheduled 9y you( and not the others. !ote the program names aremissing from the listZ atdoesnQt display them at all. If you have sheduled a ouple of Ho9s in this2ay( there is simply no 2ay you an find out the name of the program sheduled to 9e exeuted.%his an reate pro9lems for you( espeially 2hen you are una9le to reall 2hether a speifi Ho9 hasatually 9een sheduled for later exeution.

    8ny Ho9 an 9e removed 9y using the Jr Dre)ove< option( along 2ith the Ho9 manner as theargument. %o remove the first Ho9 from the Bueue( enter

    $ at r /!!+0/0)a

    %he Jroption also aepts multiple arguments in the ommand line so that they an all 9e removed2ith a single at ommand. 'i/e the line printer spooler( Ho9s are also automatially removed afterthey have 9een exeuted.

    unning Nobs *eriodicall7 cron

    Unli/e at and batch that are meant for onetime exeution( cronexeutes programs at regularintervals.

    cron is mostly dormant(9ut every minute it 2a/es up and loo/s in a ontrol file inKusrKspoolKronKronta9s for instrutions to 9e performed at that instant.8fter exeuting them ( itgoes 9a/ to sleep (only to 2a/e up the next minute.

    ou are also permitted to plae a crontab file named after herKhis login name in this diretory. Eahsuh crontab file ontains a list of ommands( along 2ith the shedule of exeution. Speimen entryin the file an loo/ li/e this#

    1 1 * +/%12 ! ec-o Time for t-i' uarter' 3acku(D

    Eah line ontains a set of five fields separated 9y 2hite spae( representing the date and time for theommand to 9e exeuted.

    "ields 'hat to s+ecif

    1st !um9er of minutes after the hour 2hen the ommand is exeuted. 'egal values

  • 8/13/2019 Unix Reference Manual

    68/101

    0nd )our in 04hour format for sheduling. 'egal values 10453rd $ay of the month. 'egal values 13154th -onth 'egal values 1105&th $ays of the 2ee/ 'egal values

  • 8/13/2019 Unix Reference Manual

    69/101

    *attern Search

    Searching "or a *attern 7gerpgre+ is one of the most popular an useful U!I" filters.It sans for the ourrene of a pattern(andan display the seleted pattern(the line num9ers in 2hih they 2ere found(or the filenames 2herethe pattern ours.gre+ an also selet lines not ontaining the pattern.

    Syntax #

    gre( o(tion' (attern filename?'@

    gre+ reBuires an expression to represent the pattern to 9e searhed for(follo2ed 9y one or more

    filenames.%he first argument is al2ays treated as the expression(and the ones remaining as filenames.

    %o display lines ontaining the pattern LmanagerM from the file emp.lst

    $ gre( managerD em()l't24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/

    If the pattern is not found Unix 2ill silently returns to the prompt.

    When gre+ is used 2ith the series of strings(it interprets the first argument as the pattern( and therest as filenames.It then dislays the filenames along 2ith the output#

    $ gre( managerD em(1)l't em(2)l'tem(1)l't:24/,anil aggar5al,manager,'ale' ,1.!.!%,!em(1)l't:12/!,')n) da'gu(ta,manager,'ale' ,12.%./+,!/em(2)l't:12++,'unil ad-a ,manager,marketing ,1.!.!%,!em(2)l't:12/!,rai rai ,manager,(er'onnel ,12.%./+,!/

    %hec Dcount< option ounts the num9er of ourenes of the pattern in the speified files5.

    $ gre( Oc managerD em(1)l't em(2)l't em(1)l't:2

    em(2)l't:2

    %henDnu)ber< option an 9e used to display the line num9ers ontaining the pattern(along 2iththe lines#

    $ gre( managerD em()l't2:24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!!:12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/

    %hevDinverse< option selets all 9ut the lines not ontaining the pattern.

    Unix User uide -ohammad Jaheer 8hmed +4

    http://www.oracle.com/index.html
  • 8/13/2019 Unix Reference Manual

    70/101

    $ gre( O managerD em()l't+!/4,'ud-ir 6gar5al ,e7ecutie,(er'onnel ,./.4,!1/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/22++,a)k '-ukla ,g)m ,'ale' ,12.12.!2,/+212,'-yam 'ak'ena ,d)g)m) ,account' ,12.12.!!,/2+4!,)3) 'a7ena ,g)m ,marketing ,12.+.4!,011,)k) agra5al ,g)m ,marketing ,12.+1.4,%2+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,0

    %hel Dlist< option displays only the names of files 2here a pattern has 9een found #

    $ gre( Ol managerD *)l't em()l't em(1)l't

    %he >iDignorecase< option ignores ase for pattern mathing

    $ gre( Oi ManagerD em()l't

    2:24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!!:12/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/

    If you have seen all the a9ove ommands are ta/ing only one pattern for mathing.7ut 2hat if you2anted to searh for more than one pattern.In suh a ase (you an use thee option.

    $ gre( Oe managerD Oe directortD em()l't24/,anil aggar5al ,manager ,'ale' ,1.!.!%,!1/,c-anc-al 'ing-i ,director ,'ale' ,+.%.+0,/2+/!,3arun 'engu(ta ,director ,(er'onnel ,!.11.4,012/!,')n) da'gu(ta ,manager ,'ale' ,12.%./+,!/

    Je option allo2s you to speify more than one pattern for mathing.

    Extending gre+ 7 egrep

    %he egrep ommand (authored 9y 8lfred 8ho


Recommended