Sei sulla pagina 1di 66

Systems lab MCCS1.8 Cycle-1 1.unix commands: a.

Text processing and backup utilities:


1. head : It displays the first few lines of one or more files (page) . Syntax: $ head !ilename" It displays the first 10 lines of the file. Ex: [root@dcalab nixser!er "#$ cat aaa i am a good girl hello hw r i am fine how is it are there any thing india is my co nty all are fine it is better dont do that where are hello hw r is it o%% what is that than% bye

[root@dcalab nixser!er "#$ head aaa i am a good girl hello hw r i am fine how is it are there any thing india is my co nty all are fine it is better

Syntax: $ head #n !ilename" Ex: [root@dcalab nixser!er "#$ head &' aaa i am a good girl hello hw r syntax: $ cat !ilename" $ head -n - %t display !irst n lines Ex: [root@dcalab nixser!er "#$ cat aaa ( head &) i am a good girl hello hw r i am fine

&. tail : It displays last few records of one or more file

Syntax: tail !ilename" It displays last 10 lines. Ex: [root@dcalab nixser!er "#$ tail aaa co nty all are fine it is better dont do that where are hello hw r is it o%% what is that than% bye Syntax: tail #n !ilename" Ex: [root@dcalab nixser!er "#$ tail &* aaa than% bye syntax: cat !ilename" $ tail -n Ex: [root@dcalab nixser!er "#$ cat aaa ( tail &1 bye If u want to redirect to permanent Ex: + head &*0 ,filename- (tail &. - file1 (file1 is newfile) Ex: + tail /'0 ,filename- (head &. - file1 (file1 is newfile)

'. tr : 0ranslate characters by characters

0ranslate or delete characters Syntax: $ tr (option) !ilename" Ex: + [root@dcalab nixser!er sril #$ cat f' welcome to first mtech *010&11 [root@dcalab nixser!er sril #$ tr 1wel1 121,f' 222com2 to first mt2ch *010&11 Duplicate characters avoid with sequential order Ex: + tr &s 3r4 , filename Delete the characters Syntax: $ tr -d *r+ !ilename [root@dcalab nixser!er sril #$ tr &d 1e1,f5 wlcom to adithya collg of technology ,. Sort: sort lines of text files according to the first character Syntax: sort (option) !ilename" 6ptions: &b7 &&ignore&leading&blan%s ignore leading blan%s &d7 &&dictionary&order consider only blan%s and alphan meric characters &f7 &&ignore&case fold lower case to pper case characters &g7 &&general&n meric&sort compare according to general n merical !al e &i7 &&ignore&nonprinting consider only printable characters &87 &&month&sort compare ( n%nown) , 9:;<9 , ... , 9=E>9 &n7 &&n meric&sort compare according to string n merical !al e &r7 &&re!erse re!erse the res lt of comparisons

Ex : sort st dent student !ilename 01 !i?ay 50 @0 1* anil A0 A. '* s ?an 5. )0 0) hari A0 ). ** ra? B. .0

-. Cut: Cemo!e sections from each line of files. 1. Character cutting Syntax: $ cut -c !ilename" Dirst character of the all lines [root@dcalab nixser!er sril #$ c t &c .&10 f5 ome to ege echnol Ex: + c t &c1 ,filename0o c t the first >haracter 6f the all records Ex: + c t &cB ,filename0o c t the Bth character in each line Ex: + c t &c)7 B ,filename0o c t )th and Bth >haracter In each line Ex: + c t &c) &B ,filename0o c t )th to Bth >haracters range

2. Field cutting

&d field separator

Syntax: $ cut -d *:+ -!& !ilename" > t *nd field in all records Ex: [root@dcalab nixser!er sril #$ cat-bbb aaa:fff:hhhh ttt: ghh:tgyh:y ert:hg??%:hhh ['#/ Etopped cat - bbb

[root@dcalab nixser!er sril #$ c t &d : &f1 bbb aaa ttt ghh

.. /ni0: Cemo!e d plicate lines from a sorted file Syntax: $ uni0 (option) !ilename" 6ptions: &c7 &&co nt prefix lines by the n mber of occ rrences &d7 &&repeated only print d plicate lines &=7 &&all&repeated[Fdelimit&method# print all d plicate lines delimit&methodFGnone(defa lt)7prepend7separateH =elimiting is done with blan% lines. &f7 &&s%ip&fieldsF< a!oid comparing the first < fields &i7 &&ignore&case

ignore differences in case when comparing &s7 &&s%ip&charsF< a!oid comparing the first < characters & 7 && niI e only print niI e lines &w7 &&chec%&charsF< compare no more than < characters in lines =isplays only the = plicate records Ex: + niI &d ,filename [root@dcalab nixser!er sril #$ cat-ccc aaa bbb aaa aaa [A#/ Etopped cat - ccc

[root@dcalab nixser!er sril #$ niI &d ccc aaa Ex: + niI &= ,filenameJrint all d plicate Cecords and demeliting is done with blan% lines Ex: + niI Kc ,filenameJrefix lines by the < mber of occ rrences Ex: [root@dcalab nixser!er sril #$ niI &c ccc 1 aaa 1 bbb * aaa 1i!!: find differences between two files Syntax: di!! (options) !rom-!ile to-!ile

6ptions: &a &b &L 0reat all files as text and compare them line&by&line7 e!en if they do not seem to be text. Ignore changes in amo nt of white space. Ignore changes that ? st insert or delete blan% lines.

[root@dcalab nixser!er sril #$ cat-l c%y apple bannana orange pineapple cat rat

[@#/ Etopped

cat - l c%y

[root@dcalab nixser!er sril #$ cat-%eert apple banana oringe pineapple mat pen

[10#/ Etopped

cat - %eert

[root@dcalab nixser!er sril #$ diff l c%y %eert *7'c*7' , bannana

, orange &&& - banana - oringe .75c.75 , cat , rat &&& - mat - pen Comm.: compare two sorted files line by line Syntax: comm (23T%24)... 5%671 5%67& Ex: [root@dcalab nixser!er sril #$ comm l c%y %eert apple banana bannana orange oringe pineapple cat mat pen rat

8. tee : It displays and redirects the same time

read from standard inp t and write to standard o tp t and files

Ex: + ls &l ,filename+ ls &l ( tee ,filename+ ls &&f ll&time ( tee &a ,filename- & appending existing file <ote: witho t ( (pipe symbol) tee filter donMt wor%s 8. FIND COMMANDS It search for files in a directory hierarchy Eyntax: find [path...# [expression# N & root " & Oome =irectory . & Jresent wor%ing directory

Ex: + find Nbin Ktype f It searches the files and directories absol te path of the root.

Ex: + find " &type d It searches the directories and s bdirectories in the Oome directory

Ex: + find " &type l It searches the lin%ed files

Ex: + find " &type f &name 3file14 It searches the absol te path of the filename

Ex: + find " &type d &name 3ra?4 It searches the absol te path of the =irectories

Ex: + find " &type f &name 3file14 exce catGHPQ It searches the absol te path of the filename and displays the contents of file.

Ex: + find " &type f &name 3file14 exce cat GHP exce rmGHPQ It searches the absol te path of the filename and displays the content of file7 and remo!es at the same time.

Ex: + find " &type f &perm 5)) It searches the file nder with 5)) permission

Ex: + find " &type f &perm 5)) &exce chmod 5)0 GHPQ It searches the filename nder with permission 5)) and change the file permissions to 5)0.

Ex: + find " &type f siRe 0Q It Eearches the Sero byte files Linux: 512 bytes 1 block Ex: + find " &type f siRe 100c (Exactly 100 bytes file) + find " &type f siRe /100c (abo!e 100 bytes file) + find " &type f siRe &100c (below 100 bytes file)

Ex: + find " &type d siRe Bb

It searches the Bbytes =irectories

Ex: + find " &type f &i n m 310@B4 It searches the file with inode n mber

Ex: + find " type d K in m 310*)4 It searches the directories with inode n mber Time &a K ;ccess &c K >hanged &m K 8odified

Ex: + find " &type f Ka min '0 It searches the file before '0 min access Ex: + find " &type f Ka time KnNnN/n It searches the file before few days

9. :rep

0he grep tilities are a family of Tnix tools7 incl ding grep7 egrep7 and fgrep7 that perform repetiti!e searching tas%s. 0he tools in the grep family are !ery similar7 and all are sed for searching the contents of files for information that matches partic lar criteria. Dor most p rposes7 yo Ull want to se fgrep7 since itUs generally the fastest 0he general syntax of the grep commands is:

Eyntax: grep [&options# pattern [filename#

Vo can se fgrep to find all the lines of a file that contain a partic lar word. Dor example7 to list all the lines of a file named my file in the c rrent directory that contain the word 1dog17 enter at the Tnix prompt: Ex: fgrep dog myfile 0his will also ret rn lines where 1dog1 is embedded in larger words7 s ch as 1dogma1 or 1dogged1. Vo can se the &w option with the grep command to ret rn only lines where 1dog1 is incl ded as a separate word: Ex: grep &w dog myfile

0o search for se!eral words separated by spaces7 enclose the whole search string in I otes7 for example:

Ex: fgrep 1dog named >hec%ers1 myfile

0he fgrep command is case sensiti!eQ specifying 1dog1 will not match 1=og1 or 1=6W1. Vo can se the &i option with the grep command to match both pper& and lowercase letters: Ex: grep &i dog myfile 0o list the lines of myfile that do not contain 1dog17 se the &! option: Ex: fgrep &! dog myfile If yo want to search for lines that contain any of se!eral different words7 yo can create a second file (named second file in the following example) that contains those words7 and then se the &f option: Ex: fgrep &f second file my file Vo can also se wildcards to instr ct fgrep to search any files that match a partic lar pattern. Dor example7 if yo wanted to find lines containing 1dog1 in any of the files in yo r directory with names beginning with 1my17 yo co ld enter:

Ex: fgrep dog my2 0his command wo ld search files with names s ch as my file7 my.hw17 and my st ff in the c rrent directory. Each line ret rned wo ld be prefaced with the name of the file where the match was fo nd. Ly sing pipes andNor redirection7 yo can se the o tp t from any of these commands with other Tnix tools7 s ch as more7 sort7 and c t. Dor example7 to print the fifth word of e!ery line of my file containing 1dog17 sort the words alphabetically7 and then filter the o tp t thro gh the more command for easy reading7 yo wo ld enter at the Tnix prompt: Ex: fgrep dog myfile ( c t &f. &d1 1 ( sort ( more If yo want to sa!e the o tp t in a file in the c rrent directory named new file7 enter:

Ex: fgrep dog my file ( c t &f. &d1 1 ( sort - new file + grep Kn ,file- & 0o display record n mber

+ grep &i,file- & 0o ignore record

+ grep Kc ,file- & 0o co nt in how many records expression

+ grep KE ,file- & 0o search for m ltiple expression

+ grep KX ,file- & It gi!e the file name and which the reg lar expression + grep Kr ,file- & 0o search regressi!ely and present wor%ing

directory

+ grep Kw ,file- & 0o search for the exact match for the word

+ grep Ks ,file- & 0o s ppress errors

+ grep 3?ai4 ,file-

& It prints the expressi!e record which

ha!e got the 3?ai4

+ grep Kn 3?ai4 ,file- & It display the record n mber and expression what we gi!e 3?ai4

+ grep Kin 3?ay4 ,file- & It ignore and prints the record 3:ay4

+ grep KE 3?ay prem4 ,file- & It search for m ltiple expressions and prints the record

+ grep Kl 3?ay42

& It displays the filename which the

3:ay4 expression is

!"

;YZ is a simple and elegant pattern scanning and processing lang age ;YZ is also the most portable scripting lang age

It was created in late A0th of the last cent ry. 0he name was composed from the initial letters of three original a thors ;lfred [. ;ho7 Lrian Y. Zernighan7 and Jeter :. Yeinberger. It is commonly sed as a command&line filter in pipes to reformat the o tp t of other commands. ItUs the prec rsor and the main inspiration of Jerl. ;ltho gh originated in Tnix it is a!ailable and widely sed in Yindows en!ironment too. ;YZ ta%es two inp ts: data file and command file. 0he command file can be absent and necessary commands can be passed as a gments. ;s Conald J. Xo i aptly noted aw i! "er# under appreciated language$ 0he main ad!antage of ;YZ is that nli%e Jerl and other 1scripting monsters1 that it is !ery slim witho t feat re creep so characteristic of Jerl and th s it can be !ery efficiently sed with pipes. ;lso it has rather simple7 clean syntax and li%e m ch hea!ier 0>X can be sed with > for 1d al& lang age1 implementations. aw%Us fa!or compared to perl: & aw% is simpler (especially important if deciding which to learn first) & aw% syntax is far more reg lar (another ad!antage for the beginner7 e!en witho t considering syntax&highlighting editors) & yo may already %now aw% well eno gh for the tas% at hand & yo may ha!e only aw% installed & aw% can be smaller7 th s m ch I ic%er to exec te for small programs & aw% !ariables donUt ha!e \+U in front of them :&) & clear perl code is better than nclear aw% codeQ b t <60OI<W comes close to nclear perl code

The basic #unction o# awk is to search #iles #or lines $or other units o# text% that contain certain patterns& !hen a line matches one o# the patterns' awk per#orms speci#ied actions on that line& awk keeps processin( input lines in this way until it reaches the end o# the input #iles

Eyntax : aw% [option# ]selection criteria GactionHM ,file-

6ptions : &D & 0o specify the field separator &f & 0o in!o%e the so rce code

GactionH & 6nly the print action

prede#ined variables in awk

all predefined !ariables are in pper cases

DE 6DE <D <C +

& Inp t field separator & 6 p t field separator & < mber of fields & Cecord n mbers or <o. of records & Dields in aw%

)omparation *perator in awk

-F , ,F FF ^F " ^"

& Wrater than & Wrater than eI al & Xess than & Xess than eI al & EI al to & <ot eI al & 8atching & not matching

Xogical 6perator

__ ((

& ;<= & 6C

aw% has got ' sections

1. LIWI< *. 8I==XE '. E<= Legin is %eyword for the begin section the !ariable can be assign in begin section ;ll the operator in the middle sections7 8iddle is not %eyword for the middle section Yhat e!er print e!ery thing in the section7 End is the %eyword for the end section

+ aw% ]Na?ayNGprintHM,fileIt prints the all the records + aw% ]Na?ay(ram NGprintHM,file0o search for m ltiple expressions and print + aw% ]<CFF)GprintHM ,file0o print specific record + aw% ]<CFF'7<CFFAGprintHM,file0o print range of records + aw% ]<C-)GprintHM ,file0o print all the records which are -) + aw% ]<C-FGprintHM ,file+ aw% ]<C,)GprintHM ,file0o print all the records which are ,) + aw% ]<C,F)GprintHM ,fileif want print only specific fields

+ aw% KD 3:4 ]<CFF) Gprint +17 +'7 +)HM ,filesimple aw% program em lates the cat tilityQ it copies whate!er yo type on the %eyboard to its standard o tp t (why this wor%s is explained shortly). + aw% UG print HU <ow is the time for all good men

&( <ow is the time for all good men to come to the aid of their co ntry. &( to come to the aid of their co ntry. Do r score and se!en years ago7 ... &( Do r score and se!en years ago7 ... Yhat7 me worry` &( Yhat7 me worry` >trl&d a Jrint the length of the longest inp t line: aw% UGif(length(+0)- max)max F length(+0) H E<= G print max HU data a Jrint e!ery line that is longer than B0 characters: aw% Ulength(+0) - B0U data 0he sole r le has a relational expression as its pattern and it has no actionbso the defa lt action7 printing the record7 is sed. a Jrint the length of the longest line in data: expand data ( aw% UGif x , length()) x Flength()H E<= Gprint 1maxim m line length is 1 xHU 0he inp t is processed by the expand tility to change tabs into spaces7 so the widths compared are act ally the right&margin col mns. a Jrint e!ery line that has at least one field: aw% U<D - 0U data 0his is an easy way to delete blan% lines from a file (or rather7 to create a new file similar to the old file b t from which the blan% lines ha!e been remo!ed). a Jrint se!en random n mbers from 0 to 1007 incl si!e: aw% ULEWI< Gfor(iF1Qi,FAQi//)print int(1012rand())HU a Jrint the total n mber of bytes sed by #iles: ls &l #iles ( aw% UG x /F +. H E<= G print 1total bytes: 1 x HU a Jrint the total n mber of %ilobytes sed by #iles: ls &l #iles ( aw% UG x /F +. H E<= G print 1total Z&bytes: 1 x / 10*')N10*) HU a Jrint a sorted list of the login names of all sers: aw% &D: UG print +1 HU NetcNpasswd ( sort a >o nt the lines in a file:

aw% UE<= G print <C HU data a Jrint the e!en&n mbered lines in the data file: aw% U<C c * FF 0U data If yo se the expression \<C c * FF 1U instead7 the program wo ld print the odd& n mbered lines 7;<M367S $ is the comment character for aw%. UfieldU means Ucol mnU $ Jrint first two fields in opposite order: aw% UG print +*7 +1 HU file $ Jrint lines longer than A* characters: aw% Ulength - A*U file $ Jrint length of string in *nd col mn aw% UGprint length(+*)HU file $ ;dd p first col mn7 print s m and a!erage: G s /F +1 H E<= G print 1s m is17 s7 1 a!erage is17 sN<C H $ Jrint fields in re!erse order: aw% UGfor i F <DQ i - 0Q &&i)print +i HU file $ Jrint the last line Gline F +0H E<= Gprint lineH $ Jrint the total n mber of lines that contain the word Jat NJatN Gnlines F nlines / 1H E<= Gprint nlinesH

$ Jrint all lines between startNstop pairs: aw% UNstartN7 NstopNU file $ Jrint all lines whose first field is different from pre!io s one: aw% U+1 ^F pre! G printQ pre! F +1 HU file $ Jrint col mn ' if col mn 1 - col mn *: aw% U+1 - +* Gprint +'HU file $ Jrint line if col mn ' - col mn *: aw% U+' - +*U file $ >o nt n mber of lines where col' -col 1 aw% U+' - +1 Gprint i / 111Q i//HU file $ Jrint seI ence n mber and then col mn 1 of file: aw% UGprint <C7 +1HU file $ Jrint e!ery line after erasing the *nd field aw% UG+* F 11Q printHU file $ Jrint hi *B times yes ( head &*B ( aw% UG print 1hi1 HU $ Jrint hi.0010 to hi.00@@ (<60E IC;D TEECE^) yes ( head &@0 ( aw% UGprintf(1hi00c*.0f Pn17 <C/@)HU $ Ceplace e!ery field by its absol te !al e G for (i F 1Q i ,F <DQ iFi/1) if (+i , 0) +i F &+i printH $ If yo ha!e another character that delimits fields7 se the &D option $ Dor example7 to print o t the phone n mber for :ones in the following file7 $ 000@0*(Lea!is(0heodore('''&*)*&****(1)@0@*

$ 000@01(:ones(Lill(.'*&'B*&0')*(*')0*' $ ... $ type aw% &D1(1 U+*FF1:ones1Gprint +)HU filename $ Eome looping for printo ts LEWI<G for (iFBA.Qi-B''Qi&&)G printf 1lprm &Jlw cdPn17 i H exit H Dormatted printo ts are of the form printf( 1formatPn17 !al e17 !al e*7 ... !al e<) e.g. printf(1howdy c&Bs Yhat it is bro. c.*fPn17 +17 +*2+') cs F string c&Bs F B character string left ? stified c.*f F n mber with * places after . c5.*f F field 5 chars with * chars after . Pn is newline Pt is a tab $ Jrint freI ency histogram of col mn of n mbers +* ,F 0.1 GnaFna/1H (+* - 0.1) __ (+* ,F 0.*) Gnb F nb/1H (+* - 0.*) __ (+* ,F 0.') Gnc F nc/1H (+* - 0.') __ (+* ,F 0.)) Gnd F nd/1H (+* - 0.)) __ (+* ,F 0..) Gne F ne/1H (+* - 0..) __ (+* ,F 0.5) Gnf F nf/1H (+* - 0.5) __ (+* ,F 0.A) Gng F ng/1H (+* - 0.A) __ (+* ,F 0.B) Gnh F nh/1H (+* - 0.B) __ (+* ,F 0.@) Gni F ni/1H (+* - 0.@) Gn? F n?/1H E<= Gprint na7 nb7 nc7 nd7 ne7 nf7 ng7 nh7 ni7 n?7 <CH $ Dind maxim m and minim m !al es present in col mn 1 <C FF 1 GmF+1 Q pF+1H +1 -F m Gm F +1H +1 ,F p Gp F +1H E<= G print 18ax F 1 m7 1 8in F 1 p H $ Example of defining !ariables7 m ltiple commands on one line <C FF 1 Gpre!F+)Q pre!a F +1Q pre!b F +*Q nF0Q s mF0H

+) ^F pre! Gprint pre!a7 pre!b7 pre!7 s mNnQ nF0Q s mF0Q pre! F +)Q pre!a F +1Q pre!b F +*H +) FF pre! Gn//Q s mFs m/+.N+5H E<= Gprint pre!a7 pre!b7 pre!7 s mNnH $ Example of sing s bstrings $ s bstr(+*7@7A) pic%s o t characters @ thr 1. of col mn * Gprint 1imarith17 s bstr(+*717A) 1 & 1 +'7 1o t.1s bstr(+*7.7')H Gprint 1imarith17 s bstr(+*7@7A) 1 & 1 +'7 1o t.1s bstr(+*71'7')H Gprint 1imarith17 s bstr(+*71A7A) 1 & 1 +'7 1o t.1s bstr(+*7*17')H print 1imarith17 s bstr(+*7*.7A) 1 & 1 +'7 1o t.1s bstr(+*7*@7')H 1. =enaming >ithin the name: ls &1 2old2 ( aw% UGprint 1m! 1+11 1+1HU ( sed sNoldNnewN* ( sh (altho gh in some cases it will fail7 as in filedolddanddold) &. =emo?e only !iles: ls &l 2 ( grep &! drwx ( aw% UGprint 1rm 1+@HU ( sh or with aw% alone: ls &l(aw% U+1^"NedrwxNGprint +@HU(xargs rm Le caref l when trying this o t in yo r home directory. Ye remo!e files^ '. =emo?e only directories ls &l ( grep UedU ( aw% UGprint 1rm &r 1+@HU ( sh or ls &p ( grep N+ ( w% UGprint 1rm &r 1+1HU or with aw% alone: ls &l(aw% U+1"Ned.2xNGprint +@HU(xargs rm &r Le caref l when trying this o t in yo r home directory. Ye remo!e things^ ,. @illing processes by name (in this example we %ill the process called netscape): %ill \ps a xww ( grep netscape ( egrep &! grep ( aw% UGprint +*HU\ 1isk utilities: 1.d!Adisk !reeB: df command reports the n mber of free dis% blo%s and inode a!ailable on all mo nted file systems or on a gi!en name. Syntax: d! (option) (!ile system) 6ptions: h i displays information in readable format. displays information abo t free inodes.

Ex: [root@dcalab nixser!er "#$ d sril

) sril Nd' B sril Ndir*Ndir1 *) sril Ndir* )) sril [root@dcalab nixser!er "#$ d &b sril )0@5 sril Nd' )1'A sril Ndir*Ndir1 B'*. sril Ndir* 155'* sril &.duAdisk usageB:d command prints dis% sage7i.e.7the n mber of .1* bytes bloc%s sed by each named directory and its s bdirectory(defa lt is c rrent directory). Syntax: du (option) (directories) 6ptions: a r s print sage of all files print 3can not open4 message if a file or directory is inaccessible. print only the grand total for each named directory

Ex: [root@dcalab nixser!er "#$ df Dilesystem 1Z&bloc%s Tsed ;!ailable Tsec 8o nted on Nde!NmapperN[olWro p00&Xog[ol00 A.A55B15 )@@0'.* 55B5.50) Ac N Nde!Nhda1 1010B5 1*15A B'A00 1'c Nboot tmpfs .1')5B 0 .1')5B 0c Nde!Nshm '.mountAmounting !ile systemB: mo nt connect the specified de!ice (which is act ally a file system) to the directory specifed the member files of the files system mo nted becomes the members of the directory on which they are mo nted . Syntax: mount (deri?ename directory) (option) 6ptions: &r the file system will be mo nted as 3read 6nly4 the defa lt option is 3rNw4 ,. umount(dismounting !ile systemB: 3 mo nt4 disconnect sho ld be sed only after ma%ing s re that the partic lar file system is not b sy that the partic lar file system is not b sy. Tnli%e the case of 3mo nt47 yo do not ha!e to specify the mo nt point. Tse 3sync4 before sing 3 mo nt4. Syntax: umount (de?ice ,name)

b. 3rocess utilities: 3rocess: ; process is a program that is being exec ted. In nix m ltiple processes can r n conc rrently. /sers: 0ypically there are n mber of sers on a nix system. Each ser is identified by a niI e ser name and ser&id. Tnix ses the ser name in se!eral different ways: 1. 0o report sage of system reso rces. *. 0o display the list of sers on the system. '. 0o implement system and file sec rity. /sergroups: ; ser may be part of one or more sergro ps. 3%1: In m lti ser en!ironment7 there are n mber of processes that are being exec ted. 0o %eep trac% of process that is being exec ted7 nix assigns a niI e process identification n mber(JI=) to each acti!e process. 1.ps Aprocess statusB: 0he ps command displays information abo t the indi!id al processe that are exec ting on the system. Syntax: $ps (options) 6ptions: &a &l reports information abo t all process report information in long format

Ex: [root@dcalab nixser!er sril #$ ps JI= 00V 0I8E >8= '1'B ptsN1 00:00:00 bash '1A@ ptsN1 00:00:00 ps &. kill: Terminating a process Zill is sed to terminate the exec tion of a bac%gro nd process. 0he process&id of the process to be terminated m st be specified with %ill command. If process id ]0 (Rero)M is specified7 all processes in the process are signed. Syntax: $kill process id" 6ptions: n 0 &1 where n is larger than 0. 0he process with pid n will be signaled. ;ll processes in the c rrent process gro p are signaled. ;ll processes with pid larger than 1 will be signaled.

Ex: [root@dcalab nixser!er sril #$ %ill &@ 0

'.at: at command allows the ser to specify the time when a command is to be exec ted. 0he command ta%es two arg ments7 the time at which the command is to be exec ted7 and the command to be exec ted. Syntax: at time(date) (increment) Ex: [root@dcalab nixser!er sril #$ [root@dcalab nixser!er sril #$ at 1*.'0pm ?an 1' at- cat f' at- ,E60?ob ' at *011&01&1' 1*:'0 ,.nice: Tnix also allows the ser to specify the priority of commands. 0he nice command is sed to r n a command at a specified sched ling priority. Syntax: nice (-increment) command (argument) Ex: [root@dcalab nixser!er "#$ nice nice 0 -. >ho: who command shows who is logged on7as well as information abo t the system. Syntax: $>ho Ex: [root@dcalab nixser!er sril #$ who root :0 *011&01&1' 0@:*5 root ptsN1 *011&01&1' 0@:)@ (1A*.15.5.*.0) ..>ho am i: =isplays c rrent ser. Syntax:$>ho am i Ex: [root@dcalab nixser!er sril #$ who am i root ptsN1 *011&01&1' 0@:)@ (1A*.15.5.*.0) 8.>: w command printf s mmaries of system sage7 c rrently logged&in sers7 and what they are doing. Syntax: > (option) (user)

6ptions: & Ignores the sername while fig ring o t the c rrent process and cp times. 0o demonstrate this7 do a 1s 1 and do a 1w1 and a 1w & 1. &s Tse the short format. =on9t print the login time7 :>JT or J>JT times. &l =isplay information in long format. &[ =isplay !ersion information.

Ex: [root@dcalab nixser!er sril #$ w 10:**:)' p 1:0A7 * sers7 load a!erage: 0.107 0.0*7 0.01 TEEC 00V DC68 X6WI<@ I=XE :>JT J>JT YO;0 root :0 & 0@:*5 `xdm` 1'.0's 0.11s N srNbinNgnome&session root ptsN1 1A*.15.5.*.0 0@:)@ 0.00s 0.0As 0.00s w B. 5ile le?el security: Tnix ses an elaborate method of file access permissions to maintain file sec rity. Dile access permissions: Each file and directory has an 1. ;n owner: 0he ser who created the file. *. ; gro p: ; gro p of sers ha!e access to the file. '. 6thers: 6ther sers of the system. 0he permissions can be granted or denied to these three classes of sers. 0hree types of file access permissions are there7 they are read: ; file can be read7 displayed on terminal7 copied and compiled. write: ; file can be read modified and deleted. exec ted: ; file can be exec ted as a program. 0hey are represented as &rwx&&&&&& drwxr&&r&& &rwx&w&&w& & & Dile d & =irectory w & Yrire r & Cead x & Exec table <ote: Yitho t write permissions we canMt copy7 remo!e7 modify7 mo!e7 crate drwxrwxrwx AAA =irectory K AAA Dile K 555 =efa lt =irectory Jermissions K A.. =efa lt Dile Jermissions K 5)) Cead Yrite Exec table K) K* K1

0otal Jermissions A chmod A change modeB: 0he chmod command changes file access permissions for a file.

Eyntax: chmod mode files 0he mode is the permission to be assigned. 8ode can be specified in two ways 1. < meric 8ethod *. Eymbolic 8ethod 1. 4umeric Method: Syntax: $ chmod (permissions) !ileC1irectory" Ex: [root@dcalab nixser!er sril #$ ls &l total *) drwxr&xr&x * root root )0@5 :an 1* 0@:'5 d' &rw&r&&r&& * root root )1 :an 1* 0A:)0 fB [root@dcalab nixser!er sril #$ chmod 5*) fB [root@dcalab nixser!er sril #$ ls &l total *) drwxr&xr&x * root root )0@5 :an 1* 0@:'5 d' &rw&&w&r&& * root root )1 :an 1* 0A:)0 fB &. Symbolic Method: Tser K Wro p K g 6thers K o ;ll Ka Cead K r Yrite K w Exec te K x Syntax: $ chmod (options) 5ileC1irectory" Ex: [root@dcalab nixser!er sril #$ ls &l total *) drwxr&xr&x * root root )0@5 :an 1* 0@:'5 d' &rw&r&&r&& 1 root root '1 :an 10 1':** f' [root@dcalab nixser!er sril #$ chmod oFrwx7gFr7 Fx f' [root@dcalab nixser!er sril #$ ls &l total *) drwxr&xr&x * root root )0@5 :an 1* 0@:'5 d' &&&xr&&rwx 1 root root '1 :an 10 1':** f' To <dd Drite permission to :roup and 2thers Ex: [root@dcalab nixser!er sril #$ ls &l total *) drwxr&xr&x * root root )0@5 :an 1* 0@:'5 d' [root@dcalab nixser!er sril #$ chmod g/w7o/w d' [root@dcalab nixser!er sril #$ ls &l total *) drwxrwxrwx * root root )0@5 :an 1* 0@:'5 d' To =emo?e the Drite 3ermission to /ser Ex: [root@dcalab nixser!er sril #$ ls Kl

&rw&&&x&&& 1 root root )1 :an 1* 0A:)) f5 [root@dcalab nixser!er sril #$ chmod &w f5 [root@dcalab nixser!er sril #$ ls Kl &r&&&&x&&& 1 root root )1 :an 1* 0A:)) f5 To =emo?e the Drite and execute permissions to :roup and 2thers Ex: [root@dcalab nixser!er sril #$ ls &l total *) drwxrwxrwx * root root )0@5 :an 1* 0@:'5 d' [root@dcalab nixser!er sril #$ chmod g&wx7o&wx d' [root@dcalab nixser!er sril #$ ls &l total *) drwxr&&r&& * root root )0@5 :an 1* 0@:'5 d' <ppend the Drite permissions to all Ex: +chmod /w7 g/w7 o/w =ir1 (or) +chmod a/w =ir1 /M<S@ A/ser 5ile Creation MaskB & c ting7 remo!e7 hidden Ly =efa lt mas% !al e is 0** =irectory Dile AAA 555 0** 0** A.. 5)) Tmas% n mber 0 1 * ' ) . 5 A Jermissions DileN=irectory &&& &&x &w& &wx r&& r&x rw& rwx

Ex: [root@dcalab nixser!er sril #$ ls &l total *) d&&&&&&&&& * root root )0@5 :an 1* 0@:'5 d' [root@dcalab nixser!er sril #$ chmod 111 d' [root@dcalab nixser!er sril #$ ls &l total *) d&&x&&x&&x * root root )0@5 :an 1* 0@:'5 d' 9. cho>n Achange o>nerB E chgrp Achange groupB: chown changes file ownership and

reassign the ownership of the file from one ser to another. Syntax: cho>n (option) ne>o>ner !ilename 6ptions: &c7 &&changes&li%e !erbose b t report only when a change is made &h7 &&no&dereference&affect each symbolic lin% instead of any referenced file Ex: [root@dcalab nixser!er sril #$ ls &l &&&xr&&rwx 1 root root '1 :an 10 1':** f' [root@dcalab nixser!er sril #$ chown mic f' [root@dcalab nixser!er sril #$ ls &l &&&xr&&rwx 1 mic root '1 :an 10 1':** f' Chgrp also changes ownership. It changes the gro p ownership of the file. Syntax: chgrp (option) groupname !ilename 6ptions: &c7 &&changes& li%e !erbose b t report only when a change is made &h7 &&no&dereference&affect each symbolic lin% instead of any referenced file &&preser!e&root fail to operate rec rsi!ely on 9N9 &f7 &&silent7 &&I iet s ppress most error message &C7 &&rec rsi!e operate on files and directories rec rsi!ely Ex: [root@dcalab nixser!er sril #$ ls &l &&&xr&&rwx 1 mic root '1 :an 10 1':** f' [root@dcalab nixser!er sril #$ chgrp mic f' [root@dcalab nixser!er sril #$ ls &l &&&xr&&rwx 1 mic mic '1 :an 10 1':** f' 1F.ne>grp : changes gro p. Syntax: ne>grp group [root@dcalab nixser!er sril #$ newgrp mic c.4et>orking commands: 1. !tp:0his command is sed to connect to any other comp ter on yo r networ% r nning ftp. Etart it with the following [root@dcalab nixser!er "#$ ftp ftp ftp- ` Oere in the ftp prompt we ha!e to specify special ftp commands.0ype a ` or help to prompt commands. 0he following table lists the most freI ently sed commands Command =esult Tses7 ascii as a file transfer type Cings the bell when file transfer is

ascii bell

Linary I it or bye close cd get filename pwd

completed Tses binary as file transfer type 0erminates ftp session Ends ftp connection with remote machine7 b t %eeps local ftp program r nning >hanges directory on remote machine Wets the filename from remote machine Xists the c rrent wor%ing directory on remote machine

ftp- open 1A*.15.5.100 >onnected to 1A*.15.5.100. **0 dcalab nixser!er D0J ser!er ([ersion ..50) ready. <ame (1A*.15.5.100:mic):sri .'0 please specify password Jassword: .'0 login s ccessf l. Cemote system type is T<If. ftp-pwd '1' 3homeNmic4 ftp-ls &rw&r&&r&& 1 mic mic 10@ ; g 1B 1B:)5 ; &rw&r&&rb1 mic &rw&r&&rb1 mic drwxr&xr&x * mic ftp- I it **1 Woodbye. mic mic mic 11' ; g 1B 1B:)@ a.c A5 6ct *1 1':0@ add1.sh

)0@5 :an . 1':*B =es%top

*. rlogin A=emote loginB:The rlogin command allo>s you to remotely login another computer on your net>ork. [mic@dcalab nixser!er "#+ rlogin root 1*').5 '. telnet: It is a command sed to comm nicate with another host. [mic@dcalab nixser!er "#+ pwd NhomeNmic [mic@dcalab nixser!er "#+ telnet 1A*.15.5.100 0rying 1A*.15.5.100... >onnected to 1A*.15.5.100 (1A*.15.5.100). Escape character is Ue#U. Ced Oat Enterprise Xin x Eer!er release ... (0i%anga) Zernel *.5.1B&1@).el. on an i5B5

login: mca0@'' Jassword: Xast login: 0 e 6ct 1@ 0@:)0:*0 from 1A*.15.5.'1 [mca0@''@dcalab nixser!er "#+ pwd NhomeNmca0@'' [mca0@''@dcalab nixser!er "#+ exit >onnection closed by foreign host. [mic@dcalab nixser!er "#+ pwd NhomeNmic ). 5inger: 0his command displays information abo t system sers. 2ptions: &s display the o tp t in short form &l display th o tp t in long form &m co ese finger to search only in login names that match with arg ment name. Ex: [mic@dcalab nixser!er "#+ finger mic Xogin: mic <ame: (n ll) =irectory: NhomeNmic Ehell: NbinNbash 6n since Yed :an 1* 10:.1 (IE0) on ptsN1 from 1A*.15.5.*.0 <o mail. <o Jlan. [mic@dcalab nixser!er "#+ finger &s mic Xogin <ame 0ty Idle Xogin 0ime 6ffice 6ffice Jhone mic ptsN1 :an 1* 10:.1 (1A*.15.5.*.0) .. Dall: Dor broad casting messages. [root@dcalab nixser!er "#$ wall hai Lroadcast message from root (ptsN*) (Yed :an 1* 1':*0:1. *011): hai

d.5ile handling utilities:


1. p>d Aprint >orking directoryB: =isplays the c rrent wor%ing directory. Syntax: $ p>d Ex: [root@dcalab nixser!er "# $ pwd Nroot &. cat command:displaying and creating files

To Create a 5ile: Syntax: $ cat (option) " !ilename"

0he option can be any one of the below E.n o 1. *. '. ). .. 5. 6ption &! &n &b &e &; &s D nction It is sed to display non printable characters. It is sed to n mbering lines in o tp t. It sed to n mber nonblan% o tp t lines. It display + at end of each line. Ehows all ne!er more than one single blan% line

Ex: [root@dcalab nixser!er "#$cat - file1 Oello.. 0his is my first Dile Oa!e a <ice =ay Lye >trl/d (Ea!e) To Gie> a already existing 5ile: Syntax: $ cat !ilename" Ex: [root@dcalab nixser!er "#$cat file1 Oello.. 0his is my first Dile Oa!e a <ice =ay Lye Cat command can also be used to accept more than one !ile as an argument. Syntax: $cat !ile1 !ile& Ex: [root@dcalab nixser!er "#$cat f1 f*

0his is simple file 0his is a simple file* 0he contents of second file are shown immediately after the first file. To append data to an existing !ile Syntax: $ cat "" !ilename" Ex: [root@dcalab nixser!er "#$cat -- file1 Oello.. 0his is my first Dile Oa!e a <ice =ay Lye Wood mornig Yelcome to new world To Create a Multiple !ile >ith help o! cat command Syntax: $ cat !ilename1 !ilename& ...!ilename AnB " Ex[root@dcalab nixser!er "#$cat -f1 -f* -f' -f) Oello.... 0his is file n mber f) Ye create m ltiple files ed (Ea!e)

1isplay the record number to the particular !ile Syntax: $ cat #n !ilename"

Ex: [root@dcalab nixser!er "#$ cat &n f1 1 welcome to adithya * college ' of technology To ignore the blank =ecords

Syntax: $ cat #b

!ilename"

Ex: [root@dcalab nixser!er "#$ cat &n f) 1 hg?fdh * ' sd?fh!idf ) . sd%lf?g!i? [root@dcalab nixser!er "#$ cat &b f) 1 hg?fdh * sd?fh!idf ' sd%lf?g!i? Create a Hidden !ile Syntax: $ cat (option) !ilename" Ex: + cat -.file1 [root@dcalab nixser!er "#$ cat -.f. hai hello 1isplay the record >ith single blank line. [root@dcalab nixser!er "#$ cat f5 gdfahf?

gfhdgh?

hgdf hdi ?hd?f?i% [root@dcalab nixser!er "#$ cat &s f5 gdfahf?

gfhdgh?

hgdf hdi ?hd?f?i% [root@dcalab nixser!er "#$ cat f. cat: f.: <o s ch file or directory !ile names >ith common strings can be displayed using cat as: syntax:$cat !ileI Ex: [root@dcalab nixser!er "#$ cat f` welcome to adithya college of technology welcome to first mtech *010&11 welcome to first mtech *010&11 hg?fdh

sd?fh!idf

sd%lf?g!i? gdfahf?

gfhdgh?

hgdf hdi ?hd?f?i%

'.CC command: used to compile one or more C source !iles. Syntax: $cc !irst.c ,. lsAlistB : It is a command to list the files and directories in the present wor%ing =irectory $ ls - a : It is a command to display all files and =irectories incl ding hidden files and =irectories. $ls J : Xist information abo t the Diles (the c rrent directory by defa lt). Eort entries alphabetically $ls K : It list the all Lac% p files $ls L : It list the all lin%ed files and =irectories $ls -d : It =isplays the present wor%ing =irectory. $ls -i : It =isplays the inode n mbers of files and =irectories $ls -s : It =isplays the siRes in bloc%s (Diles _ =irectories) $ls -l : It =isplays the long listing files and directories in present wor%ing directory 6isting directory contents: $ ls list a directory $ ls -l list a directory in long (detailed) format Dor example: $ ls -l

drwxr&xr&x ) !i?ay ser 10*) : n 1B 0@:)0 Y;I0C6<dE;C<I<WE &rw&r&&r&& 1 %iran ser A5A'@* : n 5 1):*B scanlib.tar.gR eeee e e e e e e e (( ( ( ( ( ( ( ( ( ( (( ( ( ( owner gro p siRe date time name (( ( ( n mber of lin%s to file or directory contents ( ( ( permissions for world(others) ( ( permissions for members of gro p ( permissions for owner of file: r F read7 w F write7 x F exec te &Fno permission type of file: & F normal file7 dFdirectory7 l F symbolic lin%7 and others... ls -ld J Xist all the file and directory names in the c rrent directory sing long format. Yitho t the 1d1 option7 ls wo ld list the contents of any s b&directory of the c rrent. Yith the 1d1 option7 ls ? st lists them li%e reg lar files. $ ls -al : It =isplays incl ding hidden and log listing files and =irectories $ ls -m : It =isplays all files and =irectories with separated by comma (7) $ ls -ls : It =isplays all long listing =irectories $ ls --!ull-time : It =isplays files and =irectories with total information date and time $ ls -nl : It =isplays the long listing files and=irectories according to modification 0ime $ ls -rtl : It =isplays the file and =irectories with re!erse order $ ls -= : It =isplays the all files and =irectories Cegressi!ely (order by order) $ ls -l : It =isplays the files and =irectories in a single col mn (!ertical) $ ls -x : It =isplays the files and =irectories with m ltiple col mns Dor Ex: [root@dcalab nixser!er "#$ ls * ; a.c add1.sh add.sh echocli echocli.c echoser echoser.c exam1.c loc%ings.c mani men d.sh pollcli.c pollser.c ser sig.c sri sser.c strclic.c

anaconda&%s.cfg exam.c [root@dcalab nixser!er "#$ ls &l total 151)B &rw&r&&r&& 1 root root &rw&r&&r&& 1 root root &rw&r&&rb1 root root &rw&r&&rb1 root root drwxr&xr&x * root root &rw&r&&r&& 1 root root &rw&r&&r&& 1 root root *

pser.c

strclii.c

6ct *1 1':)5 *

10@ ; g 1B 1B:)5 ; 11' ; g 1B 1B:)@ a.c A5 6ct *1 1':0@ add1.sh

)0@5 :an . 1':*B =es%top 1*1 6ct *1 1':1* e*.c 11B 6ct *1 1':1A e'.c

%. MO&' COMMANDS$ 0his command is sed to mo!e the files and directories one place to another place

Syntax: $ m? (option) Source !ileC1irectory" Target !ileC1irectory" 6ptions: E.n o 1. 6ption &b D nction

0he file1 mo!e to file* with bac% p. *. &f 0he file1 mo!e to file* with forcibly. '. &if 0he file1 mo!e to file* with interacti!e and forcibly mode. 0hese are basically ' types 1. Dile to Dile *. Dile to =irectory '. =irectory to =irectory 1. 5ile to 5ile

Syntax: $ m? (option) Source !ile" Target !ile" [root@dcalab nixser!er "#$ m! old new [root@dcalab nixser!er "#$ cat old cat: old: <o s ch file or directory [root@dcalab nixser!er "#$ cat new hello hw r this is a boo% Ex: +m! &b file1 file* 4ote: The !ile1 mo?e to !ile& >ith backup Ex: + m! &f file1 file* 4ote: The !ile1 mo?e to !ile& >ith !orcibly Ex: + m! &if file1 file* 4ote: The !ile1 mo?e to !ile& >ith interacti?e and !orcibly mode &. 5ile to 1irectory Syntax: $ m? (option) Source !ile" Target 1irectory" Ex: [root@dcalab nixser!er "#$ cd sril [root@dcalab nixser!er sril #$ ls dir1 dir* f1 f* f' [root@dcalab nixser!er sril #$ cd [root@dcalab nixser!er "#$ m! f) .Nsril Nf) [root@dcalab nixser!er "#$ cd sril [root@dcalab nixser!er sril #$ ls dir1 dir* f1 f* f' f) 4ote: The !ile1 mo?es to 1irectory A1ir1B Ex: + m! &b file1 =ir1 4ote: The !ile1 mo?e to 1ir1 >ith backup mode Ex: +m! &f file1 =ir1 4ote: The !ile1 mo?e to 1ir1 >ith !orcibly mode Ex: +m! &if file1 =ir1 4ote: The !ile1 mo?e to 1ir1 >ith interacti?e and !orcibly mode.

'. 1irectory to 1irectory Syntax: $ m? (option) Source 1irectory" Target 1irectory" Ex: + [root@dcalab nixser!er sril #$ ls dir1 dir* f1 f* f' [root@dcalab nixser!er sril #$ m! dir1 dir* [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f) [root@dcalab nixser!er dir*#$ ls dir1 f* f' Ex: [root@dcalab nixser!er sril #$ cp .Ndir*Ndir1N2 ..N cp: o!erwrite \..Nf1U` y [root@dcalab nixser!er sril #$ ls &a . .. dir* f1 f* f' f) 4ote: The content o! 1ir1 mo?es to 1ir& Ex: + m! &b =ir1 =ir* 4ote: The contents o! 1ir1 mo?e to 1ir& >ith backup Ex: + m! &f =ir1 =ir* 4ote: The contents o! 1ir1 mo?es to 1ir& >ith !orcibly Ex: + m! &if =ir1 =ir* 4ote: The contents o! 1ir1 mo?es to 1ir& >ith interacti?e and !orcibly mode.

-. CO() COMMANDS$ Xi%e m! command7 cp is sed to create new files or mo!e the contents of file to another location . Tnli%e mo!e7 howe!er7 cp lea!es the original file intact at its location. Syntax: $cp (option) source !ile" 1estination !ile" 6ptions 7 &a7 &&archi!e same as &dC &&preser!eFall &&bac% p[F>6<0C6X# ma%e a bac% p of each existing destination file &b &d li%e &&bac% p b t does not accept an arg ment same as &&no&dereference &&preser!eFlin%

&f7 &i7 &O &l7 &X7

&&force if an existing destination file cannot be opened7 remo!e it and try again &&interacti!e prompt before o!erwrite follow command&line symbolic lin%s &&lin% & lin% files instead of copying &&dereference & always follow symbolic lin%s

&J7 &&no&dereference 0hese are basically ' types 1. Dile to a Dile *. Dile to a =ictionary '. =irectory to =irectory

5ile to 5ile Syntax : $cp source !ile" 1estination !ile" Ex : [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f) [root@dcalab nixser!er sril #$ cp f) f. [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f) f. ....new file Ex :[root@dcalab nixser!er sril #$ cat f1 welcome to adithya college of technology [root@dcalab nixser!er sril #$ cat f* welcome to first mtech

*010&11 [root@dcalab nixser!er sril #$ cp f1 f* cp: o!erwrite \f*U` y [root@dcalab nixser!er sril #$ cat f* welcome to adithya college of technology ... Existing file 4ote: The content o! Mohn !ile o?er>rite >ith ?enkat Ex Q +cp &f !en%at % mar 4ote : %t is copy the !ile !orcibly ?enkat to kumar >ithout any permission Ex : +cp &i !en%at gandhi 4ote : %t is copy the !ile interacti?e mode ?enkat to kumar >ith permissions

&. 5ile to 1irectory Syntax N $ cp source !ile" 1estination 1irectory" Ex Q [root@dcalab nixser!er dir*#$ ls dir1 f* f' [root@dcalab nixser!er dir*#$ cd .. [root@dcalab nixser!er sril #$ cp f. dir* [root@dcalab nixser!er sril #$ cd dir* [root@dcalab nixser!er dir*#$ ls dir1 f* f' f. Ex : [root@dcalab nixser!er "#$ cp &d f1 sril [root@dcalab nixser!er "#$ cd sril [root@dcalab nixser!er sril #$ ls f1 4ote : %t copys the !1!ile to srilu 1irectory >ith backup

Ex : [root@dcalab nixser!er "#$ cp &bf f1 sril [root@dcalab nixser!er "#$ cd sril [root@dcalab nixser!er sril #$ ls f1 4ote N %t copys the !1 !ile to srilu 1irectory >ith !orcibly backup Ex : + [root@dcalab nixser!er "#$ cp &ibf f1 sril [root@dcalab nixser!er "#$ cd sril [root@dcalab nixser!er sril #$ ls f1 4ote : %t copys the !1 !ile to srilu 1irectory >ith backup interacti?ely and !orcibly Ex : [root@dcalab nixser!er sril #$ ls f1 [root@dcalab nixser!er sril #$ cd [root@dcalab nixser!er "#$ cp f1 f* f' sril [root@dcalab nixser!er "#$ cd sril [root@dcalab nixser!er sril #$ ls f1 f* f' 4ote : %t copies the no o! !ile to 1irectory '. 1irectory to 1irectory Syntax : $ cp Source 1irectory" 1estination 1irectory" Ex : + cp =ra!id Wang ly Ex : + cp &r 8ody [en%at 4ote : %t copies the 1irectory to 1irectory Aincluding all !iles and 1irectoriesB Ex : + cp &ir ,=irectory 1- ,=irectory *4ote : %t copies the 1irectory to 1irectory Aincluding >ith all !iles and directories >ith interacti?e modeB Ex : + cp &rf ,=ir 1 - ,=ir *- ,=ir '- ,=estination =irectory4ote : %t copies the multiple 1irectories >ith !orcibly and recessi?ely mode

.. rmAremo?ing a !ileB: 0he rm command is sed to delete a file. To =emo?e a !ile

SyntaxN $ rm (option) !ilename" 6ptions: Cemo!e ( nlin%) the DIXE(s). &f7 &&force ignore nonexistent files7 ne!er prompt &i7 &&interacti!e prompt before any remo!al &r7 &C7 &&rec rsi!e remo!e directories and their contents rec rsi!ely &!7 &&!erbose explain what is being done Ly defa lt7 rm does not remo!e directories. Tse the &&rec rsi!e (&r or &C) option to remo!e each listed directory7 too7 along with all of its contents. Ex: [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f) f. [root@dcalab nixser!er sril #$ rm f. rm: remo!e reg lar file \f.U` y [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f) To =emo?e Multiple 5iles Syntax: $ rm (option) !ilenames " Ex: + rm f1 f* f' f) To remo?e !iles 5orcibly Syntax: $ rm (option) !ilename" Ex: [root@dcalab nixser!er sril #$ rm &f f) [root@dcalab nixser!er sril #$ ls dir* f1 f* f' To =emo?e %nteracti?e Mode Syntax: $ rm (option) !ilename"

Ex: [root@dcalab nixser!er sril #$ ls dir* f1 f* f' [root@dcalab nixser!er sril #$ rm &i f' rm: remo!e reg lar file \f'U` n [root@dcalab nixser!er sril #$ ls dir* f1 f* f' To =emo?e 1irectory Syntax: $ rm (option) 1irectory 4ame" To =emo?e all 1irectories and subdirectories Syntax: $ rm -r 1irectory 4ame" Ex: $ [root@dcalabunixserver srilu]# rm -r dir3 rm: descend into directory `dir3'? y rm: remove directory `dir3/aaa'? y rm: descend into directory `dir3/d '? y rm: descend into directory `dir3/d /d3'? y rm: remove directory `dir3/d /d3/d!'? y rm: remove directory `dir3/d /d3'? y rm: remove directory `dir3/d '? y rm: remove directory `dir3'? y [root@dcalabunixserver srilu]# ls dir dir! "3 "# "$ "% "& To =emo?e 1irectories 5orcibly SyntaxN $ rm -r! 1irectory 4ame" Ex: $ [root@dcalabunixserver srilu]# rm -r" dir! [root@dcalabunixserver srilu]# ls dir "3 "# "$ "% "& 8. touch: 0he to ch command is sed to pdate the timestamp on the named file. 0he time stamp the last time the file was altered or accessed. Syntax: touch (option) !ilename 6ptions: &a change only the access time

&c7 &&no&create & do not create any files

&f &m

(ignored) change only the modification time

&r7 &&referenceFDIXE& se this file9s times instead of c rrent time

Ex: [root@dcalab nixser!er sril #$ ls dir* f1 f* f' [root@dcalab nixser!er sril #$ to ch f. [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f. 4ote: %! the !ile name issued as an argument does not eistsO touch creates it as an empty !ile. Ex: [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f. [root@dcalab nixser!er sril #$ to ch &c f5 [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f.

8. >c : wc command to determine the length of a gi!en file. Ly defa lt7 the o tp t shows the length in lines7 words7 characters. Syntax: $ >c (option) !ilename" 6ptions: &c7 &&bytes print the byte co nts &m7 &&chars print the character co nts &l7 &&lines print the newline co nts

&X7 &&max&line&length print the length of the longest line &w7 &&words print the word co nts Ex: [root@dcalab nixser!er sril #$ wc f1 ' 5 )1 f1 no of new line!$ Ex: [root@dcalab nixser!er sril #$ cat f1 welcome to adithya college of technology [root@dcalab nixser!er sril #$ wc &l f1 ' f1 8. lnAcreating linksB: ln command is sed to create lin%s for files and allowing them to be accessed by different names. >all the lin% f nction to create a lin% to a file. >all the lin% f nction to create a lin% named DIXE* to an existing DIXE1 1. Oard lin% *. Eoft Xin% Syntax: ln (option) !ile1 !ile& ln (option) !ile directory 6ptions: &b li%e &&bac% p b t does not accept an arg ment

&f7 &&force&remo!e existing destination files &n7 &&no&dereference&treat destination that is a symlin% to a directory as if it were a normal file &i7 &&interacti!e&prompt whether to remo!e destinations &s7 &&symbolic&ma%e symbolic lin%s instead of hard lin%s

1. Hard link: Oard lin% can be b ild single file system Oard lin% can recogniRed it lin%s same permissions and same siRe7 same i&node n mber If the data in so rce is loss we can get it from hard lin% i&node n mber is gi!en space allocated i&node n mbers can recogniRed different logical names Syntax : ln source !ile" target !ile" Ex: [root@dcalab nixser!er sril #$ ls &l total 15 drwxr&xr&x ' root root )0@5 :an 11 1*:). dir* &rw&r&&r&& 1 root root )1 :an 1* 0A:)0 f* &rw&r&&r&& 1 root root '1 :an 10 1':** f' &rw&r&&r&& 1 root root 0 :an 1* 0A:)0 f.

&rw&r&&r&& 1 root root )1 :an 1* 0A:)) f5 [root@dcalab nixser!er sril #$ ln f* f1 [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f. f5 [root@dcalab nixser!er sril #$ ln f* fA [root@dcalab nixser!er sril #$ ls &l total *) drwxr&xr&x ' root root )0@5 :an 11 1*:). dir* &rw&r&&r&& ' root root )1 :an 1* 0A:)0 f1 &rw&r&&r&& ' root root )1 :an 1* 0A:)0 f* &rw&r&&r&& 1 root root '1 :an 10 1':** f' &rw&r&&r&& 1 root root 0 :an 1* 0A:)0 f.

&rw&r&&r&& 1 root root )1 :an 1* 0A:)) f5 &rw&r&&r&& ' root root )1 :an 1* 0A:)0 fA

[root@dcalab nixser!er sril #$ ln f* fB [root@dcalab nixser!er sril #$ ls &l total *B drwxr&xr&x ' root root )0@5 :an 11 1*:). dir* &rw&r&&r&& ) root root )1 :an 1* 0A:)0 f1 &rw&r&&r&& ) root root )1 :an 1* 0A:)0 f* &rw&r&&r&& 1 root root '1 :an 10 1':** f' &rw&r&&r&& 1 root root 0 :an 1* 0A:)0 f.

&rw&r&&r&& 1 root root )1 :an 1* 0A:)) f5 &rw&r&&r&& ) root root )1 :an 1* 0A:)0 fA &rw&r&&r&& ) root root )1 :an 1* 0A:)0 fB [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f. f5 fA fB [root@dcalab nixser!er sril #$ rm f* rm: remo!e reg lar file \f*U` y [root@dcalab nixser!er sril #$ ls dir* f1 f' f. f5 fA fB

&.So!t link: It can be b ilt across the file system If the so rce file is delete we canUt retrie!e from target file Syntax : ln -s source !ile" target !ile" Ex: [root@dcalab nixser!er sril #$ ln &s f* f@ [root@dcalab nixser!er sril #$ ls &l total *B

drwxr&xr&x ' root root )0@5 :an 11 1*:). dir* &rw&r&&r&& ) root root )1 :an 1* 0A:)0 f1 &rw&r&&r&& ) root root )1 :an 1* 0A:)0 f* &rw&r&&r&& 1 root root '1 :an 10 1':** f' &rw&r&&r&& 1 root root 0 :an 1* 0A:)0 f.

&rw&r&&r&& 1 root root )1 :an 1* 0A:)) f5 &rw&r&&r&& ) root root )1 :an 1* 0A:)0 fA &rw&r&&r&& ) root root )1 :an 1* 0A:)0 fB lrwxrwxrwx 1 root root * :an 1* 0B:1* f@ &- f* [root@dcalab nixser!er sril #$ ls dir* f1 f* f' f. f5 fA fB [root@dcalab nixser!er sril #$ rm f* rm: remo!e reg lar file \f*U` y [root@dcalab nixser!er sril #$ ls dir* f1 f' f. f5 fA fB f@

9. unlink: se nlin% to delete the name and possibly the file it referMs to. Syntax:$ unlink !ilename" Ex: [root@dcalab nixser!er sril #$ ls dir* f1 f' f. f5 fA fB [root@dcalab nixser!er sril #$ nlin% f1 [root@dcalab nixser!er sril #$ ls dir* f' f. f5 fA fB 1F. mkdir E rmdir: (8a%ing and Cemo!ing directories) m%dir creates the new directory in which we can store files and other directories.

Syntax: $ mkdir (option) 1irectory name" 6ptions: &m7 &&modeF86=E set permission mode (as in chmod)7 not rwxrwxrwx & mas% &p7 &&parents no error if existing7 ma%e parent directories as needed &!7 &&!erbose print a message for each created directory &S7 &&contextF>6<0Ef0 (EEXin x) set sec rity context to >6<0Ef0 Ex: [root@dcalab nixser!er sril #$ m%dir dir' [root@dcalab nixser!er sril #$ ls dir* dir' f' f. f5 fA fB Create Multiple 1irectories Eyntax: + m%dir [option# ,=irectory namesEx: [root@dcalab nixser!er sril #$ ls dir* dir' f' f. f5 fA fB [root@dcalab nixser!er sril #$ m%dir dir) dir. [root@dcalab nixser!er sril #$ ls dir* dir' dir) dir. f' f. f5 fA fB Create Multi # le?el 1irectories Syntax: $ mkdir -p 1irectory names" Ex: [root@dcalab nixser!er dir'#$ m%dir &p d*Nd'Nd) [root@dcalab nixser!er dir'#$ ls d* [root@dcalab nixser!er dir'#$ cd d* [root@dcalab nixser!er d*#$ ls d' [root@dcalab nixser!er d*#$ cd d' [root@dcalab nixser!er d'#$ ls d) Create Multiple Sub directories Syntax: $ mkdir -p mes" 1irectory na

Ex: [root@dcalab nixser!er sril #$ m%dir &p dir'Naaa dir)Nbbb [root@dcalab nixser!er sril #$ cd dir' [root@dcalab nixser!er dir'#$ ls aaa d* [root@dcalab nixser!er dir'#$ cd .. [root@dcalab nixser!er sril #$ cd dir) [root@dcalab nixser!er dir)#$ ls bbb rmdir: sed to remo!e directories if they are empty. Syntax:rmdir (option) directory name" Ex: [root@dcalab nixser!er sril #$ ls dir* dir' dir) dir. f' f. f5 fA fB [root@dcalab nixser!er sril #$ rmdir dir. [root@dcalab nixser!er sril #$ ls dir* dir' dir) f' f. f5 fA fB 11. cdAchange directoryB: 0he cd command enables yo to mo!e aro nd with in the file system. Tsed witho t arg ments it ret rns to yo r home directory. 0o mo!e to another directory directory name is reI ired as an arg ment. Syntax: cd directory name" To Change the 1irectory Syntax: $ cd 1irectory name" Ex: [root@dcalab nixser!er dir'#$ cd aaa [root@dcalab nixser!er aaa#$ To change the directory !or>ard Syntax: $ cd .. Ex: [root@dcalab nixser!er aaa#$ cd .. [root@dcalab nixser!er dir'#$

To Mo?e parent 1irectory ArootB Syntax: $ cd

Ex: [root@dcalab nixser!er dir'#$ cd [root@dcalab nixser!er "#$

&. Shell programming i.<%M: To print the !actorial o! !irst n natural numbers. S2/=C7 C217:

[root@dcalab nixser!er sril #+cat factorial.sh echo 1Enter n !al e1 read n let iF1 let fF1 while [ +i &le +n # do let fFf2i let iFi/1 done echo 1 factorial of +n is +f1 [root@dcalab nixser!er sril #+ sh factorial.sh Enter n !al e . factorial of . is 1*0

ii.<%M: To list the !iles in the current directory to >hich the user has readO >rite and execute permissions. S2/=C7 C217:

[root@dcalab nixser!er sril #+ cat dirlist.sh ls - c $ cat c exec , c while read line do if test &f +lite then if test &r +line then if test &w +line then if test &x +line then echo +line fi fi fi fi done [root@dcalab nixser!er sril #+ sh dirlist.sh a.o t sample11.txt

iii.<%M: per!orm the !ollo>ing string operations. a. To extract a substring !rom a gi?en string. S2/=C7 C217:

[root@dcalab nixser!er sril #+ cat sample.sh echo 1enter any string1 read s echo 1enter range1 read n read m echo 1\+s(c t &c +m&+n\1 [root@dcalab nixser!er sril #+ sh sample.sh enter any string an sha enter range * . < sh

b. To !ind the length o! a gi?en string. S2/=C7 C217: echo 1enter string1 cat & (wc &c [root@dcalab nixser!er sril #+ sh len.sh enter string comp ters@ '. simulate the !ollo>ing C3/ scheduling algorithms. a. =ound =obin <%M: Drite a C program !or =2/41 =2P%4 C3/ scheduling algorithm.

S2/=C7 C217: $incl de,stdio.hmain() G int s[10#7p[10#7n7i7?7wiF07w[10#7t[10#7 st[10#7tI7tstF0Q int ttF07twF0Q float aw atQ printf(1enter no.of process1)Q scanf(1cd17_n)Q printf(1Pn enter time I an m1)Q scanf(1cd17_tI)Q printf(1Pn enter process_ser!ice time1)Q for(iF0Qi,nQi//) scanf(1cdcd17_p[i#7_s[i#)Q for(iF0Qi,nQi//) st[i#Fs[i#Q tstFtst/s[i#Q for(?F0Q?,tstQ?//) for(iF0Qi,nQi//) G if(s[i#-tI) G s[i#Fs[i#&tIQ w1Fw1/tIQ t[i#Fw1Q w[i#Ft[i#&st[i#Q H else if(s[i#^F0) G w1Fw1/tIQ t[i#Fw1Q w[i#Ft[i#&st[i#Q s[i#Fs[i#&tIQ H H for(iF0Qi,nQi//) G twFtw/w[i#Q ttFtt/t[i#Q H awFtwNnQ atFttNnQ printf(1processPtstPtwtPttt1)Q for(iF0Qi,nQi//) printf(1cdPtcdPtcdPtcd17p[i#7st[i#7w[i#7t[i#)Q

printf(1awtFcd17aw)Q printf(1attFcd17at)Q H %nput: enter no of process ' enter time I ant m * enter process_ser!ice time 1 ) * 5 ' * 2utput: process st >t tt 1 ) ) B * 5 5 1* ' * ) 5 ;wt F ).000000 att F B.000000

b. SQ5 <%M: Drite a C program !or SQ5 C3/ scheduling algorithm S2/=C7 C217:

$incl de,stdio.hmain() G int i7?7bt[10#7n7pt[10#7wt[10#7tt[10#7t7%7l7w1F07t1F0Q float at7awQ printf(3enter no of ?obs4)Q scanf(3cd47_n)Q printf(3enter b rst time4)Q for(iF0Qi,nQi//) scanf4(cd47_bt[i#)Q for(iF0Qi,nQi//) for(?F0Q?,nQ?//) if(bt[i#,bt[?#) G tFbt[i#Q bt[i#Fbt[?#Q bt[?#FtQ H for(iF0Qi,nQi//) G wt[i/1#Fbt[i#/wt[i#Q tt[i/1#Ftt[i#/bt[i#Q w1Fw1/wt[i#Q t1Ft1/tt[i#Q H awFw1NnQ atFt1NnQ printf(3PnbtPtwtPttt4)Q for(iF0Qi,nQi//) printf(3cdPtcdPtcdPn47bt[i#7wt[i#7tt[i#)Q printf(3awFcdPnatFcd47aw7at)Q getch()Q H %nput: Enter no of ?obs ) Enter b rst time . 1* B *0 2utput: Pt >t tt

. 0 . 1* . 1' B 1' *. *0 *. ). awF10.A.000 atF**.000000

c. 5C5S <%M: Drite a C program !or 5C5S C3/ scheduling algorithm

S2/=C7 C217: incl de,stdio.hmain() G int i7?7bt[10#7n7pt[10#7wt[10#7tt[10#7t7%7l7w1F07t1F0Q float at7awQ printf(3enter no of ?obs4)Q scanf(3cd47_n)Q printf(3enter b rst time4)Q for(iF0Qi,nQi//) scanf4(cd47_bt[i#)Q for(iF0Qi,nQi//) G wt[i/1#Fbt[i#/wt[i#Q tt[i/1#Ftt[i#/bt[i#Q w1Fw1/wt[i#Q t1Ft1/tt[i#Q H awFw1NnQ atFt1NnQ printf(3PnbtPtwtPttt4)Q for(iF0Qi,nQi//) printf(3cdPtcdPtcdPn47bt[i#7wt[i#7tt[i#)Q printf(3awFcdPnatFcd47aw7at)Q getch()Q H %nput: enter no of ?obs ' enter b rsttime 1* B *0 output: bt >t tt 1* 0 1* B 1* *0 *0 *0 )0 awF10.5555A0 atF*).00000 d. 3=%2=%TR <%M: Drite a C 3rogram !or priority C3/ scheduling algorithm.

S2/=C7 C217: $incl de,stdio.hmain() G int i7?7bt[10#7n7pt[10#7wt[10#7tt[10#7t7%7l7w1F07t1F0Q float at7awQ printf(3enter no of ?obs4)Q scanf(3cd47_n)Q printf(3enter b rst time4)Q for(iF0Qi,nQi//) scanf4(cd47_bt[i#)Q printf(3enter priority !al es4)Q for(iF0Qi,nQi//) scanf(3cd47_pt[i#)Q for(iF0Qi,nQi//) for(?F0Q?,nQ?//) if(pt[i#,pt[?#) G tFpt[i#Q pt[i#Fpt[?#Q pt[?#FtQ %Fbt[i#Q bt[i#Fbt[?#Q bt[?#F%Q H for(iF0Qi,nQi//) G wt[i/1#Fbt[i#/wt[i#Q tt[i/1#Ftt[i#/bt[i#Q w1Fw1/wt[i#Q t1Ft1/tt[i#Q H awFw1NnQ atFt1NnQ printf(3PnbtPtprorityPtwtPttt4)Q for(iF0Qi,nQi//) printf(3cdPtcdPtcdPtcdPn47bt[i#7pt[i#7wt[i#7tt[i#)Q printf(3awFcdPnatFcd47aw7at)Q getch()Q H %nput: Enter no of ?obs

) Enter b rsttime 10 * ) A Enter priority !al es ) * 1 ' 2utput: Pt priority >t tt ) 1 0 ) * * ) 5 A ' 5 1' 10 ) 1' *' awF..A.0000 atF1*..00000

,. >rite programs !or !orkO ?!ork.

<%M: Drite a C program that illustrates the creation o! child process using !orkA B system call <lgorithm: 1. Etart *. =eclare pid '. create new process sing for%( ) system call ). If pid^F0 then .. =isplay parent process getpid()7getppid(). 5. Else A. =isplay child process getpid().getppid(). B. End S2/=C7 C217: $incl de,stdio.hint main( ) G printf(3original process with pid cd ppid cdPn47 getpid() 7getppid())Q pidFfor%()Q if(pid^F0) printf(3parent process with pid cd ppid cd Pn47 getpid()7getppid())Q else G sleep(.)Q printf(3child process with pid cd ppid cdPn47 getpid()7getppid())Q H printf(3 pid cd terminates 37getpid())Q H 2utput: original process with pid ').5 and ppid '.*. child process with pid ').A and ppid ').5 pid ').A terminates parent process with pid ').5 and ppid '.*. pid ').5 terminates <%M: Drite a C program that illustrates the creation o! child process using ?!orkA B system call

<lgorithm: 1. Etart *. =eclare pid '. create new process sing !for%( ) system call ). If pid^F0 then .. =isplay parent process getpid()7getppid(). 5. Else A. =isplay child process getpid().getppid(). B. End S2/=C7 C217: $incl de,stdio.hint main( ) G printf(3original process with pid cd ppid cdPn47 getpid() 7getppid())Q pidF!for%()Q if(pid^F0) printf(3parent process with pid cd ppid cd Pn47 getpid()7getppid())Q else G sleep(.)Q printf(3child process with pid cd ppid cdPn47 getpid()7getppid())Q H printf(3 pid cd terminates 37getpid())Q H 2utput: original process with pid ').5 and ppid '.*. child process with pid ').A and ppid ').5 pid ').A terminates parent process with pid ').5 and ppid '.*. pid ').5 terminates

Potrebbero piacerti anche