Upload
testerasa-tester
View
233
Download
0
Embed Size (px)
Citation preview
8/13/2019 The Linux Command Line-09.12
1/521
The Linux
Command Line
William E. Shotts, Jr.
A LinuxCommand.org Book
8/13/2019 The Linux Command Line-09.12
2/521
Copyright 2008-2009, William E. Shotts, Jr.
This work is licese! "!er the Creati#e Commos $ttri%"tio-&ocommercial-&o'eri#ati#e Works (.0 )ite! States *icese. To #iew a copy o+ this licese, #isit
httpcreati#ecommos.orgliceses%y-c-!(.0"s or se! a letter to Creati#eCommos, / Seco! Street, S"ite (00, Sa racisco, Cali+oria, 910, )S$.
*i"3 is the registere! tra!emark o+ *i"s Tor#al!s. $ll other tra!emarks %elog totheir respecti#e owers.
This %ook is part o+ the *i"3Comma!.org pro4ect, a site +or *i"3 e!"catio a!a!#ocacy !e#ote! to helpig "sers o+ legacy operatig systems migrate ito the +"t"re.5o" may cotact the *i"3Comma!.org pro4ect at httpli"3comma!.org.
6rite! copies o+ this %ook, i large, easy-to-rea!-+ormat, are a#aila%le +orp"rchase +rom l"l".com. 7r!ers may %e place! at the +ollowig
httpwww.l"l".comcotetpaper%ack-%ookthe-li"3-comma!-lie/9181
Release History
Version Date Description
09.2 'ecem%er 1, 2009 irst E!itio.
09. &o#em%er 9, 2009 o"rth !ra+t with almost all re#iewer +ee!%ackicorporate! a! e!ite! thro"gh chapter (/.
09.0 7cto%er (, 2009 Thir! !ra+t with re#ise! ta%le +ormattig,
partial applicatio o+ re#iewers +ee!%ack a!e!ite! thro"gh chapter 8.
09.08 $"g"st 2, 2009 Seco! !ra+t icorporatig the +irst e!itigpass.
09.0/ J"ly 8, 2009 Complete! +irst !ra+t.
http://creativecommons.org/licenses/by-nc-nd/3.0/us/http://linuxcommand.org/http://linuxcommand.org/http://www.lulu.com/content/paperback-book/the-linux-command-line/7594184http://linuxcommand.org/http://www.lulu.com/content/paperback-book/the-linux-command-line/7594184http://creativecommons.org/licenses/by-nc-nd/3.0/us/8/13/2019 The Linux Command Line-09.12
3/521
Table of Contents
Part 1 Introduction.........................................................................1
1 Introduction................................................................................................2
Why Use The Command Line?.......................................................................................2What This Book Is About.................................................................................................3Who Shoud !ead This Book..........................................................................................3What"s In This Book.........................................................................................................#$o% To !ead This Book..................................................................................................&
'rere(uisites...............................................................................................................&Why I )on"t Ca It *+,U-Linux.............................................................................&
A/kno%edgments...........................................................................................................01our eedba/k Is ,eeded..............................................................................................4urther !eading...............................................................................................................4Coo5hon.........................................................................................................................4
Part 2 Learning The Shell..............................................................
2 What Is The Shell?...................................................................................10Termina 6muators........................................................................................................781our irst 9eystrokes....................................................................................................78
Command $istory.....................................................................................................77Cursor :o;ement.....................................................................................................77
A e% Words About :i/e And o/us...................................................................77Try Some Sim5e Commands........................................................................................726nding A Termina Session............................................................................................73
The Consoe Behind The Curtain........................................................................73urther !eading.............................................................................................................73
3 Navigation.................................................................................................1
Understanding The ie System Tree............................................................................7#The Current Working )ire/tory......................................................................................7#Listing The Contents
i
8/13/2019 The Linux Command Line-09.12
4/521
Im5ortant a/ts About ienames........................................................................7>
!"#loring The S$ste%.............................................................................20
:ore un With s...........................................................................................................28
8/13/2019 The Linux Command Line-09.12
5/521
%hatis )is5ay A ery Brie= )es/ri5tion
8/13/2019 The Linux Command Line-09.12
6/521
Com5etion....................................................................................................................>0'rogrammabe Com5etion..................................................................................>>
Using $istory.................................................................................................................>>Sear/hing $istory.....................................................................................................>@$istory 6x5ansion.....................................................................................................@7
s/ri5t....................................................................................................................@7
Summing U5..................................................................................................................@2urther !eading.............................................................................................................@2
10 er%issions............................................................................................3
8/13/2019 The Linux Command Line-09.12
7/521
:odi=ying The 6n;ironment.........................................................................................73#Whi/h ies Shoud We :odi=y?.............................................................................73#Text 6ditors.............................................................................................................73#Using A Text 6ditor.................................................................................................73&
Why Comments Are Im5ortant..........................................................................73>A/ti;ating
Part ' Common Tas(s #nd $ssential Tools.............................1)
1& ac,age 'anage%ent..........................................................................10
#
8/13/2019 The Linux Command Line-09.12
8/521
'a/kaging Systems.....................................................................................................748$o% A 'a/kage System Works...................................................................................747
'a/kage ies.........................................................................................................747!e5ositories............................................................................................................747)e5enden/ies.........................................................................................................742$igh And Lo%e;e 'a/kage Toos........................................................................742
Common 'a/kage :anagement Tasks.......................................................................743inding A 'a/kage In A !e5ository........................................................................743Instaing A 'a/kage rom A !e5ository.................................................................743Instaing A 'a/kage rom A 'a/kage ie.............................................................74#!emo;ing A 'a/kage.............................................................................................74#U5dating 'a/kages rom A !e5ository..................................................................74&U5grading A 'a/kage rom A 'a/kage ie...........................................................74&Listing Instaed 'a/kages......................................................................................740)etermining I= A 'a/kage Is Instaed.....................................................................740)is5aying In=o About An Instaed 'a/kage...........................................................744inding Whi/h 'a/kage Instaed A ie.................................................................744
Summing U5................................................................................................................744The Linux So=t%are Instaation :yth.................................................................74>
urther !eading..........................................................................................................74@
1+ Storage 'edia.......................................................................................1-0
:ounting And Unmounting Storage )e;i/es..............................................................7>8ie%ing A List 2
Why Unmounting Is Im5ortant...........................................................................7>&)etermining )e;i/e ,ames....................................................................................7>0
Creating ,e% ie Systems.........................................................................................7>@:ani5uating 'artitions With =disk..........................................................................7>@Creating A ,e% ie System With mk=s..................................................................7@2
Testing And !e5airing ie Systems............................................................................7@3What The =s/k?..................................................................................................7@3
ormatting o55y )isks..............................................................................................7@3:o;ing )ata )ire/ty To-rom )e;i/es.......................................................................7@#Creating C)!
8/13/2019 The Linux Command Line-09.12
9/521
=t5............................................................................................................................28#=t5 A Better =t5.....................................................................................................280
%get........................................................................................................................280Se/ure Communi/ation With !emote $osts...............................................................280
ssh..........................................................................................................................284Tunneing With SS$..........................................................................................278
s/5 And s=t5............................................................................................................277An SS$ Cient or Windo%s?............................................................................272
urther !eading..........................................................................................................272
1- Searching (or (iles..............................................................................213
o/ate ind ies The 6asy Way...............................................................................273Where )oes The o/ate )atabase Come rom?..............................................27&
=ind ind ies The $ard Way...................................................................................27&Tests.......................................................................................................................270'rede=ined A/tions..................................................................................................227User )e=ined A/tions..............................................................................................223
Im5ro;ing 6==i/ien/y...............................................................................................22#xargs.......................................................................................................................22#)eaing With unny ienames..........................................................................22&
A !eturn To The 'ayground..................................................................................220
urther !eading..........................................................................................................22@
1 )rchiving )nd 6ac,u#.........................................................................230
Com5ressing ies.......................................................................................................238gHi5.........................................................................................................................237bHi52.......................................................................................................................233
)ont Be Com5ressi;e Com5usi;e...................................................................23#Ar/hi;ing ies.............................................................................................................23#
tar............................................................................................................................23#Hi5...........................................................................................................................2#8
Syn/hroniHing ies And )ire/tories............................................................................2#2Using rsyn/
8/13/2019 The Linux Command Line-09.12
10/521
'Aternation...................................................................................................................2&@Duanti=iers...................................................................................................................208
? :at/h An 6ement Kero
8/13/2019 The Linux Command Line-09.12
11/521
=mt A Sim5e Text ormatter................................................................................3735r ormat Text or 'rinting..................................................................................3745rint= ormat And 'rint )ata................................................................................37>
)o/ument ormatting Systems...................................................................................327gro==.........................................................................................................................322
Summing U5................................................................................................................32>
urther !eading..........................................................................................................32>
23 rinting..................................................................................................330
A Brie= $istory
$o% To Write A She S/ri5t.........................................................................................3&>S/ri5t ie ormat........................................................................................................3&@6xe/utabe 'ermissions..............................................................................................308S/ri5t ie Lo/ation......................................................................................................308
+ood Lo/ations or S/ri5ts....................................................................................302:ore ormatting Tri/ks................................................................................................302
Long
8/13/2019 The Linux Command Line-09.12
12/521
Indentation And Line Continuation.........................................................................302Con=iguring ;im or S/ri5t Writing.....................................................................303
Summing U5................................................................................................................30#urther !eading..........................................................................................................30#
2+ Starting ) ro9ect.................................................................................3+&
irst StageQ :inima )o/ument...................................................................................30&Se/ond StageQ Adding A Litte )ata............................................................................304ariabes And Constants.............................................................................................30>
Assigning aues To ariabes And Constants.......................................................347$ere )o/uments.........................................................................................................342Summing U5................................................................................................................34&urther !eading..........................................................................................................34&
2 To#:*o5n *esign.................................................................................3+
She un/tions............................................................................................................344Lo/a ariabes............................................................................................................3>89ee5 S/ri5ts !unning..................................................................................................3>7
She un/tions In 1our .bashr/ ie..................................................................3>#Summing U5................................................................................................................3>#urther !eading..........................................................................................................3>#
2- (lo5 Control; 6ranching With if.........................................................3-&
i=...................................................................................................................................3>&6xit Status...................................................................................................................3>0test...............................................................................................................................3>>
ie 6x5ressions.....................................................................................................3>>String 6x5ressions..................................................................................................3@7Integer 6x5ressions................................................................................................3@2
A :ore :odern ersion
:enus..........................................................................................................................#8@Summing U5................................................................................................................#77
6xtra Credit.............................................................................................................#77urther !eading...........................................................................................................#77
30 (lo5 Control;
8/13/2019 The Linux Command Line-09.12
13/521
Loo5ing........................................................................................................................#72%hie........................................................................................................................#72
Breaking
31 Troubleshooting...................................................................................1
Synta/ti/ 6rrors...........................................................................................................#7@:issing Duotes.......................................................................................................#28:issing
8/13/2019 The Linux Command Line-09.12
14/521
3& Strings )nd Nu%bers..........................................................................&
'arameter 6x5ansion..................................................................................................#&4Basi/ 'arameters...................................................................................................#&46x5ansions To :anage 6m5ty ariabes...............................................................#&>6x5ansions That !eturn ariabe ,ames..............................................................#08
String 0
Tra5s............................................................................................................................#>>Tem5orary ies.................................................................................................#@7
Asyn/hronous 6xe/ution.............................................................................................#@2%ait.........................................................................................................................#@2
,amed 'i5es...............................................................................................................#@3Setting U5 A ,amed 'i5e.......................................................................................#@#Using ,amed 'i5es................................................................................................#@#
Summing U5................................................................................................................#@&urther !eading..........................................................................................................#@&
Inde"..............................................................................................................+
3ii
8/13/2019 The Linux Command Line-09.12
15/521
6art tro!"ctio
'art 7 Introdu/tion
8/13/2019 The Linux Command Line-09.12
16/521
tro!"ctio
1 Introduction
wat to tell yo" a story.
&o, ot the story o+ how, i 99, *i"s Tor#al!s wrote the +irst #ersio o+ the *i"3kerel. 5o" ca rea! that story i lots o+ *i"3 %ooks. &or am goig to tell yo" thestory o+ how, some years earlier, :ichar! Stallma %ega the ;&) 6ro4ect to create a +ree
)i3-like operatig system. Thatay people speak o+ ?+ree!om@ with regar! to *i"3, %"t !o
8/13/2019 The Linux Command Line-09.12
17/521
Why )se The Comma! *ie
the comp"ter, he e#er to"ches a mo"se tay other %ooks try to %roa!e their appeal %yicl"!ig other plat+orms s"ch as geeric )i3 a! >ac7S G. !oig so, they ?water!ow@ their cotet to +eat"re oly geeral topics. This %ook, o the other ha!, olyco#ers cotemporary *i"3 !istri%"tios. &iety-+i#e percet o+ the cotet is "se+"l +or"sers o+ other )i3-like systems, %"t this %ook is highly targete! at the mo!er *i"3comma! lie "ser.
+ho Should ead This /oo(
This %ook is +or ew *i"3 "sers who ha#e migrate! +rom other plat+orms. >ost likelyyo" are a ?power "ser@ o+ some #ersio o+ >icroso+t Wi!ows. 6erhaps yo"r %oss hastol! yo" to a!miister a *i"3 ser#er, or may%e yo"
8/13/2019 The Linux Command Line-09.12
18/521
tro!"ctio
all the sec"rity pro%lems a! wat to gi#e *i"3 a try. That
8/13/2019 The Linux Command Line-09.12
19/521
What,@isert the li#e C', a! re%oot. )sig a li#e C' is a great way to test a comp"ter+or *i"3 compati%ility prior to istallatio. The !isa!#atage o+ "sig a li#e C'is that it may %e #ery slow compare! to ha#ig *i"3 istalle! o yo"r har! !ri#e.oth )%"t" a! e!ora amog othersF ha#e li#e C' #ersios.
:egar!less o+ how yo" istall *i"3, yo" will ee! to ha#e occasioal s"per"ser i.e.,a!miistrati#eF pri#ileges to carry o"t the lessos i this %ook.
$+ter yo" ha#e a workig istallatio, start rea!ig a! +ollow alog with yo"r owcomp"ter. >ost o+ the material i this %ook is ?ha!s o,@ so sit !ow a! get typigL
+h, I Dont Call It 567-8Linux9 some ="arters, it
8/13/2019 The Linux Command Line-09.12
20/521
tro!"ctio
people i a #ast, !istri%"te! !e#elopmet e++ort. Techically speakig, *i"3 isthe ame o+ the operatig systemr. '#orak !escri%e! the process o+ writig ?Aell. Write 200 wor!s a !ay a!i a year, yo" ha#e a o#el.@ This a!#ice le! me to write a page a !ay "til ha! a %ook.
'mitri 6opo# wrote a article i ree So+tware >agaDie title!, ?Creatig a %ooktemplate with Writer,@ which ispire! me to "se 7pe7++ice.org Writer +or composig
the te3t. $s it t"re! o"t, it worke! wo!er+"lly.
>ark 6olesky per+orme! a e3traor!iary re#iew a! test o+ the te3t.
Jesse ecker, TomasD ChrDcDoowicD, >ichael *e#i, Spece >ier also teste! a!re#iewe! portios o+ the te3t.
K
8/13/2019 The Linux Command Line-09.12
21/521
$ckowle!gmets
Mare >. Shotts cotri%"te! a lot o+ ho"rs, polishig my so-calle! Eglish %y e!itig thete3t.
$! lastly, the rea!ers o+ *i"3Comma!.org, who ha#e set me so may ki! emails.
Their eco"ragemet ga#e me the i!ea that was really o to somethigL
:our ;eed0ac( Is 7eededore " With ls
'eskto7 'ocuments +usic ictures ublic Tem7lates ;ideos
6usr:bin games kerberos libexec sbin srcetc include lib local share tm7
We ca also chage the +ormat o+ the o"tp"t to re#eal more !etail
[me@linuxbox ~]$ ls -ltotal ".dr9xr9xr-x ! me me %(0. !(()-#(-!. #):!( 'eskto7dr9xr9xr-x ! me me %(0. !(()-#(-!. #):!( 'ocumentsdr9xr9xr-x ! me me %(0. !(()-#(-!. #):!( +usic
dr9xr9xr-x ! me me %(0. !(()-#(-!. #):!( icturesdr9xr9xr-x ! me me %(0. !(()-#(-!. #):!( ublicdr9xr9xr-x ! me me %(0. !(()-#(-!. #):!( Tem7latesdr9xr9xr-x ! me me %(0. !(()-#(-!. #):!( ;ideos
y a!!ig ?-l@ to the comma!, we chage! the o"tp"t to the log +ormat.
ost comma!s "se optios cosistig o+ a sigle character prece!e! %y a !ash, +ore3ample, ?-l@, %"t may comma!s, icl"!ig those +rom the ;&) 6ro4ect, also s"pportlong options, cosistig o+ a wor! prece!e! %y two !ashes. $lso, may comma!s allowm"ltiple short optios to %e str"g together. this e3ample, the lscomma! is gi#e
two optios, the ?l@ optio to pro!"ce log +ormat o"tp"t, a! the ?t@ optio to sort theres"lt %y the +ile
8/13/2019 The Linux Command Line-09.12
36/521
1 E3plorig The System
We
8/13/2019 The Linux Command Line-09.12
37/521
>ore " With ls
-r9-r--r-- # root root ").!0. !(()-(%-( ##:(" &x7erience ubuntu
8/13/2019 The Linux Command Line-09.12
38/521
1 E3plorig The System
file filename
Whe i#oke!, the filecomma! will prit a %rie+ !escriptio o+ the +ile6( a! J6E;, thereare may ki!s that are a little less o%#io"s a! a +ew that are ="ite strage.
Vie4ing ;ile Contents +ith less
The lesscomma! is a program to #iew te3t +iles. Thro"gho"t o"r *i"3 system, there
are may +iles that cotai h"ma-rea!a%le te3t. The less program pro#i!es a
co#eiet way to e3amie them.
+hat Is 5Text9
There are may ways to represet i+ormatio o a comp"ter. $ll metho!si#ol#e !e+iig a relatioship %etwee the i+ormatio a! some "m%ers thatwill %e "se! to represet it. Comp"ters, a+ter all, oly "!ersta! "m%ers a! all!ata is co#erte! to "meric represetatio.
Some o+ these represetatio systems are #ery comple3 s"ch as compresse!#i!eo +ilesF, while others are rather simple. 7e o+ the earliest a! simplest iscalle!)S%** te+t. $SC proo"ce! Q$s-MeyQF is short +or $merica Sta!ar!Co!e +or +ormatio terchage. This is a simple eco!ig scheme that was +irst"se! o Teletype machies to map key%oar! characters to "m%ers.
Te3t is a simple oe-to-oe mappig o+ characters to "m%ers. t is #ery compact.
i+ty characters o+ te3t traslates to +i+ty %ytes o+ !ata. t is importat to"!ersta! that te3t oly cotais a simple mappig o+ characters to "m%ers. tis ot the same as a wor! processor !oc"met s"ch as oe create! %y >icroso+tWor! or 7pe7++ice.org Writer. Those +iles, i cotrast to simple $SC te3t,
21
8/13/2019 The Linux Command Line-09.12
39/521
Riewig ile Cotets With less
cotai may o-te3t elemets that are "se! to !escri%e its str"ct"re a!+ormattig. 6lai $SC te3t +iles cotai oly the characters themsel#es a! a
+ew r"!imetary cotrol co!es like ta%s, carriage ret"rs a! lie +ee!s.
Thro"gho"t a *i"3 system, may +iles are store! i te3t +ormat a! there aremay *i"3 tools that work with te3t +iles. E#e Wi!ows recogiDes theimportace o+ this +ormat. The well-kow &7TE6$'.EGE program is a e!itor+or plai $SC te3t +iles.
Why wo"l! we wat to e3amie te3t +iles eca"se may o+ the +iles that cotai systemsettigs calle! configuration filesF are store! i this +ormat, a! %eig a%le to rea! themgi#es "s isight a%o"t how the system works. a!!itio, may o+ the act"al programsthat the system "ses calle! scriptsF are store! i this +ormat. later chapters, we will
lear how to e!it te3t +iles i or!er to mo!i+y systems settigs a! write o"r ow scripts,%"t +or ow we will 4"st look at their cotets.
The lesscomma! is "se! like this
less filename
7ce starte!, the lessprogram allows yo" to scroll +orwar! a! %ackwar! thro"gh a
te3t +ile. or e3ample, to e3amie the +ile that !e+ies all the system
8/13/2019 The Linux Command Line-09.12
40/521
1 E3plorig The System
'ow $rrow Scroll !ow oe lie
>o#e to the e! o+ the te3t +ile
#or g >o#e to the %egiig o+ the te3t +ile
6characters Search +orwar! to the e3t occ"rrece o+ characters
n Search +or the e3t occ"rrece o+ the pre#io"s search
h 'isplay help scree
B "it less
Less Is >oreThe lessprogram was !esige! as a impro#e! replacemet o+ a earlier )i3
program calle! more. The ame ?less@ is a play o the phrase ?less is more@Bamotto o+ mo!erist architects a! !esigers.
less+alls ito the class o+ programs calle! ?pagers,@ programs that allow the
easy #iewig o+ log te3t !oc"mets i a page %y page maer. Whereas themoreprogram co"l! oly page +orwar!, the lessprogram allows pagig %oth
+orwar! a! %ackwar! a! has may other +eat"res as well.
# 6uided Tour
The +ile system layo"t o yo"r *i"3 system is m"ch like that +o"! o other )i3-likesystems. The !esig is act"ally speci+ie! i a p"%lishe! sta!ar! calle! the (inu+Filesystem ,ierarchy Standard. &ot all *i"3 !istri%"tios co+orm to the sta!ar!e3actly %"t most come pretty close.
&e3t, we are goig to wa!er aro"! the +ile system o"rsel#es to see what makes o"r*i"3 system tick. This will gi#e yo" a chace to practice yo"r a#igatio skills. 7e o+the thigs we will !isco#er is that may o+ the iterestig +iles are i plai h"ma-rea!a%le te3t. $s we go a%o"t o"r to"r, try the +ollowig
. cdito a gi#e !irectory
2. *ist the !irectory cotets with ls -l
(. + yo" see a iterestig +ile, !etermie its cotets with file
1. + it looks like it might %e te3t, try #iewig it with less
2K
8/13/2019 The Linux Command Line-09.12
41/521
$ ;"i!e! To"r
Remember the co(y and (aste trick + yo" are "sig a mo"se, yo" ca !o"%leclick o a +ileame to copy it a! mi!!le click to paste it ito comma!s.
$s we wa!er aro"!, !o
8/13/2019 The Linux Command Line-09.12
42/521
1 E3plorig The System
Director, Comments
6home ormal co+ig"ratios, each "ser is gi#e a !irectory i6home. 7r!iary "sers ca oly write +iles i their home
!irectories. This limitatio protects the system +rom errat"ser acti#ity.
6lib Cotais share! li%rary +iles "se! %y the core systemprograms. These are similar to '**s i Wi!ows.
6lost8found Each +ormatte! partitio or !e#ice "sig a *i"3 +ile system,s"ch as e3t(, will ha#e this !irectory. t is "se! i the caseo+ a partial reco#ery +rom a +ile system corr"ptio e#et.)less somethig really %a! has happee! to yo"r system,this !irectory will remai empty.
6media 7 mo!er *i"3 systems the 6media!irectory willcotai the mo"t poits +or remo#a%le me!ia s"ch )S!ri#es, C'-:7>s, etc. that are mo"te! a"tomatically atisertio.
6mnt 7 ol!er *i"3 systems, the 6mnt!irectory cotais mo"t
poits +or remo#a%le !e#ices that ha#e %ee mo"te!ma"ally.
6o7t The 6o7t!irectory is "se! to istall ?optioal@ so+tware.
This is maily "se! to hol! commercial so+tware pro!"ctsthat may %e istalle! o yo"r system.
67roc The 67roc!irectory is special. t
8/13/2019 The Linux Command Line-09.12
43/521
$ ;"i!e! To"r
Director, Comments
6usr The 6usr!irectory tree is likely the largest oe o a *i"3
system. t cotais all the programs a! s"pport +iles "se!%y reg"lar "sers.
6usr6bin 6usr6bincotais the e3ec"ta%le programs istalle! %y
yo"r *i"3 !istri%"tio. t is ot "commo +or this!irectory to hol! tho"sa!s o+ programs.
6usr6lib The share! li%raries +or the programs i 6usr6bin.
6usr6local The 6usr6localtree is where programs that are ot
icl"!e! with yo"r !istri%"tio %"t are ite!e! +or system-wi!e "se are istalle!. 6rograms compile! +rom so"rce co!eare ormally istalle! i 6usr6local6bin. 7 a ewly
istalle! *i"3 system, this tree e3ists, %"t it will %e empty"til the system a!miistrator p"ts somethig i it.
6usr6sbin Cotais more system a!miistratio programs.
6usr6share 6usr6sharecotais all the share! !ata "se! %y
programs i 6usr6bin. This icl"!es thigs like !e+a"lt
co+ig"ratio +iles, icos, scree %ackgro"!s, so"! +iles,etc.
6usr6share6doc >ost packages istalle! o the system will icl"!e someki! o+ !oc"metatio. 6usr6share6doc, we will
+i! !oc"metatio +iles orgaiDe! %y package.6var With the e3ceptio o+ 6tm7a! 6home, the !irectories we
ha#e looke! at so +ar remai relati#ely static, that is, theircotets !o
8/13/2019 The Linux Command Line-09.12
44/521
1 E3plorig The System
lr9xr9xr9x # root root ## !(()-(/-## ():% libc
8/13/2019 The Linux Command Line-09.12
45/521
>aip"latig iles $! 'irectories
& 'ani#ulating (iles )nd *irectories
$t this poit, we are rea!y +or some real workL This chapter will itro!"ce the +ollowigcomma!s
c7 Copy +iles a! !irectories
mv >o#ereame +iles a! !irectories
mkdir Create !irectories
rm :emo#e +iles a! !irectories
ln Create har! a! sym%olic liks
These +i#e comma!s are amog the most +re="etly "se! *i"3 comma!s. They are"se! +or maip"latig %oth +iles a! !irectories.
&ow, to %e +rak, some o+ the tasks per+orme! %y these comma!s are more easily !oewith a graphical +ile maager. With a +ile maager, we ca !rag a! !rop a +ile +rom oe!irectory to aother, c"t a! paste +iles, !elete +iles, etc. So why "se these ol! comma!
lie programsThe aswer is power a! +le3i%ility. While it is easy to per+orm simple +ile maip"latioswith a graphical +ile maager, complicate! tasks ca %e easier with the comma! lieprograms. or e3ample, how co"l! we copy all the AT>* +iles +rom oe !irectory toaother, %"t oly copy +iles that !o ot e3ist i the !estiatio !irectory or are ewer thathe #ersios i the !estiatio !irectory 6retty har! with with a +ile maager. 6retty easywith the comma! lie
cp -u *.html destination
+ildcards
e+ore we %egi "sig o"r comma!s, we ee! to talk a%o"t a shell +eat"re that makesthese comma!s so power+"l. Sice the shell "ses +ileames so m"ch, it pro#i!es specialcharacters to help yo" rapi!ly speci+y gro"ps o+ +ileames. These special characters are
(
8/13/2019 The Linux Command Line-09.12
46/521
>aip"latig iles $! 'irectories
calle! wildcards. )sig wil!car!s which is also kow as globbingF allow yo" to select+ileames %ase! o patters o+ characters. The ta%le %elow lists the wil!car!s a! whatthey select
Table .-1: /ildcards
+ildcard >eaning
E >atches ay characters
F >atches ay sigle character
[characters] >atches ay character that is a mem%er o+ the set characters
[Gcharacters] >atches ay character that is ot a mem%er o+ the setcharacters
[[:class:]] >atches ay character that is a mem%er o+ the speci+ie!class
Ta%le -2 lists the most commoly "se! character classes
Table .-': %ommonly 0sed %haracter %lasses
Character Class >eaning
[:alnum:] >atches ay alpha"meric character
[:al7ha:] >atches ay alpha%etic character
[:digit:] >atches ay "meral
[:lo9er:] >atches ay lowercase letter
[:u77er:] >atches ay "ppercase letter
)sig wil!car!s makes it possi%le to costr"ct #ery sophisticate! selectio criteria +or+ileames. Aere are some e3amples o+ patters a! what they match
Table .-3: /ildcard +amples
Pattern >atches
E $ll +iles
gE $y +ile %egiig with ?g@
bE
8/13/2019 The Linux Command Line-09.12
47/521
8/13/2019 The Linux Command Line-09.12
48/521
>aip"latig iles $! 'irectories
>ay i!eas origially +o"! i the comma! lie iter+ace make their way itothe graphical iter+ace, too. t is oe o+ the may thigs that make the *i"3
!esktop so power+"l.
mkdir Create Directories
The mkdircomma! is "se! to create !irectories. t works like this
mkdir directory...
# note on notation5Whe three perio!s +ollow a arg"met i the !escriptio o+ acomma! as a%o#eF, it meas that the arg"met ca %e repeate!, th"s
mkdir dir1
wo"l! create a sigle !irectory ame! ?!ir@, while
mkdir dir1 dir2 dir
wo"l! create three !irectories ame! ?!ir@, ?!ir2@, a! ?!ir(@.
cp Cop, ;iles #nd Directories
The c7comma! copies +iles or !irectories. t ca %e "se! two !i++eret ways
cp item1item2
to copy the sigle +ile or !irectory ?item@ to +ile or !irectory ?item2@ a!
cp item... directory
to copy m"ltiple items either +iles or !irectoriesF ito a !irectory.
(1
8/13/2019 The Linux Command Line-09.12
49/521
cp Copy iles $! 'irectories
Use=u eaning
-a> --archive Copy the +iles a! !irectories a! all o+ their attri%"tes,icl"!ig owerships a! permissios. &ormally,copies take o the !e+a"lt attri%"tes o+ the "serper+ormig the copy.
-i> --interactive e+ore o#erwritig a e3istig +ile, prompt the "ser +orco+irmatio. )" this o(tion is not s(eci"ied6 cp+ill
silently over+rite "iles.-r> --recursive :ec"rsi#ely copy !irectories a! their cotets. This
optio or the -aoptioF is re="ire! whe copyig
!irectories.
-u> --u7date Whe copyig +iles +rom oe !irectory to aother, olycopy +iles that either !o --verbose 'isplay i+ormati#e messages as the copy isper+orme!.
Table .-.: cp +amples
Command esults
c7 file1file2 Copy file1to file'. )" file2exists6 it is over+ritten+ith the contents o" file1. + file'!oes ot e3ist, itis create!.
c7 -i file1file2 Same as a%o#e, e3cept that i+ file'e3ists, the "ser isprompte! %e+ore it is o#erwritte.
c7 file1file2dir1 Copy file1a! file'ito !irectory dir1. dir1m"stalrea!y e3ist.
c7 dir#6E dir! )sig a wil!car!, all the +iles i dir1are copie!ito dir'. dir'm"st alrea!y e3ist.
(
8/13/2019 The Linux Command Line-09.12
50/521
>aip"latig iles $! 'irectories
c7 -r dir1dir2 Copy the cotets o+ !irectory dir1to !irectorydir'. + !irectory dir'!oes ot e3ist, it is create!a!, a+ter the copy, will cotai the same cotets
as !irectory dir1.+ !irectory dir'!oes e3ist, the !irectory dir1a!its cotetsF will %e copie! ito dir'.
mv >o%e #nd ename ;iles
The mvcomma! per+orms %oth +ile mo#ig a! +ile reamig, !epe!ig o how it is
"se!. either case, the origial +ileame o loger e3ists a+ter the operatio. mvis "se!
i m"ch the same way as c7
mv item1item2
to mo#e or reame +ile or !irectory ?item@ to ?item2@ or
mv item... directory
to mo#e oe or more items +rom oe !irectory to aother.
Use=u eaning
-i> --interactive e+ore o#erwritig a e3istig +ile, prompt the "ser +orco+irmatio. )" this o(tion is not s(eci"ied6 mv+ill
silently over+rite "iles.
-u> --u7date Whe mo#ig +iles +rom oe !irectory to aother, olymo#e +iles that either !o --verbose 'isplay i+ormati#e messages as the mo#e is
(K
8/13/2019 The Linux Command Line-09.12
51/521
m# >o#e $! :eame iles
per+orme!.
Table .-: mv +amples
Command esults
mv file1file2 >o#e file1to file'. )" file2exists6 it is over+ritten+ith the contents o" file1. + file'!oes ot e3ist, itis create!. )n either case6 file1ceases to exist.
mv -i file1file2 Same as a%o#e, e3cept that i+ file'e3ists, the "ser isprompte! %e+ore it is o#erwritte.
mv file1file2dir1 >o#e file1a! file'ito !irectory dir1. dir1m"stalrea!y e3ist.
mv dir1dir2 + !irectory dir'!oes ot e3ist, create !irectorydir'a! mo#e the cotets o+ !irectory dir1itodir'a! !elete !irectory dir1.+ !irectory dir'!oes e3ist, mo#e !irectory dir1a! its cotetsF ito !irectory dir'.
rm emo%e ;iles #nd Directories
The rmcomma! is "se! to remo#e !eleteF +iles a! !irectories
rm item...
where ?item@ is oe or more +iles or !irectories.
Use=u eaning
-i> --interactive e+ore !eletig a e3istig +ile, prompt the "ser +orco+irmatio. )" this o(tion is not s(eci"ied6 rm+ill
silently delete "iles.
(/
8/13/2019 The Linux Command Line-09.12
52/521
>aip"latig iles $! 'irectories
-r> --recursive :ec"rsi#ely !elete !irectories. This meas that i+ a!irectory %eig !elete! has s"%!irectories, !elete themtoo. To !elete a !irectory, this optio m"st %e
speci+ie!.
-f> --force gore oe3istet +iles a! !o ot prompt. Thiso#erri!es the --interactiveoptio.
-v> --verbose 'isplay i+ormati#e messages as the !eletio isper+orme!.
Table .-5: rm +amples
Command esults
rm file1 'elete file1siletly.rm -i file1 Same as a%o#e, e3cept that the "ser is prompte! +or
co+irmatio %e+ore the !eletio is per+orme!.
rm -r file1dir1 'eletefile1a! dir1a! its cotets.
rm -rf file1dir1 Same as a%o#e, e3cept that i+ either file1or dir1!oot e3ist, rmwill coti"e siletly.
/e Care"ul +ith rmaip"latig iles $! 'irectories
:o;ing And !enaming ies
&ow, the ame ?passw!@ !oes
8/13/2019 The Linux Command Line-09.12
57/521
*et
8/13/2019 The Linux Command Line-09.12
58/521
>aip"latig iles $! 'irectories
The lscomma! has a way to re#eal this i+ormatio. t is i#oke! with the ?-i@ optio
[me@linuxbox 7la1ground]$ ls -litotal #.#!""0 dr9xr9xr-x ! me me %(0. !((/-(#-#% #.:#) dir##!""%( dr9xr9xr-x ! me me %(0. !((/-(#-#% #.:#) dir!#!""/ -r9-r--r-- % me me #."( !((/-(#-#( #.: fun#!""/ -r9-r--r-- % me me #."( !((/-(#-#( #.: fun-hard
this #ersio o+ the listig, the +irst +iel! is the io!e "m%er a!, as we ca see, %othfun a! fun-hardshare the same io!e "m%er, which co+irms they are the same
+ile.
Creating Symboi/ LinksSym%olic liks were create! to o#ercome the two !isa!#atages o+ har! liks har! likscaot spa physical !e#ices a! har! liks caot re+erece !irectories, oly +iles.Sym%olic liks are a special type o+ +ile that cotais a te3t poiter to the target +ile or!irectory.
Creatig sym%olic liks is similar to creatig har! liks
[me@linuxbox 7la1ground]$ ln -s fun fun-s!m[me@linuxbox 7la1ground]$ ln -s ../fun dir1/fun-s!m[me@linuxbox 7la1ground]$ ln -s ../fun dir2/fun-s!m
The +irst e3ample is pretty straight+orwar!, we simply a!! the ?-s@ optio to create asym%olic lik rather tha a har! lik. "t what a%o"t the e3t two :emem%er, whe wecreate a sym%olic lik, we are creatig a te3t !escriptio o+ where the target +ile isrelati#e to the sym%olic lik. t
8/13/2019 The Linux Command Line-09.12
59/521
*et
8/13/2019 The Linux Command Line-09.12
60/521
>aip"latig iles $! 'irectories
[me@linuxbox 7la1ground]$ rm -i funrm: remove regular file JfunKF
Eter ?y@ at the prompt a! the +ile is !elete!. "t let
8/13/2019 The Linux Command Line-09.12
61/521
*etE a! M'E pro#i!e a easy a! a"tomaticmetho! o+ creatig sym%olic liks. With ;&7>E, hol!ig the CtrlVShi+t keyswhile !raggig a +ile will create a lik rather tha copyig or mo#igF the +ile. M'E, a small me" appears whee#er a +ile is !roppe!, o++erig a choice o+copyig, mo#ig, or likig the +ile.
Summing U5We
8/13/2019 The Linux Command Line-09.12
62/521
K Workig With Comma!s
+ Wor,ing With Co%%ands
)p to this poit, we ha#e see a series o+ mysterio"s comma!s, each with its owmysterio"s optios a! arg"mets. this chapter, we will attempt to remo#e some o+that mystery a! e#e create some o+ o"r ow comma!s. The comma!s itro!"ce! ithis chapter are
t17e !icate how a comma! ame is iterprete! 9hich 'isplay which e3ec"ta%le program will %e e3ec"te!
man 'isplay a comma!
8/13/2019 The Linux Command Line-09.12
63/521
!eti+yig Comma!s
Identi",ing Commands
t is o+te "se+"l to kow e3actly which o+ the +o"r ki!s o+ comma!s is %eig "se! a!*i"3 pro#i!es a co"ple o+ ways to +i! o"t.
t17e )is5ay A Command"s Ty5e
The t17ecomma! is a shell %"ilti that !isplays the ki! o+ comma! the shell will
e3ec"te, gi#e a partic"lar comma! ame. t works like this
t!pe command
where ?comma!@ is the ame o+ the comma! yo" wat to e3amie. Aere are some
e3amples
[me@linuxbox ~]$ t!pe t!pet17e is a shell builtin[me@linuxbox ~]$ t!pe lsls is aliased to Jls --colorMtt1K[me@linuxbox ~]$ t!pe cpc7 is 6bin6c7
Aere we see the res"lts +or three !i++eret comma!s. &otice that the oe +or lstake
+rom a e!ora systemF a! how the lscomma! is act"ally a alias +or the lscomma!
with the ?-- colortty@ optio a!!e!. &ow we kow why the o"tp"t +rom lsis !isplaye!
i colorL
9hich )is5ay An 6xe/utabe"s Lo/ation
Sometimes there is more tha oe #ersio o+ a e3ec"ta%le program istalle! o asystem. While this is ot #ery commo o !esktop systems, it
8/13/2019 The Linux Command Line-09.12
64/521
K Workig With Comma!s
e3ample, cd, we either get o respose or a error message
[me@linuxbox ~]$ which cd6usr6bin69hich: no cd inN6o7t6jre# thenC'4TS is not used< Af the director1 is not found> and the shello7tion JcdableOvarsK is set> then tr1 the 9ord as a variable name then cd to the value of that variableiscellaeo"s
8 System a!miistratio comma!s
Sometimes we ee! to look i a speci+ic sectio o+ the ma"al to +i! what we arelookig +or. This is partic"larly tr"e i+ we are lookig +or a +ile +ormat that is also theame o+ a comma!. Witho"t speci+yig a sectio "m%er, we will always get the +irstistace o+ a match, pro%a%ly i sectio . To speci+y a sectio "m%er, we "se manlike
this
man sectionsearch_term
or e3ample
[me@linuxbox ~]$ man " passwd
2
8/13/2019 The Linux Command Line-09.12
67/521
;ettig $ Comma!
8/13/2019 The Linux Command Line-09.12
68/521
K Workig With Comma!s
o#er eighty pages log a! e3tremely !ese, a! its str"ct"re makes a%sol"tely osese to a ew "ser.
7 the other ha!, it is #ery acc"rate a! cocise, as well as %eig e3tremelycomplete. So check it o"t i+ yo" !are a! look +orwar! to the !ay whe yo" carea! it a! it all makes sese.
info )is5ay A 'rogram"s In=o 6ntry
The ;&) 6ro4ect pro#i!es a alterati#e to ma pages +or their programs, calle! ?i+o.@+o pages are !isplaye! with a rea!er program ame!, appropriately eo"gh, info.
+o pages are hyperlinkedm"ch like we% pages. Aere is a sample
ile: coreutils Lode: ls invocation> Lext: dir invocation>37: 'irector1 listing
#(including directoriesP< 7tions and file arguments can be intermixed
arbitraril1> as usual b1default JlsK lists the contents of directories> not recursivel1> andomitting files 9ith names beginning 9ith J b1 default JlsK lists just the filename< Af no non-o7tionargument is s7ecified> JlsK o7erates on the current director1> actingas if it had been invoked 9ith a single argument of J the out7ut is sorted al7habeticall1> according to the---Anfo: Ncoreutils
8/13/2019 The Linux Command Line-09.12
69/521
8/13/2019 The Linux Command Line-09.12
70/521
K Workig With Comma!s
Creating :our ?4n Commands +ith alias
&ow +or o"r #ery +irst e3periece with programmigL We will create a comma! o+ o"row "sig the alias comma!. "t %e+ore we start, we ee! to re#eal a small
comma! lie trick. t
8/13/2019 The Linux Command Line-09.12
71/521
Creatig 5o"r 7w Comma!s With alias
&otice the str"ct"re o+ this comma!
alias name$%string%
$+ter the comma! ?alias@ we gi#e alias a ame +ollowe! imme!iately o whitespaceallowe!F %y a e="als sig, +ollowe! imme!iately %y a ="ote! strig cotaiig themeaig to %e assige! to the ame. $+ter we !e+ie o"r alias, it ca %e "se! aywherethe shell wo"l! e3pect a comma!. *et
8/13/2019 The Linux Command Line-09.12
72/521
K Workig With Comma!s
+ig"re o"t what they all !o
[me@linuxbox ~]$ aliasalias l
8/13/2019 The Linux Command Line-09.12
73/521
/ :e!irectio
/edirection
this lesso we are goig to "leash what may %e the coolest +eat"re o+ the comma!lie. t
8/13/2019 The Linux Command Line-09.12
74/521
/ :e!irectio
7 re!irectio allows "s to chage where o"tp"t goes a! where ip"t comes +rom.&ormally, o"tp"t goes to the scree a! ip"t comes +rom the key%oar!, %"t with 7re!irectio, we ca chage that.
edirecting Standard ?utput
7 re!irectio allows "s to re!e+ie where sta!ar! o"tp"t goes. To re!irect sta!ar!o"tp"t to aother +ile %esi!es the scree, we "se the ?X@ re!irectio operator +ollowe! %ythe ame o+ the +ile. Why wo"l! we wat to !o this t
8/13/2019 The Linux Command Line-09.12
75/521
:e!irectig Sta!ar! 7"tp"t
how to re!irect sta!ar! error i 4"st a mi"te, %"t +irst, let
8/13/2019 The Linux Command Line-09.12
76/521
/ :e!irectio
sta!ar! error we m"st re+er to its file descriptor. $ program ca pro!"ce o"tp"t o ayo+ se#eral "m%ere! +ile streams. While we ha#e re+erre! to the +irst three o+ these +ilestreams as sta!ar! ip"t, o"tp"t a! error, the shell re+ereces them iterally as +ile
!escriptors Dero, oe a! two, respecti#ely. The shell pro#i!es a otatio +or re!irectig+iles "sig the +ile !escriptor "m%er. Sice sta!ar! error is the same as +ile !escriptor"m%er two, we ca re!irect sta!ar! error with this otatio
[me@linuxbox ~]$ ls -l /bin/usr 2& ls-error.txt
The +ile !escriptor ?2@ is place! imme!iately %e+ore the re!irectio operator to per+ormthe re!irectio o+ sta!ar! error to the +ile ls-error
8/13/2019 The Linux Command Line-09.12
77/521
:e!irectig Sta!ar! Error
com%ie! re!irectio
[me@linuxbox ~]$ ls -l /bin/usr '& ls-output.txt
this e3ample, we "se the sigle otatio YD to re!irect %oth sta!ar! o"tp"t a!
sta!ar! error to the +ile ls-out7ut
8/13/2019 The Linux Command Line-09.12
78/521
8/13/2019 The Linux Command Line-09.12
79/521
:e!irectig Sta!ar! p"t
[me@linuxbox ~]$ cat+he ,uick brown fox jumped over the la! dog.The Buick bro9n fox jum7ed over the la1 dogoth@ +ormat. Thisway, the !irectory ames will sort i chroological or!er. We co"l! type o"t a completelist o+ !irectories, %"t that
8/13/2019 The Linux Command Line-09.12
92/521
8 Seeig The Worl! $s The Shell Sees t
[me@linuxbox ~]$ printenv 0 less
5o" may ha#e otice! that with other types o+ e3pasio, i+ yo" mistype a patter, thee3pasio will ot take place a! the echocomma! will simply !isplay the mistype!
patter. With parameter e3pasio, i+ yo" misspell the ame o+ a #aria%le, the e3pasiowill still take place, %"t will res"lt i a empty strig
[me@linuxbox ~]$ echo 3FEGH
[me@linuxbox ~]$
Command SubstitutionComma! s"%stit"tio allows "s to "se the o"tp"t o+ a comma! as a e3pasio
[me@linuxbox ~]$ echo 34ls6'eskto7 'ocuments ls-out7ut
8/13/2019 The Linux Command Line-09.12
93/521
E3pasio
6usr6bin6funi7: &= !-bit =*H executable> Antel /(/.>version # N*U*;P> d1namicall1 linked Nuses shared libsP> forL36=inux !
8/13/2019 The Linux Command Line-09.12
94/521
8 Seeig The Worl! $s The Shell Sees t
treate! as or!iary characters. The e3ceptios are ?[@, ?\@ %ackslashF, a! ?]@ %ack-="oteF. This meas that wor!-splittig, pathame e3pasio, til!e e3pasio, a! %racee3pasio are s"ppresse!, %"t parameter e3pasio, arithmetic e3pasio, a! comma!
s"%stit"tio are still carrie! o"t. )sig !o"%le ="otes, we ca cope with +ileamescotaiig em%e!!e! spaces. Say we were the "+ort"ate #ictim o+ a +ile calle!t9o 9ords
8/13/2019 The Linux Command Line-09.12
95/521
"otig
y !e+a"lt, wor!-splittig looks +or the presece o+ spaces, ta%s, a! ewlies lie+ee!charactersF a! treats them as delimiters %etwee wor!s. This meas that "="ote!spaces, ta%s, a! ewlies are ot cosi!ere! to %e part o+ the te3t. They oly ser#e as
separators. Sice they separate the wor!s ito !i++eret arg"mets, o"r e3amplecomma! lie cotais a comma! +ollowe! %y +o"r !istict arg"mets. + we a!!!o"%le ="otes
[me@linuxbox ~]$ echo Jthis is a testJthis is a test
wor!-splittig is s"ppresse! a! the em%e!!e! spaces are ot treate! as !elimiters, ratherthey %ecome part o+ the arg"met. 7ce the !o"%le ="otes are a!!e!, o"r comma! liecotais a comma! +ollowe! %y a sigle arg"met.
The +act that ewlies are cosi!ere! !elimiters %y the wor!-splittig mechaism ca"sesa iterestig, al%eit s"%tle, e++ect o comma! s"%stit"tio. Cosi!er the +ollowig
[me@linuxbox ~]$ echo 34cal6ebruar1 !((/ *u +o Tu ,e Th r *a # ! % " . ) / 0 #( ## #! # #%#" #. #) #/ #0 !( !# !! ! !% !" !. !) !/ !0[me@linuxbox ~]$ echo J34cal6J ebruar1 !((/*u +o Tu ,e Th r *a # ! % " . ) / 0
#( ## #! # #% #" #.#) #/ #0 !( !# !! !!% !" !. !) !/ !0
the +irst istace, the "="ote! comma! s"%stit"tio res"lte! i a comma! liecotaiig thirty-eight arg"mets. the seco!, a comma! lie with oe arg"met thaticl"!es the em%e!!e! spaces a! ewlies.
Singe Duotes
+ we ee! to s"ppress alle3pasios, we "se single
8/13/2019 The Linux Command Line-09.12
96/521
8 Seeig The Worl! $s The Shell Sees t
[me@linuxbox ~]$ echo Jtext ~/*.txt 9a;b> 34echo foo6 34425266 3EFGHJtext ~6Eb\ foo % me
[me@linuxbox ~]$ echo %text ~/*.txt 9a;b> 34echo foo6 34425266 3EFGH%text ~6Eb\ $Necho fooP $NN!8!PP $3*&R
$s we ca see, with each s"ccee!ig le#el o+ ="otig, more a! more o+ the e3pasiosare s"ppresse!.
6s/a5ing Chara/ters
Sometimes we oly wat to ="ote a sigle character. To !o this, we ca prece!e acharacter with a %ackslash, which i this cote3t is calle! the escape character. 7+tethis is !oe isi!e !o"%le ="otes to selecti#ely pre#et a e3pasio
[me@linuxbox ~]$ echo J+he balance for user 3EFGH is K3".JThe balance for user me is: $"
8/13/2019 The Linux Command Line-09.12
97/521
"otig
The ta%le a%o#e lists some o+ the commo %ackslash escape se="eces. The i!ea%ehi! this represetatio "sig the %ackslash origiate! i the C programmig
lag"age a! has %ee a!opte! %y may others, icl"!ig the shell.
$!!ig the ?-e@ optio to echowill ea%le iterpretatio o+ escape se="eces.
5o" may also place them isi!e $K K. Aere, "sig the slee7 comma!, a
simple program that 4"st waits +or the speci+ie! "m%er o+ seco!s a! the e3its,we ca create a primiti#e co"t!ow timer
sleep 1# echo -e J+ime%s upKaJ
We co"l! also !o this
sleep 1# echo J+ime%s upJ 3%Ka%
Summing -p
$s we mo#e +orwar! with "sig the shell, we will +i! that e3pasios a! ="otig will%e "se! with icreasig +re="ecy, so it makes sese to get a goo! "!ersta!ig o+ theway they works. +act, it co"l! %e arg"e! that they are the most importat s"%4ects tolear a%o"t the shell. Witho"t a proper "!ersta!ig o+ e3pasio, the shell will always%e a so"rce o+ mystery a! co+"sio, a! m"ch o+ it potetial power waste!.
;urther eading
The bashma page has ma4or sectios o %oth e3pasio a! ="otig whichco#er these topics i a more +ormal maer.
The 8ash 9eference !anualalso cotais chapters o e3pasio a! ="otighttpwww.g".orgso+tware%ashma"al%ashre+.html
8(
$scape SeBuence >eaning
a ell ?$lert@ - ca"ses the comp"ter to %eepF
b ackspace
n &ewlie. 7 )i3-like systems, thispro!"ces a lie+ee!.
r Carriage ret"r
t Ta%
http://www.gnu.org/software/bash/manual/bashref.htmlhttp://www.gnu.org/software/bash/manual/bashref.html8/13/2019 The Linux Command Line-09.12
98/521
9 $!#ace! Mey%oar! Tricks
)dvanced e$board Tric,s
o+te ki!!igly !escri%e )i3 as ?the operatig system +or people who like to type.@ 7+co"rse, the +act that it e#e has a comma! lie is a testamet to that. "t comma! lie"sers !o
8/13/2019 The Linux Command Line-09.12
99/521
Comma! *ie E!itig
Table 5-1: %ursor !ovement %ommands
=e, #ction
Ctrl-a >o#e c"rsor to the %egiig o+ the lie.Ctrl-e >o#e c"rsor to the e! o+ the lie.
Ctrl-f >o#e c"rsor +orwar! oe characterH same as the right arrow key.
Ctrl-b >o#e c"rsor %ackwar! oe characterH same as the le+t arrow key.
4lt-f >o#e c"rsor +orwar! oe wor!.
4lt-b >o#e c"rsor %ackwar! oe wor!.
Ctrl-l Clear the scree a! mo#e the c"rsor to the top le+t corer. Theclearcomma! !oes the same thig.
:odi=ying Text
Ta%le 9-2 lists key%oar! comma!s that are "se! to e!it characters o the comma! lie.
Table 5-': Te+t diting %ommands
=e, #ction
Ctrl-d 'elete the character at the c"rsor locatio
Ctrl-t Traspose e3chageF the character at the c"rsor locatio with the
oe prece!ig it.
4lt-t Traspose the wor! at the c"rsor locatio with the oe prece!ig it.
4lt-l Co#ert the characters +rom the c"rsor locatio to the e! o+ thewor! to lowercase.
4lt-u Co#ert the characters +rom the c"rsor locatio to the e! o+ thewor! to "ppercase.
Cutting And 'asting E9iing And 1ankingF Text
The :ea!lie !oc"metatio "ses the terms killinga!yankingto re+er to what we wo"l!commoly call c"ttig a! pastig. tems that are c"t are store! i a %"++er calle! thekill-ring.
8
8/13/2019 The Linux Command Line-09.12
100/521
9 $!#ace! Mey%oar! Tricks
Table 5-3: %ut )nd 6aste %ommands
=e, #ction
Ctrl-k Mill te3t +rom the c"rsor locatio to the e! o+ lie.Ctrl-u Mill te3t +rom the c"rsor locatio to the %egiig o+ the lie.
4lt-d Mill te3t +rom the c"rsor locatio to the e! o+ the c"rret wor!.
4lt-Hacks7ace
Mill te3t +rom the c"rsor locatio to the %egiig o+ the c"rretwor!. + the c"rsor is at the %egiig o+ a wor!, kill the pre#io"swor!.
Ctrl-1 5ak te3t +rom the kill-rig a! isert it at the c"rsor locatio.
The >eta =e,
+ yo" #et"re ito the :ea!lie !oc"metatio, which ca %e +o"! i the:E$'*&E sectio o+ the bashma page, yo" will eco"ter the term ?meta
key.@ 7 mo!er key%oar!s this maps to the 4ltkey %"t it was
8/13/2019 The Linux Command Line-09.12
101/521
Completio
this works. ;i#e a home !irectory that looks like this
[me@linuxbox ~]$ ls'eskto7 ls-out7ut
8/13/2019 The Linux Command Line-09.12
102/521
9 $!#ace! Mey%oar! Tricks
The completio is s"ccess+"l.
While this e3ample shows completio o+ pathames, which is its most commo "se,completio will also work o #aria%les i+ the %egiig o+ the wor! is a ?[@F, "ser ames
i+ the wor! %egis with ?@F, comma!s i+ the wor! is the +irst wor! o the lie.F a!host ames i+ the %egiig o+ the wor! is ?N@F. Aost ame completio oly works +orhost ames liste! i 6etc6hosts.
There are a "m%er o+ cotrol a! meta key se="eces that are associate! withcompletio
Table 5-$: %ompletion %ommands
=e, #ction
4lt-F 'isplay list o+ possi%le completios. &n most systems you can also
do this by pressing the tab key a second time" which is much easier#4lt-E sert all possi%le completios. This is "se+"l whe yo" wat to "se
more tha oe possi%le match.
There ="ite a +ew more that +i! rather o%sc"re. 5o" ca see a list i the bashma
page "!er ?:E$'*&E@.
Programma0le Completion
:ecet #ersios o+ bash ha#e a +acility calle! programmable completion.6rogramma%le completio allows yo" or more likely, yo"r !istri%"tio pro#i!erFto a!! a!!itioal completio r"les. )s"ally this is !oe to a!! s"pport +orspeci+ic applicatios. or e3ample it is possi%le to a!! completios +or the optiolist o+ a comma! or match partic"lar +ile types that a applicatio s"pports.)%"t" has a +airly large set !e+ie! %y !e+a"lt. 6rogramma%le completio isimplemete! %y shell +"ctios, a ki! o+ mii shell script that we will co#er ilater chapters. + yo" are c"rio"s, try
set 0 less
a! see i+ yo" ca +i! them. &ot all !istri%"tios icl"!e them %y !e+a"lt.
-sing 3istor,
$s we !isco#ere! i Chapter 2, bashmaitais a history o+ comma!s that ha#e %ee
etere!. This list o+ comma!s is kept i yo"r home !irectory i a +ile calle!
88
8/13/2019 The Linux Command Line-09.12
103/521
8/13/2019 The Linux Command Line-09.12
104/521
9 $!#ace! Mey%oar! Tricks
[me@linuxbox ~]$
irst type Ctrl-r
Nreverse-i-searchPJK:
The prompt chages to i!icate that we are per+ormig a re#erse icremetal search. t is?re#erse@ %eca"se we are searchig +rom ?ow@ to some time i the past. &e3t, we starttypig o"r search te3t. this e3ample ?"sr%i@
Nreverse-i-searchPJ/usr/binK: ls -l 6usr6bin D ls-out7uto#e to the pre#io"s history etry. Same actio as the "p arrow.
Ctrl-n >o#e to the e3t history etry. Same actio as the !ow arrow.
4lt-X >o#e to the %egiig topF o+ the history list.
4lt-D >o#e to the e! %ottomF o+ the history list, i.e., the c"rretcomma! lie.
Ctrl-r :e#erse icremetal search. Searches icremetally +rom thec"rret comma! lie "p the history list.
4lt-7 :e#erse search, o-icremetal. With this key, type i the searchstrig a! press eter %e+ore the search is per+orme!.
90
8/13/2019 The Linux Command Line-09.12
105/521
)sig Aistory
4lt-n orwar! search, o-icremetal.
Ctrl-o E3ec"te the c"rret item i the history list a! a!#ace to the e3t
oe. This is ha!y i+ yo" are tryig to re-e3ec"te a se="ece o+comma!s i the history list.
$istory 6x5ansion
The shell o++ers a specialiDe! type o+ e3pasio +or items i the history list %y "sig the?L@ character. We ha#e alrea!y see how the e3clamatio poit ca %e +ollowe! %y a"m%er to isert a etry +rom the history list. There are a "m%er o+ other e3pasio+eat"res
Table 5-2: ,istory +pansion %ommandsSeBuence #ction
GG :epeat the last comma!. t is pro%a%ly easier to press "p arrowa! eter.
Gnumber :epeat history list item number.
Gstring :epeat last history list item startig with strig.
GFstring :epeat last history list item cotaiig strig.
wo"l! ca"tio agaist "sig the ?Lstrig@ a! ?Lstrig@ +orms "less yo" are a%sol"telys"re o+ the cotets o+ the history list items.
There are may more elemets a#aila%le i the history e3pasio mechaism, %"t thiss"%4ect is alrea!y too arcae a! o"r hea!s may e3plo!e i+ we coti"e. The AST7:5EG6$&S7& sectio o+ the bashma page goes ito all the gory !etails. eel +ree to
e3ploreL
script
a!!itio to the comma! history +eat"re i bash, most *i"3 !istri%"tios
icl"!e a program calle! scri7t that ca %e "se! to recor! a etire shell
sessio a! store it i a +ile. The %asic syta3 o+ the comma! is
scri7t [file]
9
8/13/2019 The Linux Command Line-09.12
106/521
8/13/2019 The Linux Command Line-09.12
107/521
0 6ermissios
10 er%issions
7peratig systems i the )i3 tra!itio !i++er +rom those i the >S-'7S tra!itio ithat they are ot oly multitaskingsystems, %"t also multi-usersystems, as well.
What e3actly !oes this mea t meas that more tha oe perso ca %e "sig thecomp"ter at the same time. While a typical comp"ter will likely ha#e oly oe key%oar!
a! moitor, it ca still %e "se! %y more tha oe "ser. or e3ample, i+ a comp"ter isattache! to a etwork or the teret, remote "sers ca log i #ia sshsec"re shellF a!
operate the comp"ter. +act, remote "sers ca e3ec"te graphical applicatios a! ha#ethe graphical o"tp"t appear o a remote !isplay. The G Wi!ow System s"pports this aspart o+ its %asic !esig.
The m"lti-"ser capa%ility o+ *i"3 is ot a recet Qio#atio,Q %"t rather a +eat"re that is!eeply em%e!!e! ito the !esig o+ the operatig system. Cosi!erig the e#irometi which )i3 was create!, this makes per+ect sese. 5ears ago, %e+ore comp"ters wereQpersoal,Q they were large, e3pesi#e, a! cetraliDe!. $ typical "i#ersity comp"tersystem, +or e3ample, cosiste! o+ a large cetral comp"ter locate! i oe %"il!ig a!termials which were locate! thro"gho"t the camp"s, each coecte! to the large cetral
comp"ter. The comp"ter wo"l! s"pport may "sers at the same time.
or!er to make this practical, a metho! ha! to %e !e#ise! to protect the "sers +rom eachother. $+ter all, the actios o+ oe "ser co"l! ot %e allowe! to crash the comp"ter, orco"l! oe "ser iter+ere with the +iles %elogig to aother "ser.
this chapter we are goig to look at this essetial part o+ system sec"rity a! itro!"cethe +ollowig comma!s
id 'isplay "ser i!etity
chmod Chage a +ile
8/13/2019 The Linux Command Line-09.12
108/521
0 6ermissios
chgr7 Chage a +ile no read 7ermission[me@linuxbox ~]$ less /etc/shadow6etc6shado9: ermission denied
The reaso +or this error message is that, as reg"lar "sers, we !o ot ha#e permissio torea! this +ile.
the )i3 sec"rity mo!el, a "ser may own+iles a! !irectories. Whe a "ser ows a +ileor !irectory, the "ser has cotrol o#er its access. )sers ca, i t"r, %elog to a groupcosistig o+ oe or more "sers who are gi#e access to +iles a! !irectories %y theirowers. a!!itio to gratig access to a gro"p, a ower may also grat some set o+access rights to e#ery%o!y, which i )i3 terms is re+erre! to as the world. To +i! o"ti+ormatio a%o"t yo"r i!etity, "se the idcomma!
[me@linuxbox ~]$ id
uidM"((NmeP gidM"((NmeP grou7sM"((NmeP
*et!0NaudioP>(Ndi7P>%%NvideoP>%.N7lugdevP>#(/Nl7adminP>##%NadminP>#(((NmeP
$s we ca see, the "i! a! gi! "m%ers are !i++eret. This is simply %eca"se e!orastarts its "m%erig o+ reg"lar "ser acco"ts at 00, while )%"t" starts at 000. We ca
91
8/13/2019 The Linux Command Line-09.12
109/521
7wers, ;ro"p >em%ers, $! E#ery%o!y Else
also see that the )%"t" "ser %elogs to a lot more gro"ps. This has to !o with the way)%"t" maages pri#ileges +or system !e#ices a! ser#ices.
So where !oes this i+ormatio come +rom *ike so may thigs i *i"3, +rom a co"ple
o+ te3t +iles. )ser acco"ts are !e+ie! i the 6etc67ass9d+ile a! gro"ps are !e+ie!i the 6etc6grou7 +ile. Whe "ser acco"ts a! gro"ps are create!, these +iles are
mo!i+ie! alog with 6etc6shado9which hol!s i+ormatio a%o"t the "ser
8/13/2019 The Linux Command Line-09.12
110/521
0 6ermissios
c $ character special file. This +ile type re+ers to a !e#ice thatha!les !ata as a stream o+ %ytes, s"ch as a termial or mo!em.
b$ block special file. This +ile type re+ers to a !e#ice that ha!les!ata i %locks, s"ch as a har! !ri#e or C'-:7> !ri#e.
The remaiig ie characters o+ the +ile attri%"tes, calle! the file mode"represet therea!, write, a! e3ec"te permissios +or the +ile
8/13/2019 The Linux Command Line-09.12
111/521
:ea!ig, Writig, $! E3ec"tig
Table 1=-3: 6ermission )ttribute +amples
;ile #ttri0utes >eaning
-r9x------ $ reg"lar +ile that is rea!a%le, writa%le, a! e3ec"ta%le %y the+ile
8/13/2019 The Linux Command Line-09.12
112/521
0 6ermissios
+hat The 3ec( Is ?ctal
&ctal%ase 8F, a! its co"si, he+adecimal%ase KF are "m%er systems o+te"se! to e3press "m%ers o comp"ters. We h"mas, owig to the +act that we orat least most o+ "sF were %or with te +igers, co"t "sig a %ase 0 "m%ersystem. Comp"ters, o the other the other ha!, were %or with oly oe +igera! th"s !o all all their co"tig i binary %ase 2F. Their "m%er system olyhas two "merals, Dero a! oe. So i %iary, co"tig looks like this
0, , 0, , 00, 0, 0, , 000, 00, 00, 0...
octal, co"tig is !oe with the "merals Dero thro"gh se#e, like so
0, , 2, (, 1, , K, /, 0, , 2, (, 1, , K, /, 20, 2...
Ae3a!ecimal co"tig "ses the "merals Dero thro"gh ie pl"s the letters ?$@thro"gh ?@
0, , 2, (, 1, , K, /, 8, 9, $, , C, ', E, , 0, , 2, (...
While we ca see the sese i %iary sice comp"ters oly ha#e oe +igerF,what are octal a! he3a!ecimal goo! +or The aswer has to !o with h"maco#eiece. >ay times, small portios o+ !ata are represete! o comp"ters asbit patterns. Take +or e3ample a :; color. 7 most comp"ter !isplays, eachpi3el is compose! o+ three color compoets eight %its o+ re!, eight %its o+ gree,a! eight %its o+ %l"e. $ lo#ely me!i"m %l"e wo"l! %e a twety-+o"r !igit"m%er
0000000000Aow wo"l! yo" like to rea! a! write those ki!s o+ "m%ers all !ay !i!
8/13/2019 The Linux Command Line-09.12
113/521
:ea!ig, Writig, $! E3ec"tig
each !igit i a octal "m%er represets three %iary !igits, this maps icely to thescheme "se! to store the +ile mo!e. This ta%le shows what we mea
?ctal /inar, ;ile >ode
( ((( ---
# ((# --x
! (#( -9-
(## -9x
% #(( r--
" #(# r-x
. ##( r9-
) ### r9xy "sig three octal !igits, we ca set the +ile mo!e +or the ower, gro"p ower, a!worl!
[me@linuxbox ~]$ & foo.txt[me@linuxbox ~]$ ls -l foo.txt-r9-r9-r-- # me me ( !((/-(-(. #%:"! foo
8/13/2019 The Linux Command Line-09.12
114/521
0 6ermissios
o Short +or ?others,@ %"t meas worl!.
a Short +or ?all.@ The com%iatio o+ ?"@, ?g@, a! ?o@.
+ o character is speci+ie!, ?all@ will %e ass"me!. The operatio may %e a ?V@ i!icatigthat a permissio is to %e a!!e!, a ?-@ i!icatig that a permissio is to %e take away, ora ?@ i!icatig that oly the speci+ie! permissios are to %e applie! a! that all othersare to %e remo#e!.
6ermissios are speci+ie! with the ?r@, ?w@, a! ?3@ characters. Aere are some e3ampleso+ sym%olic otatio
Table 1=-.: chmod Symbolic >otation +amples
7otation >eaning
u8x $!! e3ec"te permissio +or the ower.
u-x :emo#e e3ec"te permissio +rom the ower.
8x $!! e3ec"te permissio +or the ower, gro"p, a! worl!.E="i#alet to a8x.
o-r9 :emo#e the rea! a! write permissio +rom ayoe %esi!es theower a! gro"p ower.
goMr9 Set the gro"p ower a! ayoe %esi!es the ower to ha#e rea! a!write permissio. + either the gro"p ower or worl! pre#io"sly ha!e3ec"te permissios, they are remo#e!.
u8x>goMrx $!! e3ec"te permissio +or the ower a! set the permissios +orthe gro"p a! others to rea! a! e3ec"te. >"ltiple speci+icatiosmay %e separate! %y commas.
Some people pre+er to "se octal otatio, some +olks really like the sym%olic. Sym%olicotatio !oes o++er the a!#atage o+ allowig yo" to set a sigle attri%"te witho"t!ist"r%ig ay o+ the others.
Take a look at the chmodma page +or more !etails a! a list o+ optios. $ wor! o+
ca"tio regar!ig the ?--rec"rsi#e@ optio it acts o %oth +iles a! !irectories, so it
8/13/2019 The Linux Command Line-09.12
115/521
:ea!ig, Writig, $! E3ec"tig
"!ersta! the permissio !ialogs i the ;). %oth &a"til"s ;&7>EF a!Mo="eror M'EF, right-clickig a +ile or !irectory ico will e3pose a properties !ialog.Aere is a e3ample +rom M'E (.
Aere we ca see the settigs +or the ower, gro"p, a! worl!. M'E, clickig o the?$!#ace! 6ermissios@ %"tto %rigs "p aother !ialog that allows yo" to set each o+the mo!e attri%"tes i!i#i!"ally. $other #ictory +or "!ersta!ig %ro"ght to "s %y thecomma! lieL
umask Set )e=aut 'ermissions
The umaskcomma! cotrols the !e+a"lt permissios gi#e to a +ile whe it is create!.
t "ses octal otatio to e3press a mask o+ %its to %e remo#e! +rom a +ile
8/13/2019 The Linux Command Line-09.12
116/521
0 6ermissios
[me@linuxbox ~]$ ls -l foo.txt-r9-r9-r-- # me me ( !((/-(-(. #%:" foo
8/13/2019 The Linux Command Line-09.12
117/521
:ea!ig, Writig, $! E3ec"tig
$gai, where a appears i the %iary #al"e, the correspo!ig attri%"te is "set. 6laywith some #al"es try some se#esF to get "se! to how this works. Whe yo"
8/13/2019 The Linux Command Line-09.12
118/521
8/13/2019 The Linux Command Line-09.12
119/521
8/13/2019 The Linux Command Line-09.12
120/521
0 6ermissios
)sig this +orm, a sigle comma! lie is passe! to the ew shell +or e3ec"tio. t isimportat to eclose the comma! i ="otes, as we !o ot wat e3pasio to occ"r i o"rshell, %"t rather i the ew shell
[me@linuxbox ~]$ su -c %ls -l /root/*%ass9ord:-r9------- # root root )"% !(()-(/-## (:#0 6root6anaconda-ks
8/13/2019 The Linux Command Line-09.12
121/521
Chagig !etities
N4==P 4==
-0untu #nd sudo
7e o+ the rec"rret pro%lems +or reg"lar "sers is how to per+orm certai tasksthat re="ire s"per"ser pri#ileges. These tasks icl"!e istallig a! "p!atigso+tware, e!itig system co+ig"ratio +iles, a! accessig !e#ices. theWi!ows worl!, this is o+te !oe %y gi#ig "sers a!miistrati#e pri#ileges. Thisallows "sers to per+orm these tasks. Aowe#er, it also ea%les programs e3ec"te!%y the "ser to ha#e the same a%ilities. This is !esira%le i most cases, %"t it alsopermits malware malicio"s so+twareF s"ch as #ir"ses to ha#e +ree reig o+ thecomp"ter.
the )i3 worl!, there has always %ee a larger !i#isio %etwee reg"lar "sersa! a!miistrators, owig to the m"lti-"ser heritage o+ )i3. The approach takei )i3 is to grat s"per"ser pri#ileges oly whe ee!e!. To !o this, the sua!
sudocomma!s are commoly "se!.
)p "til a co"ple o+ years ago, most *i"3 !istri%"tios relie! o su +or this
p"rpose. su!i!
8/13/2019 The Linux Command Line-09.12
122/521
0 6ermissios
chown (owner)((group)) file...
cho9nca chage the +ile ower a!or the +ile gro"p ower !epe!ig o the +irstarg"met o+ the comma!. Aere are some e3amples
Table 1=-2: chown )rgument +amples
#rgument esults
bob Chages the owership o+ the +ile +rom its c"rret ower to "serbob.
bob:users Chages the owership o+ the +ile +rom its c"rret ower to "serboba! chages the +ile gro"p ower to gro"p "sers.
:admins Chages the gro"p ower to the gro"p admins. The +ile ower is"chage!.
bob: Chage the +ile ower +rom the c"rret ower to "ser boba!
chages the gro"p ower to the logi gro"p o+ "ser bob.
*et
8/13/2019 The Linux Command Line-09.12
123/521
Chagig !etities
r"s o"t.
/hgr5 Change +rou5
8/13/2019 The Linux Command Line-09.12
124/521
0 6ermissios
[bill@linuxbox ~]$ sudo mkdir /usr/local/share/Ousicass9ord:
Sice bill is maip"latig +iles o"tsi!e his home !irectory, s"per"ser pri#ileges are
re="ire!. $+ter the !irectory is create!, it has the +ollowig owerships a! permissios
[bill@linuxbox ~]$ ls -ld /usr/local/share/Ousicdr9xr-xr-x ! root root %(0. !((/-(-!# #/:(" 6usr6local6share6+usic
$s we ca see, the !irectory is owe! %y roota! has / permissios. To make this
!irectory shara%le, billee!s to chage the gro"p owership a! the gro"p permissios
to allow writig
[bill@linuxbox ~]$ sudo chown music /usr/local/share/Ousic[bill@linuxbox ~]$ sudo chmod CC" /usr/local/share/Ousic[bill@linuxbox ~]$ ls -ld /usr/local/share/Ousicdr9xr9xr-x ! root music %(0. !((/-(-!# #/:(" 6usr6local6share6+usic
So what !oes this all mea t meas that we ow ha#e a !irectory,6usr6local6share6+usic that is owe! %y root a! allows rea! a! write
access to gro"p music. ;ro"p musichas mem%ers billa! karen, th"s billa!
karenca create +iles i !irectory 6usr6local6share6+usic. 7ther "sers ca
list the cotets o+ the !irectory %"t caot create +iles there."t we still ha#e a pro%lem. With the c"rret permissios, +iles a! !irectories create!withi the >"sic !irectory will ha#e the ormal permissios o+ the "sers bill a!
karen
[bill@linuxbox ~]$ & /usr/local/share/Ousic/testfile[bill@linuxbox ~]$ ls -l /usr/local/share/Ousic-r9-r--r-- # bill bill ( !((/-(-!% !(:( testOfile
$ct"ally there are two pro%lems. irst, the !e+a"lt umasko this system is 0022 which
pre#ets gro"p mem%ers +rom writig +iles %elogig to other mem%ers o+ the gro"p.This wo"l! ot %e a pro%lem i+ the share! !irectory oly cotaie! +iles, %"t sice this!irectory will store m"sic, a! m"sic is "s"ally orgaiDe! i a hierarchy o+ artists a!al%"ms, mem%ers o+ the gro"p will ee! the a%ility to create +iles a! !irectories isi!e!irectories create! %y other mem%ers. We ee! to chage the umask"se! %y billa!
0
8/13/2019 The Linux Command Line-09.12
125/521
E3ercisig 7"r 6ri#ileges
karento 0002 istea!.
Seco!, each +ile a! !irectory create! %y oe mem%er will %e set to the primary gro"p o+the "ser rather tha the gro"p music. This ca %e +i3e! %y settig the setgi! %it o the
!irectory
[bill@linuxbox ~]$ sudo chmod g5s /usr/local/share/Ousic[bill@linuxbox ~]$ ls -ld /usr/local/share/Ousicdr9xr9sr-x ! root music %(0. !((/-(-!% !(:( 6usr6local6share6+usic
&ow we test to see i+ the ew permissios +i3 the pro%lem. bill sets his umask to
0002, remo#es the pre#io"s test +ile, creates a ew test +ile a! !irectory
[bill@linuxbox ~]$ umask 2[bill@linuxbox ~]$ rm /usr/local/share/Ousic/testfile[bill@linuxbox ~]$ & /usr/local/share/Ousic/testfile[bill@linuxbox ~]$ mkdir /usr/local/share/Ousic/testdir[bill@linuxbox ~]$ ls -l /usr/local/share/Ousicdr9xr9sr-x ! bill music %(0. !((/-(-!% !(:!% testOdir-r9-r9-r-- # bill music ( !((/-(-!% !(:!! testOfile[bill@linuxbox ~]$
oth +iles a! !irectories are ow create! with the correct permissios to allow allmem%ers o+ the gro"p musicto create +iles a! !irectories isi!e the +usic!irectory.
The oe remaiig iss"e is umask. The ecessary settig oly lasts "til the e! o+sessio a! m"st %e reset. the e3t part o+ the %ook, we
8/13/2019 The Linux Command Line-09.12
126/521
0 6ermissios
[me@linuxbox ~]$ passwdNcurrentP 3LAV 7ass9ord:Le9 3LAV 7ass9ord:
The 7ass9dcomma! will try to e+orce "se o+ ?strog@ passwor!s. This meas the it
will re+"se to accept passwor!s that are too short, too similar to pre#io"s passwor!s, are!ictioary wor!s, or too easily g"esse!
[me@linuxbox ~]$ passwdNcurrentP 3LAV 7ass9ord:Le9 3LAV 7ass9ord:H4' 4**,R': is too similar to the old oneLe9 3LAV 7ass9ord:H4' 4**,R': it is ,4U too short
Le9 3LAV 7ass9ord:H4' 4**,R': it is based on a dictionar1 9ord
+ yo" ha#e s"per"ser pri#ileges, yo" ca speci+y a "ser ame as a arg"met to the7ass9d comma! to set the passwor! +or aother "ser. There are other optios
a#aila%le to the s"per"ser to allow acco"t lockig, passwor! e3piratio, etc. See the7ass9dma page +or !etails.
;urther eading
Wikipe!ia has a goo! article o malwarehttpe.wikipe!ia.orgwiki>alware
There are "m%er o+ comma! lie programs "se! to create a! maitai "sers a!gro"ps. or more i+ormatio, see the ma pages +or the +ollowig comma!s
adduser
useradd
grou7add
2
http://en.wikipedia.org/wiki/Malwarehttp://en.wikipedia.org/wiki/Malware8/13/2019 The Linux Command Line-09.12
127/521
6rocesses
11 rocesses
>o!er operatig systems are "s"ally multitasking, meaig that they create the ill"sioo+ !oig more tha oe thig at oce %y rapi!ly switchig +rom oe e3ec"tig program toaother. The *i"3 kerel maages this thro"gh the "se o+processes. 6rocesses are how*i"3 orgaiDes the !i++eret programs waitig +or their t"r at the C6).
Sometimes a comp"ter will %ecome sl"ggish or a applicatio will stop respo!ig. this chapter, we will look at some o+ the tools a#aila%le at the comma! lie that let "se3amie what programs are !oig, a! how to termiate processes that are mis%eha#ig.
This chapter will itro!"ce the +ollowig comma!s
7s :eport a sapshot o+ c"rret processes
to7 'isplay tasks
jobs *ist acti#e 4o%s
bg 6lace a 4o% i the %ackgro"!
fg 6lace a 4o% i the +oregro"!
kill Se! a sigal to a process
killall Mill processes %y ame
shutdo9n Sh"t!ow or re%oot the system
3o4 # Process +or(s
Whe a system starts "p, the kerel iitiates a +ew o+ its ow acti#ities as processes a!la"ches a program calle! init. init, i t"r, r"s a series o+ shell scripts locate! i
6etcF calle! init scripts, which start all the system ser#ices. >ay o+ these ser#ices are
implemete! as daemon programs, programs that 4"st sit i the %ackgro"! a! !o theirthig witho"t ha#ig ay "ser iter+ace. So e#e i+ we are ot logge! i, the system is atleast a little %"sy per+ormig ro"tie st"++.
The +act that a program ca la"ch other programs is e3presse! i the process scheme asaparent processpro!"cig a child process.
(
8/13/2019 The Linux Command Line-09.12
128/521
6rocesses
The kerel maitais i+ormatio a%o"t each process to help keep thigs orgaiDe!. ore3ample, each process is assige! a "m%er calle! a process * or 6*. 6's areassige! i asce!ig or!er, with init always gettig 6' . The kerel also keeps
track o+ the memory assige! to each process, as well as the processes< rea!iess tores"me e3ec"tio. *ike +iles, processes also ha#e owers a! "ser 's, e++ecti#e "ser's, etc.
Vie4ing Processes
The most commoly "se! comma! to #iew processes there are se#eralF is 7s. The 7s
program has a lot o+ optios, %"t i it simplest +orm it is "se! like this
[me@linuxbox ~]$ ps A' TTU TA+& C+'
"#0/ 7ts6# ((:((:(( bash#(#!0 7ts6# ((:((:(( 7s
The res"lt i this e3ample lists two processes, process 98 a! process 029, which arebasha! 7s respecti#ely. $s we ca see, %y !e+a"lt, 7s!oes
8/13/2019 The Linux Command Line-09.12
129/521
Riewig 6rocesses
$!!ig the ?3@ optio ote that there is o lea!ig !ashF tells 7sto show all o+ o"r
processes regar!less o+ what termial i+ ayF they are cotrolle! %y. The presece o+ a?@ i the TTUcol"m i!icates o cotrollig termial. )sig this optio, we see a list
o+ e#ery process that we ow.Sice the system is r"ig a lot o+ processes, 7spro!"ces a log list. t is o+te help+"l
to pipe the o"tp"t +rom 7sito less+or easier #iewig. Some optio com%iatios also
pro!"ce log lies o+ o"tp"t, so ma3imiDig the termial em"lator wi!ow may %e agoo! i!ea, too.
$ ew col"m title! *T4Thas %ee a!!e! to the o"tp"t. *T4Tis short +or ?state@ a!
re#eals the c"rret stat"s o+ the process
Table 11-1: 6rocess States
State >eaning
R :"ig. This meas that the process is r"ig or rea!y to r".
* Sleepig. $ process is ot r"igH rather, it is waitig +or a e#et,s"ch as a keystroke or etwork packet.
' )iterr"pti%le Sleep. 6rocess is waitig +or 7 s"ch as a !isk!ri#e.
T Stoppe!. 6rocess has %ee istr"cte! to stop. >ore o this later.
I $ !e+"ct or ?Dom%ie@ process. This is a chil! process that hastermiate!, %"t has ot %ee cleae! "p %y its paret.
X $ high priority process. t
8/13/2019 The Linux Command Line-09.12
130/521
6rocesses
[me@linuxbox ~]$ ps aux3*&R A' 5C3 5+&+ ;*I R** TTU *T4T *T4RT TA+& C++4L'root # (
8/13/2019 The Linux Command Line-09.12
131/521
8/13/2019 The Linux Command Line-09.12
132/521
6rocesses
that are waitig to r", that is, the "m%er o+processes that are i a r"a%le state a! aresharig the C6). Three #al"es are show,
each +or a !i++eret perio! o+ time. The +irst isthe a#erage +or the last K0 seco!s, the e3t thepre#io"s mi"tes, a! +ially the pre#io"s mi"tes. Ral"es "!er .0 i!icate that themachie is ot %"sy.
2 Tasks: This s"mmariDes the "m%er o+ processes a!their #ario"s process states.
( C7uNsP: This row !escri%es the character o+ theacti#ities that the C6) is per+ormig.
(aager i Wi!ows worksF, %"t +i!
that to7is %etter tha the graphical #ersios %eca"se it is +aster a! it cos"mes +ar
+ewer system reso"rces. $+ter all, o"r system moitor program sho"l!
8/13/2019 The Linux Command Line-09.12
133/521
Cotrollig 6rocesses
e3perimets, we
8/13/2019 The Linux Command Line-09.12
134/521
6rocesses
program. WeIll !o this %y placig the program i the background. Thik o+ the termialas ha#ig a foregroundwith st"++ #isi%le o the s"r+ace like the shell promptF a! a%ackgro"! with hi!!e st"++ %ehi! the s"r+ace.F To la"ch a program so that it is
imme!iately place! i the %ackgro"!, we +ollow the comma! with a- ?^@ character
[me@linuxbox ~]$ xlogo '[#] !/!.[me@linuxbox ~]$
$+ter eterig the comma!, the xlogowi!ow appeare! a! the shell prompt ret"re!,
%"t some +"y "m%ers were prite! too. This message is part o+ a shell +eat"re calle!Aob control. With this message, the shell is tellig "s that we ha#e starte! 4o% "m%er ?[#]@F a! that it has 6' 282(K. + we r" 7s, we ca see o"r process
[me@linuxbox ~]$ ps A' TTU TA+& C+'#(.( 7ts6# ((:((:(( bash!/!. 7ts6# ((:((:(( xlogo!/!0 7ts6# ((:((:(( 7s
The shell
8/13/2019 The Linux Command Line-09.12
135/521
Cotrollig 6rocesses
xlogo
The comma! fg+ollowe! %y a percet sig a! the 4o% "m%er calle! aAobspecF !oesthe trick. + we oly ha#e oe %ackgro"! 4o%, the 4o%spec is optioal. To termiatexlogo, type Ctrl-c.
Sto55ing E'ausingF A 'ro/ess
Sometimes weo#ig a process +rom the +oregro"! to the %ackgro"! is ha!y i+ we la"ch agraphical program +rom the comma!, %"t +orget to place it i the %ackgro"! %yappe!ig the trailig ?^@.
Why wo"l! yo" wat to la"ch a graphical program +rom the comma! lie There aretwo reasos. irst, the program yo" wish to r" might ot %e liste! o the wi!ow
maager
8/13/2019 The Linux Command Line-09.12
136/521
6rocesses
Signals
The killcomma! is "se! to ?kill@ programs. This allows "s to