Anda di halaman 1dari 13

Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1

Disclaimer: The orgna verson of hs arce was frs pubshed on IBM deveoperWorks, and s
propery of Wesech Informaon Servces. Ths documen s an updaed verson of he orgna arce,
and conans varous mprovemens made by he Genoo Lnux Documenaon eam.
Ths documen s no acvey mananed.
LPl cerificaion 101 (release 2) examprep, Par 1
Conen: 1. Before You Sar
1. Before You Sar
Abou his uorial
Wecomeo "Lnux fundamenas," he frs of four uoras desgnedo prepareyou for he Lnux Professona
Insue's 101 exam. In hs uora, we' nroduceyou o bash(he sandardLnux she), showyou howo ake fu
advanageof sandardLnux commandske s, cp, and mv, expannodesand hard and symbocnks, and much
more. By he end of hs uora, you' havea sodgroundngn Lnux fundamenasand w evenbe ready o begn
earnngsomebasc Lnux sysemadmnsraonasks. By he end of hs seres of uoras (egh n a), you' have
he knowedgeyou needo becomea Lnux SysemsAdmnsraorand w be ready o aan an LPIC Leve 1
cerfcaon from he Lnux ProfessonaInsuef you so choose.
Ths parcuar uora (Par 1) s dea for hosewhoare newo Lnux, or hosewhowan o revewor mproveher
undersandngof fundamenaLnux conceps ke copyngand movngfes, creang symbocand hard nks, and
usngLnux' sandardex-processngcommandsaongwh ppenesand redrecon. Aonghe way, we' share
peny of hns, ps, and rcks o keephe uora meay and pracca, evenfor hosewh a goodamoun of prevo
Lnux experence. For begnners, much of hs maera w be new, bu more experencedLnux users may fnd hs
uora o be a grea way of roundngou her fundamenaLnux sks.
For hosewhohaveakenhe reease1 versonof hs uora for reasonsoher han LPI exampreparaon, you
probabydon' needo ake hs one. However, f you do pan o ake he exams, you shoudsrongyconsder readn
hs revseduora.
Abou he auhor
Resdngn Abuquerque, NewMexco, Dane Robbnss he Chef Archec of GenooLnux an advancedpors-based
Lnux mea dsrbuon. He asowres arces, uoras, and ps for he IBM deveoperWorksLnux zoneand Ine
DeveoperServces and has asoservedas a conrbungauhor for severa books, ncudngSambaUneashedand
SuSELnux Uneashed. Dane enoysspendngme wh hs wfe, Mary, and hs daugher, Hadassah. You can conac
Dane a drobbns@genoo.org.
2. lnroducingbash
The shell
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
Are you running bash7
You can check o see f you're runnngbashby ypng:
CodeLising2.2: TheSHELLvariable
$ echo $ShELL
b1hbash
If he abovene gaveyou an error or ddn' respondsmaryo our exampe, hen you may be runnnga she oher
han bash. In ha case, mos of hs uora shouds appy, bu woudbe advanageousfor you o swcho bash
for he sakeof preparngfor he 101 exam.
Abou bash
Bash, an acronymfor "Bourne-aganshe," s he defau she on mos Lnux sysems. The she's ob s o obey your
commandsso ha you can nerac wh your Lnux sysem. Whenyou're fnshedenerngcommands, you may
nsruc he she o ex or ogou, a whch pon you' be reurnedo a ognpromp.
By he way, you can asoog ou by pressngconro-D a he bashpromp.
Using "cd"
As you've probabyfound, sarnga your bashpromp sn' he mos excnghng n he word. So, e's sar usng
basho navgaearoundour fe sysem. A he promp, ype he foowng(whou he $):
CodeLising2.3: Changingdirecories
$ cd
We've us od bashha you wan o work n /, asoknownas he roo drecory; a he drecores on he sysemform
a ree, and / s consderedhe op of hs ree, or he roo. cd ses he drecory whereyou are curreny workng, aso
knownas he "curren workngdrecory."
Pahs
To see bash's curren workngdrecory, you can ype:
CodeLising2.4: PresenWorkingDirecory
$ pwd

In he aboveexampe, he / argumeno cd s caed a path. I es cd wherewe wan o go. In parcuar, he /


argumens an absolutepah, meanngha specfesa ocaonreaveo he roo of he fe sysemree.
Absolue pahs
Here are someoher absouepahs:
CodeLising2.5: Exampleabsoluepahs
dev
usJ
usJb1h
usJocab1h
As you can see, he one hng ha a absouepahs haven commons ha hey begnwh /. Wh a pah of /usr/
oca/bn, we're engcd o ener he / drecory, hen he usr drecory under ha, and hen oca and bn. Absoue
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
Then, we can use a reavepah o changeo he /usr/oca/bn drecory:
CodeLising2.7: Changingdirecorieswihrelaivepahs
$ cd ocab1h
$ pwd
usJocab1h
Using ..
Reavepahs may asoconan one or more .. drecores. The .. drecory s a speca drecory ha pons o he
paren drecory. So, connungfrom he exampeabove:
CodeLising2.8: Usinghe'parendirecory' noaion
$ pwd
usJocab1h
$ cd ..
$ pwd
usJoca
As you can see, our curren drecory s now/usr/oca. We wereabe o go "backwards" one drecory, reaveo he
curren drecory ha we weren.
In addon, we can asoadd .. o an exsngreavepah, aowngus o go no a drecory ha's aongsdeone we
are aready n, for exampe:
CodeLising2.9: Usingheparendirecorynoaionin relaivepahs
$ pwd
usJoca
$ cd ..shaJe
$ pwd
usJshaJe
Relaivepah examples
Reavepahs can ge que compex. Here are a few exampes, a whou he resuanarge drecory dspayed.
Try o fgure ou whereyou' end up afer ypnghesecommands:
CodeLising2.10: Relaivepahexamples
$ cd b1h
$ cd ..usJshaJezohe1h1o
$ cd usJX11R6b1h
$ cd ..1bX11
$ cd usJb1h
$ cd ..b1h..b1h
Now, ry hem ou and see f you go hem rgh :)
Undersanding.
Before we fnshour coverageof cd, here are a few more hngs I needo menon. Frs, here s anoher speca
drecory caed ., whch means "he curren drecory". Whehs drecory sn' usedwh he cd command, 's ofe
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
CodeLising2.12: Go o heHOME direcory
$ cd
Wh no argumens, cd w changeo your homedrecory, whch s /roo for he superuserand ypcay /home/
usernamefor a reguar user. Bu wha f we wan o specfy a fe n our homedrecory? Maybe we wan o pass a f
argumeno he mypJogcommand. If he fe ves n our homedrecory, we can ype:
CodeLising2.13: Execuea programin hecurrendirecory
$ .mypJog homedJobb1hsmy11e.1x1
However, usngan absouepah ke ha sn' awaysconvenen. Thankfuy, we can use he -(de) characer o d
he samehng:
CodeLising2.14: Usinghe'homedirecory' noaion
$ .mypJog -my11e.1x1
Oher users' home direcories
Bashw expanda one -o pon o your homedrecory, bu you can asouse o pon o oher users' home
drecores. For exampe, f we wanedo refer o a fe caed fredsfe.x n Fred's homedrecory, we coud ype:
CodeLising2.15: Exampleuseof he'homedirecory' noaion
$ .mypJog -1Jed1Jeds11e.1x1
3. Using LinuxCommands
lnroducing ls
Now, we' ake a quck ook a he s command. Very key, you're aready famar wh s and knowha ypng by
sef w s he conens of he curren workngdrecory:
CodeLising3.1: Lisingfiles
$ cd usJ
$ s
X11R6 doc 1686-pc-1hux-ghu 1b mah sb1h ss
b1h geh1oo-x86 1hcude 1bexec poJ1age shaJe 1mp
d1s111es 1686-1hux 1h1o oca poJ1age.od sJc
By specfynghe -a opon, you can see a of he fes n a drecory, ncudnghddenfes: hoseha begnwh .. A
you can see n he foowngexampe, s -a reveashe . and .. speca drecory nks:
CodeLising3.2: Lisingfiles, includinghiddenones
$ s -a
. b1h geh1oo-x86 1hcude 1bexec poJ1age shaJe 1mp
.. d1s111es 1686-1hux 1h1o oca poJ1age.od sJc
X11R6 doc 1686-pc-1hux-ghu 1b mah sb1h ss
Long direcorylisings
You can asospecfy one or more fes or drecores on he s commandne. If you specfy a fe, s w showha
fe ony. If you specfy a drecory, s w showhe contents of he drecory. The - oponcomes n very handy
whenyou needo vewpermssons, ownershp, modfcaonme, and szenformaonn your drecory sng.
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
dJwxJ-xJ-x 4 Joo1 Joo1 96 hov 24 13.17 1686-pc-1hux-ghu
dJwxJ-xJ-x 54 Joo1 Joo1 5992 0ec 24 22.30 1hcude
JwxJwxJwx 1 Joo1 Joo1 10 0ec 22 20.57 1h1o -> shaJe1h1o
dJwxJ-xJ-x 28 Joo1 Joo1 13552 0ec 26 00.31 1b
dJwxJ-xJ-x 3 Joo1 Joo1 72 hov 25 00.34 1bexec
dJwxJ-xJ-x 8 Joo1 Joo1 240 0ec 22 20.57 oca
JwxJwxJwx 1 Joo1 Joo1 9 0ec 22 20.57 mah -> shaJemah
JwxJwxJwx 1 Joo1 Joo1 11 0ec 8 07.59 poJ1age -> geh1oo-x86
dJwxJ-xJ-x 60 Joo1 Joo1 1864 0ec 8 07.55 poJ1age.od
dJwxJ-xJ-x 3 Joo1 Joo1 3096 0ec 22 20.57 sb1h
dJwxJ-xJ-x 46 Joo1 Joo1 1144 0ec 24 15.32 shaJe
dJwxJ-xJ-x 8 Joo1 Joo1 328 0ec 26 00.07 sJc
dJwxJ-xJ-x 6 Joo1 Joo1 176 hov 24 14.25 ss
JwxJwxJwx 1 Joo1 Joo1 10 0ec 22 20.57 1mp -> ..vaJ1mp
The frs coumn dspayspermssonsnformaonfor each em n he sng. I' expanhowo nerpre hs
nformaonn a b. The nex coumn ss he number of nks o each fe sysemobec, whch we' goss over now
bu reurn o aer. The hrd and fourh coumns s he owner and group, respecvey. The ffh coumn ss he
obec sze. The sxhcoumn s he "as modfed" me or "mme" of he obec. The as coumn s he obec's
name. If he fe s a symbocnk, you' see a rang->and he pah o whch he symbocnk pons.
Looking a direcories
Somemes, you' wan o ook a a drecory, raher han nsde. For hesesuaons, you can specfy he -d opo
whch w e s o ook a any drecores ha woudnormayook nsde:
CodeLising3.4: Lisingdirecories
$ s -d usJ usJb1h usJX11R6b1h ..shaJe
dJwxJ-xJ-x 4 Joo1 Joo1 96 0ec 18 18.17 ..shaJe
dJwxJ-xJ-x 17 Joo1 Joo1 576 0ec 24 09.03 usJ
dJwxJ-xJ-x 2 Joo1 Joo1 3192 0ec 26 12.52 usJX11R6b1h
dJwxJ-xJ-x 2 Joo1 Joo1 14576 0ec 27 08.56 usJb1h
Recursiveand inode lisings
So you can use -d o ook a a drecory, bu you can asouse -R o do he oppose: no us ook nsdea drecory,
bu recursveyook nsdea he fes and drecores nsdeha drecory! We won' ncudeany exampeoupu for
hs opon(snce 's generayvoumnous), bu you may wan o ry a few s -R and s -R commandso ge a
fee for howhs works.
Fnay, he -1 s oponcan be usedo dspayhe nodenumbersof he fe sysemobecs n he sng:
CodeLising3.5: Lisinginodes
$ s -1 usJ
1409 X11R6 314258 1686-1hux 43090 1bexec 13394 sb1h
1417 b1h 1513 1686-pc-1hux-ghu 5120 oca 13408 shaJe
8316 d1s111es 1517 1hcude 776 mah 23779 sJc
43 doc 1386 1h1o 93892 poJ1age 36737 ss
70744 geh1oo-x86 1585 1b 5132 poJ1age.od 784 1mp
Undersandinginodes
Every obec on a fe sysems assgneda unquendex, caed an inoenumber. Ths mgh seemrva, bu
undersandngnodess essenao undersandngmany fe sysemoperaons. For exampe, consder he . and .. nk
ha appear n every drecory. To fuy undersandwha a .. drecory acuay s, we' frs ake a ook a /usr/oca's
nodenumber:
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
CodeLising3.7: Lisinginodefor a direcory
$ s -1d usJocab1h..
5120 usJocab1h..
As you can see, /usr/oca/bn/.. has he samenodenumber as /usr/oca! Here's howcan we come o grps wh hs
shockngreveaon. In he pas, we've consdered/usr/oca o be he drecory sef. Now, we dscover ha node
5120s n fac he drecory, and we havefoundwo drecory enres (caed "nks") ha pon o hs node. Boh /
usr/oca and /usr/oca/bn/.. are nks o node5120. Ahoughnode5120ony exss n one pace on dsk, mupe
hngs nk o . Inode5120s he acua enry on dsk.
In fac, we can see he oa number of mes ha node5120s referencedby usnghe s -d command:
CodeLising3.8: Lisinginodereference
$ s -d usJoca
dJwxJ-xJ-x 8 Joo1 Joo1 240 0ec 22 20.57 usJoca
If we ake a ook a he secondcoumn from he ef, we see ha he drecory /usr/oca (node5120) s referenced
egh mes. On my sysem, here are he varouspahs ha referencehs node:
CodeLising3.9: lnodereferences
usJoca
usJoca.
usJocab1h..
usJocagames..
usJoca1b..
usJocasb1h..
usJocashaJe..
usJocasJc..
mkdir
Le's ake a quck ook a he mkdr command, whch can be usedo creae newdrecores. The foowngexampe
creaes hree newdrecores, c, ac, and oe, a under /mp:
CodeLising3.10: Creaingdirecories
$ cd 1mp
$ mkd1J 11c 1ac 1oe
By defau, he mkdr commanddoesn' creae paren drecores for you; he enre pah up o he nex-o-as
eemen needs o exs. So, f you wan o creae he drecores wohdeJ1u, you'd needo ssuehree separae
mkd1Jcommands:
CodeLising3.11: Creaingparendirecories
$ mkd1J wohdeJ1u
mkd1J. cahho1 cJea1e d1Jec1oJy`wohdeJ1u`. ho such 11e oJ d1Jec1oJy
$ mkd1J woh
$ mkd1J wohdeJ
$ mkd1J wohdeJ1u
However, mkdr has a handy -p oponha es mkdr o creae any mssngparen drecores, as you can see here:
CodeLising3.12: Creaingparendirecoriesin onego
$ mkd1J -p easyasp1e
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
$ cd 1mp
$ 1ouch copyme
The ouch commandupdaeshe "mme" of a fe f exss (reca he sxhcoumn n s - oupu). If he fe
doesn' exs, hen a new, empy fe w be creaed. You shoudnowhavea 1mpcopymefe wh a szeof zero.
echo
Nowha he fe exss, e's add somedaa o he fe. We can do hs usnghe echo command, whch akes s
argumensand prns hem o sandardoupu. Frs, he echo commandby sef:
CodeLising3.14: Creainga file using'echo'
$ echo "11Js111e"
11Js111e
Now, he sameecho commandwh oupu redrecon:
CodeLising3.15: Usingoupuredirecion
$ echo "11Js111e" > copyme
The greaer-han sgnes he she o wre echo's oupu o a fe caed copyme. Ths fe w be creaed f doesn
exs, and w be overwrenf does exs. By ypngs -, we can see ha he copymefe s 10 byes ong, snce
conans he wordfrsfeand he newnecharacer:
CodeLising3.16: Viewingfile informaion
$ s - copyme
-Jw-J--J-- 1 Joo1 Joo1 10 0ec 28 14.13 copyme
ca and cp
To dspayhe conens of he fe on he ermna, use he ca command:
CodeLising3.17: Viewingfile conens
$ ca1 copyme
11Js111e
Now, we can use a basc nvocaonof he cp commando creae a copedmefe from he orgna copymefe:
CodeLising3.18: Copyingfiles
$ cp copyme cop1edme
Uponnvesgaon, we fnd ha hey are ruy separaefes; her nodenumbersare dfferen:
CodeLising3.19: Checkinginodedifferences
$ s -1 copyme cop1edme
648284 cop1edme 650704 copyme
mv
Now, e's use he mv commando rename"copedme" o "movedme". The nodenumber w remanhe same;
however, he fenameha pons o he nodew change.
CodeLising3.20: Renaminga file
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
Whewe're akngabou mv, e's ook a anoher way o use hs command. mv, n addono aowngus o
renamefes, asoaowsus o moveone or more fes o anoher ocaonn he drecory herarchy. For exampe, o
movevaJ1mpmy11e.1x1 o homedJobb1hs(whch happenso be my homedrecory,) I coud ype:
CodeLising3.21: Movinga file o a differenlocaion
$ mv vaJ1mpmy11e.1x1 homedJobb1hs
Afer ypnghs command, myfe.x w be movedo homedJobb1hsmy11e.1x1. And f homedJobb1hss on
dfferen fe sysemhan vaJ1mp, he mv commandw handehe copyngof myfe.x o he newfe syseman
erasng from he od fe sysem. As you mgh guess, whenmyfe.x s movedbeweenfe sysems, he myfe.x
a he newocaonw havea newnodenumber. Ths s becauseevery fe sysemhas s ownndependense of
nodenumbers.
We can asouse he mv commando movemupefes o a sngedesnaondrecory. For exampe, o move
myfe1.x and myarce3.x o /home/drobbns, I coud ype:
CodeLising3.22: Movingmuliplefiles
$ mv vaJ1mpmy11e1.1x1 vaJ1mpmyaJ11ce3.1x1 homedJobb1hs
4. Creaing Links and RemovingFiles
Hard links
We've menonedhe erm "nk" whenreferrngo he reaonshpbeweendrecory enres (he "names" we ype)
and nodes(he ndex numberson he underyngfe sysemha we can usuaygnore.) Thereare acuay wo knds
of nks avaabeon Lnux. The knd we've dscussedso far are caed har links. A gvennodecan haveany number
of hard nks, and he nodew perss on he fe sysemun he a he hard nks dsappear. Whenhe as hard nk
dsappearsand no programs hodnghe fe open, Lnux w deeehe fe auomacay. Newhard nks can be
creaed usnghe h command:
CodeLising4.1: Linkingfiles
$ cd 1mp
$ 1ouch 11Js11hk
$ h 11Js11hk secohd1hk
$ s -1 11Js11hk secohd1hk
15782 11Js11hk 15782 secohd1hk
As you can see, hard nks work on he nodeeve o pon o a parcuar fe. On Lnux sysems, hard nks have
severa maons. For one, you can ony makehard nks o fes, no drecores. Tha's rgh; evenhough. and .. a
sysem-creaed hard nks o drecores, you (evenas he "roo" user) aren' aowedo creae any of your own. The
secondmaonof hard nks s ha hey can' spanfe sysems. Ths means ha you can' creae a nk from /usr/
bn/basho /bn/bashf your / and /usr drecores exs on separaefe sysems.
Symbolic links
In pracce, symbocnks (or symlinks) are usedmore ofen han hard nks. Symnksare a speca fe ype where
he nk refers o anoher fe by name, raher han drecy o he node. Symnksdo no preven a fe from beng
deeed; f he arge fe dsappears, hen he symnkw us be unusabe, or broken.
A symbocnk can be creaed by passnghe -s opono n.
CodeLising4.2: Viewingsymboliclinks
$ h -s secohd1hk 1h1Jd1hk
$ s - 11Js11hk secohd1hk 1h1Jd1hk
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
Symbocnks are generaymore fexbehan hard nks. You can creae a symbocnk o any ype of fe sysem
obec, ncudngdrecores. And becausehe mpemenaonof symbocnks s basedon pahs (no nodes), 's
perfecy fne o creae a symbocnk ha pons o an obec on anoher physca fe sysem. However, hs fac ca
asomakesymbocnks rcky o undersand.
Consdera suaonwherewe wan o creae a nk n /mp ha pons o /usr/oca/bn. Shoudwe ype hs:
CodeLising4.3: Linkingdirecories , firsaemp
$ h -s usJocab1h b1h1
$ s - b1h1
JwxJwxJwx 1 Joo1 Joo1 14 Jah 1 15.42 b1h1 -> usJocab1h
Or aernavey:
CodeLising4.4: Linkingdirecories , secondaemp
$ h -s ..usJocab1h b1h2
$ s - b1h2
JwxJwxJwx 1 Joo1 Joo1 16 Jah 1 15.43 b1h2 -> ..usJocab1h
As you can see, boh symbocnks pon o he samedrecory. However, f our secondsymbocnk s ever moved
o anoher drecory, w be "broken" becauseof he reavepah:
CodeLising4.5: Breakinga symboliclink
$ s - b1h2
JwxJwxJwx 1 Joo1 Joo1 16 Jah 1 15.43 b1h2 -> ..usJocab1h
$ mkd1J myhewd1J
$ mv b1h2 myhewd1J
$ cd myhewd1J
$ cd b1h2
bash. cd. b1h2. ho such 11e oJ d1Jec1oJy
Becausehe drecory /mp/usr/oca/bn doesn' exs, we can no onger changedrecores no bn2; n oher words,
bn2 s nowbroken.
For hs reason, s somemesa gooddea o avodcreang symbocnks wh reavepah nformaon. However
here are many cases wherereavesymbocnks come n handy. Consderan exampewhereyou wan o creae a
aernaenamefor a programn /usr/bn:
CodeLising4.6: Viewingfile informaionfor keychain
# s - usJb1hkeycha1h
-JwxJ-xJ-x 1 Joo1 Joo1 10150 0ec 12 20.09 usJb1hkeycha1h
As he roo user, you may wan o creae an aernaenamefor "keychan", such as "kc". In hs exampe, we have
roo access, as evdencedby our bashpromp changngo "#". We needroo access becausenorma users aren' ab
o creae fes n /usr/bn. As roo, we coud creae an aernaenamefor keychanas foows:
CodeLising4.7: Symlinkingkeychain
# cd usJb1h
# h -s usJb1hkeycha1h kc
# s - keycha1h
-JwxJ-xJ-x 1 Joo1 Joo1 10150 0ec 12 20.09 usJb1hkeycha1h
# s - kc
JwxJwxJwx 1 Joo1 Joo1 17 haJ 27 17.44 kc -> usJb1hkeycha1h
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
CodeLising4.8: Movinghesymboliclink
# mv usJb1hkeycha1husJb1hkc usJocab1h
# s - usJocab1hkeycha1h
-JwxJ-xJ-x 1 Joo1 Joo1 10150 0ec 12 20.09 usJocab1hkeycha1h
# s - usJocab1hkc
JwxJwxJwx 1 Joo1 Joo1 17 haJ 27 17.44 kc -> usJb1hkeycha1h
Becausewe usedan absouepah n our symbocnk, our kc symnks s ponngo /usr/bn/keychan, whch no
onger exss snce we moved/usr/bn/keychano /usr/oca/bn.
Tha means ha kc s nowa brokensymnk. Boh reaveand absouepahs n symbocnks haveher mers, and
you shouduse a ype of pah ha's appropraefor your parcuar appcaon. Ofen, eher a reaveor absoue
pah w work us fne. The foowngexampewoudhaveworkedevenafer boh fes weremoved:
CodeLising4.9: Movingfiles aroundwihsymlinks
# cd usJb1h
# h -s keycha1h kc
# s - kc
JwxJwxJwx 1 Joo1 Joo1 8 Jah 5 12.40 kc -> keycha1h
# mv keycha1h kc usJocab1h
# s - usJocab1hkeycha1h
-JwxJ-xJ-x 1 Joo1 Joo1 10150 0ec 12 20.09 usJocab1hkeycha1h
# s - usJocab1hkc
JwxJwxJwx 1 Joo1 Joo1 17 haJ 27 17.44 kc -> keycha1h
Now, we can run he keychanprogramby ypng/usr/oca/bn/kc. /usr/oca/bn/kc pons o he programkeychann
he samedrecory as kc.
rm
Nowha we knowhowo use cp, mv, and n, 's me o earn howo removeobecs from he fe sysem. Normay
hs s donewh he Jm command. To removefes, smpy specfy hem on he commandne:
CodeLising4.10: Removingfiles
$ cd 1mp
$ 1ouch 11e1 11e2
$ s - 11e1 11e2
-Jw-J--J-- 1 Joo1 Joo1 0 Jah 1 16.41 11e1
-Jw-J--J-- 1 Joo1 Joo1 0 Jah 1 16.41 11e2
$ Jm 11e1 11e2
$ s - 11e1 11e2
s. 11e1. ho such 11e oJ d1Jec1oJy
s. 11e2. ho such 11e oJ d1Jec1oJy
Noeha under Lnux, once a fe s rm'ed, 's ypcay goneforever. For hs reason, many unor sysem
admnsraorsw use he - oponwhenremovngfes. The - opones rm o removea fes n neracvemode
-- ha s, promp beforeremovngany fe. For exampe:
CodeLising4.11: Removingfiles andask for confirmaion
$ Jm -1 11e1 11e2
Jm. Jemove JeguaJ emp1y 11e `11e1`? y
Jm. Jemove JeguaJ emp1y 11e `11e2`? y
In he aboveexampe, he rm commandprompedwheheror no he specfedfes shoud*reay* be deeed. In
order for hem o be deeed, I had o ype "y" and Ener wce. If I had yped"n", he fe woudno havebeen
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
rmdir
To removedrecores, you havewo opons. You can removea he obecs nsdehe drecory and hen use Jmd1J
o removehe drecory sef:
CodeLising4.13: Removingdirecories
$ mkd1J myd1J
$ 1ouch myd1J11e1
$ Jm myd1J11e1
$ Jmd1J myd1J
Ths mehods commony referredo as "drecory remova for suckers." A rea power users and admnsraors
worhher sa use he much more convenenJm -J1 command, coverednex.
The bes way o removea drecory s o use he recursiveforce oponsof he rm commando e rm o removehe
drecory you specfy, as we as a obecs conanedn he drecory:
CodeLising4.14: Removinga compleedirecory
$ Jm -J1 myd1J
Generay, rm -rf s he preferredmehodof removnga drecory ree. Be very carefu whenusngrm -rf, snce s
power can be usedfor boh goodand ev :)
5. Using Wildcards
lnroducing Wild cards
In your day-o-day Lnux use, here are many mes whenyou may needo performa sngeoperaon(such as rm) o
many fe sysemobecs a once. In hesesuaons, can ofen be cumbersomeo ype n many fes on he
commandne:
CodeLising5.1: Removingindividualfiles
$ Jm 11e1 11e2 11e3 11e4 11e5 11e6 11e7 11e8
To sovehs probem, you can ake advanageof Lnux' bu-n wdcard suppor. Ths suppor, asocaed
"gobbng" (for hsorca reasons), aowsyou o specfymupefes a once by usnga wilcat pattern. Bashand
oher Lnux commandsw nerpre hs paernby ookngon dsk and fndngany fes ha mach . So, f you had
fes fe1 hroughfe8 n he curren workngdrecory, you coud removehesefes by ypng:
CodeLising5.2: Removingfiles usingshellcompleion
$ Jm 11e|1-8]
Or f you smpy wanedo removea fes whosenames begnwh fe as we as any fe namedfe, you coud ype
CodeLising5.3: Removingfiles usingshellcompleionwihhe* glob
$ Jm 11e*
The * wdca maches any characer or sequenceof characers, or even"no characer." Of course, gob wdcardsca
be usedfor more han smpy removngfes, as we' see n he nex pane.
Undersandingnon-maches
If you wanedo s a he fe sysemobecs n /ec begnnngwh g as we as any fe caed g, you coud ype:
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
$ s -d usJb1hasd1*1k
s. usJb1hasd1*1k. ho such 11e oJ d1Jec1oJy
Here's wha happened. Normay, whenwe specfy a paern, ha paernmaches one or more fes on he underyn
fe sysem, and bashreplaces the patternwith a space-separatelist of all matchingobjects. However, whenhe
paerndoesn' produceany maches, bashleavesthe argument, wilcars an all, as-is. So, hen s can' fnd he
fe /usr/bn/asdf*k and gves us an error. The operaverue here s ha glob patterns are expaneonly if they
match objects in the file system. Oherwsehey remanas s and are passederay o he programyou're cang.
Wild card synax: * and 7
Nowha we've seenhowgobbngworks, we shoudook a wdcard synax. You can use speca characers for wd
card expanson:
* w mach zero or more characers. I means "anyhngcan go here, ncudngnohng". Exampes:
/ec/g* maches a fes n /ec ha begnwh g, or a fe caed g.
/mp/my*1 maches a fes n /mp ha begnwh my and end wh 1, ncudnghe fe my1.
? maches any sngecharacer. Exampes:
myfe? maches any fe whosenameconsssof myfefoowedby a sngecharacer
/mp/noes?x woudmach boh /mp/noes.x and /mp/noesx, f hey exs
Wild card synax: l|
Ths wdcard s ke a ?, bu aowsmore specfcy. To use hs wdcard, pace any characers you'd ke o mac
nsdehe l|. The resuanexpressonw mach a sngeoccurrence of any of hesecharacers. You can asouse - o
specfy a range, and evencombneranges. Exampes:
myfel12| w mach myfe1 and myfe2. The wdcard w be expandedas ong as a eas one of hesefes
exss n he curren drecory.
lCc|hangelL |og w mach Changeog, ChangeLog, changeLog, and changeog. As you can see, usngbracke
wdcards can be usefu for machngvaraonsn capazaon.
s /ec/l0-9|* w s a fes n /ec ha begnwh a number.
s /mp/lA-Za-z|* w s a fes n /mp ha begnwh an upper or ower-case eer.
The l!| consruc s smar o he l| consruc, excep raher han machngany characers nsdehe brackes, '
mach any characer, as ong as s no sedbeweenhe l! and |. Exampe:
rm myfel!9| w removea fes namedmyfepus a sngecharacer, excep for myfe9
Wild card caveas
Here are somecaveas o wach ou for whenusngwdcards. Snce bashreas wdcard-reaedcharacers (?, l, |,
and *) specay, you needo ake speca care whenypngn an argumeno a commandha conans hese
characers. For exampe, f you wan o creae a fe ha conans he srnglfo|*, he foowngcommandmay no d
wha you wan:
CodeLising5.6: Wronguseof specialcharacers
$ echo |1o]* > 1mpmyhew11e.1x1
If he paernlfo|* maches any fes n he curren workngdrecory, hen you' fnd he names of hosefes nsde
mp/mynewfe.x raher han a era lfo|* ke you wereexpecng. The souon? We, one approachs o surround
your characers wh sngequoes, whch e basho performabsoueyno wdcard expansonon hem:
Gentoo IInux ocuentatIon -- II certIIIcatIon 101 (reIease 2) exa prep, Iart 1
Boh approaches(sngequoes and backsashescapng) havehe sameeffec. Snce we're akngabou backsash
expanson, nowwoudbe a goodme o menonha n order o specfy a era \, you can eher encose n snge
quoes as we, or ype \\ nsead( w be expandedo \).
Noe: Doube quoes w work smary o snge quoes, bu w s aow bash o do some med expanson.
Therefore, snge quoes are your bes be when you are ruy neresed n passng era ex o a command. For
more nformaon on wd card expanson, ype mah 7 gob. For more nformaon on quong n bash, ype mah 8 gob
and read he secon ed OUOTING. If you're pannng o ake he LPI exams, consder hs a homework assgnmen :)
6. Summary and Resources
Summary
Congrauaons; you've reachedhe end of our revewof Lnux fundamenas! I hopeha has hepedyou o frm up
your foundaonaLnux knowedge. The opcs you've earnedhere, ncudnghe bascs of bash, basc Lnux
commands, nks, and wdcards, havead he groundworkfor our nex uora on basc admnsraon, n whch we'
cover opcs ke reguar expressons, ownershpand permssons, user accoun managemen, and more.
By connungn hs uora seres, you' soonbe ready o aan your LPIC Leve 1 Cerfcaonfrom he Lnux
ProfessonaInsue. Speakngof LPIC cerfcaon, f hs s somehngyou're neresedn, hen we recommendh
you sudy he Resourcesn he nex pane, whch havebeencarefuy seecedo augmenhe maera coveredn h
uora.
Resources
In he "Bashby exampe" arce seres on deveoperWorks, Dane showsyou howo use bashprogrammng
consrucs o wre your ownbashscrps. Ths seres (parcuary Pars 1 and 2) w be goodpreparaonfor he LPIC
Leve 1 exam:
Bashby exampe, Par 1: Fundamena programmngn he Bourne-aganshe
Bashby exampe, Par 2: More bashprogrammngfundamenas
Bashby exampe, Par 3: Expornghe ebudsysem
If you're a begnnngor nermedaeLnux user, you reayowe o yoursef o check ou he Technca FAO for Lnu
users, a 50-pagen-dephs of frequeny-askedLnux quesons, aongwh deaedanswers. The FAO sef s n
PDF (Acroba) forma.
If you're no oo famar wh he v edor, see he deveoperWorksuora Inro o v. Ths uora gves you a gen
ye fas-paced nroducono hs powerfu ex edor. Consderhs mus-read maera f you don' knowhowo us
v.
Copyrigh 2001-2011 Genoo Foundaion , lnc. Ouesion

Anda mungkin juga menyukai