Sie sind auf Seite 1von 26

Command line tricks for smart geeks

Posted at 10:37pm on Monday November 16th 2009 Command line Everyone kno s the ans er to the !"estion o# li#e$ the "niverse and everythin% is &'2&$ b"t #or the #irst time e (an reveal the !"estion) *t is this: ho many (ommand+line tri(ks m"st a man memorise, -o" see$ %raphi(al "ser inter#a(es are all ell and %ood$ b"t hen yo" ant to %et .real. ork done it/s time to s it(h to the terminal) 0nd so$ e s!"ee1ed o"r brain (ells$ d"% thro"%h d"sty piles o# old iss"es o# 2in"3 4ormat$ and sat re#le(tin% !"ietly over many a pint o# ale$ all ith the %oal o# brin%in% yo" this: '2 a esome ne (ommand line tri(ks e think yo" o"%ht to (ommit to memory) 5e/ve tried to in(l"de a #e that are easier #or o"r$ er$ less+e3perien(ed readers to en6oy$ b"t e think even the most hardened 2in"3 veteran ill learn somethin% ne over the ne3t 12$000 ords) 7o$ strap yo"rsel# in and %et ready #or (ommand+line heaven: it/s time to ki(k ass and (he b"bble %"m$ and e/re all o"t o# %"m))) .8P7: i# yo"/re lookin% #or %eneral 2in"3 tips$ (he(k o"t o"r previo"s t o arti(les: 2in"3 tips every %eek sho"ld kno 9http:.. 9http:.. )t"3radar)(om.(ontent.lin"3+tips+every+%eek+sho"ld+kno : and )t"3radar)(om.(ontent.more+lin"3+tips+every+%eek+sho"ld+kno : More 2in"3 tips every %eek sho"ld kno ) 5e also have an arti(le ith more ;ash tips #or po er "sers 9http:..t"3radar)(om.(ontent.bash+tips+po er+"sers: i# yo"/re ea%er to the nei%hbo"rhood ;ash i1ard$ and an e3(ellent %"ide to masterin% the 2in"3 (ommand line 9http:.. )t"3radar)(om.(ontent.master+lin"3+(ommand+line:)<.

Make your own Bash wormholes


Even #or the ;ash a#i(ionado$ the mk#i#o (ommand is likely to be one o# the lesser "sed in yo"r (olle(tion) *t (reates a pipe #or sharin% data$ (onne(tin% t o r"nnin% "tilities ith a kind o# (ommand line ormhole) =ata sent into one end ill instantaneo"sly appear at the other) ;e#ore e look at ho to "se it$ it/s orth %oin% over ho e typi(ally see pipes) *# yo"/ve "sed the shell #or anythin% other than s(arin% yo"r #riends ith (at .dev.random$ yo"/ll be "sed to the idea o# pipes)

>hey/re most o#ten "sed to stream the o"tp"t o# one pro%ram into the inp"t o# another) 0 (ommon "se is hen there is too m"(h te3t"al o"tp"t #rom one (ommand to read) Pipin% this o"tp"t into another ? "s"ally either less or more + lets yo" pa"se and pa%e thro"%h the o"tp"t in yo"r o n time:
cat /var/log/messages | less

*n this instan(e$ the pipe is temporarily (reated #or the e3e("tion o# a sin%le (ommand$ b"t "sin% mk#i#o it/s possible to (reate persistent pipes that yo" (an "se #or similar tasks) >he /#i#o/ part o# the (ommand re#ers to the nat"re o# the pipe ? the data that/s #irst in is #irst o"t) Creatin% the pipe itsel# is as easy as typin% mk#i#o$ #ollo ed by the name yo" ish to (all it) *t/s also possible to set the permissions #or the pipe 8"sin% the ?mode parameter< so yo" (an restri(t a((ess) @n(e the pipe is (reated$ yo" 6"st need to ro"te data into it) Aere/s a brie# e3ample) 4irst e (reate the pipe$ and "se tail +# to o"tp"t any data that/s sent to it:
mkfifo fifo_pipe tail -f fifo_pipe

>he ne3t step$ "s"ally #rom another (onsole or "ser a((o"nt 8i# the permissions have been set<$ is to send data to the pipe) >ypin%
echo "This is a test" >> fifo_pipe

ill send the test messa%e$ hi(h ill itsel# be o"tp"t by the tail (ommand e atta(hed to the pipe) Bemote (ontrol MPlayer >here are t o types o# people in this orld: those ho think MPlayer is the best media player in the history o# e3isten(e$ and those ho are ron%) @ne o# MPlayer/s lesser+kno n #eat"res is the ability to (ontrol it #rom a (onsole$ a shell s(ript or even over the net ork) >he se(ret to this tri(k is in MPlayer/s +slave option$ hi(h tells the pro%ram to a((ept (ommands #rom the stdin stream instead o# keystrokes) Combine this ith the +inp"t option$ and (ommands are read #rom a #ile$ or a 4*4@) 4or instan(e$ try this in one terminal:
mkfifo ~/mplayer-control mplayer -slave -input file=/home/user/mplayercontrol filetoplay

>hen$ in another terminal or #rom a s(ript$ enter:


echo "pause" >~/mplayer-control

>his (ommand ill pa"se the ("rrently r"nnin% MPlayer$ and iss"in% the (ommand a%ain ill res"me

playba(k) Note that yo" have to %ive the #"ll path o# the (ontrol #ile to MPlayer$ ith .home."ser and so #orth$ be(a"se C.mplayer+(ontrol alone on/t ork) >here are plenty o# other (ommands yo" (an send to MPlayer + indeed$ any keyboard operation in the pro%ram tri%%ers a (ommand that yo" (an "se in yo"r (ontrol s(ript) -o" (an even operate MPlayer #rom another (omp"ter on the net ork$ "sin% 77A or Net(at) 7ee this e3ample:

ssh user@host "echo pause >mplayer-control"

Aere$ e lo% in to a remote ma(hine 8host< ith the "sername "ser$ and r"n a (ommand to send pa"se to the remote ma(hine/s MPlayer (ontrol #ile) @# (o"rse$ this (an be made m"(h #aster i# yo" have 77A key a"thenti(ation enabled$ as yo" don/t need to %ive the pass ord ea(h time)

Share files the easy way


4ile sharin% ith 7amba or N47 is easy on(e yo"/ve %ot it set "p on both (omp"ters$ b"t hat i# yo" 6"st ant to trans#er a #ile to another (omp"ter on the net ork itho"t the hassle o# settin% "p so#t are, *# the #ile is small$ yo" may be able to email it) *# the (omp"ters are in the same room$ and D7; devi(es are permitted on both$ yo" (an "se a D7; #lash drive$ b"t there is also another option) 5oo# is a Python s(ript that ill r"n on any 2in"3 8or similar< (omp"ter) >he name is an a(ronym #or 5eb @##er @ne 4ile$ hi(h s"ms it "p #airly ell$ as it is a one+hit eb server) >here/s nothin% to installE 6"st do nload the s(ript #rom the homepa%e at
www home uni!-ag org/ simon/woof html an" make it e!ecuta#le$ then share the file #y entering /woof /path/to/myfile

*t ill respond ith a DB2 that (an be typed into a eb bro ser on another (omp"ter on the net ork + no so#t are beyond a bro ser is needed) 5oo# ill serve the #ile to that (omp"ter and then e3it 8yo" (an "se the +( option to have it served more times<) 5oo# (an also serve a dire(tory$ like so:
/woof -% /some/"ir

>his ill send a %1ipped tarball o# the dire(tory$ and yo" (an repla(e +1 ith +6 or +" to %et a b1ipped or "n(ompressed tarball) *# others like 5oo# and ant to "se it$ yo" (an even let them have a (opy ith
/woof -s

Find lost files


Aave yo" ever saved a #ile$ maybe a do nload$ then been "nable to #ind it, Maybe yo" saved it in a

di##erent dire(tory or ith an "n"s"al name) >he #ind (ommand (omes in handy here:
fin" ~ -type f -mtime &

ill sho all #iles in yo"r home dire(tory modi#ied or (reated today) ;y de#a"lt$ #ind (o"nts days #rom midni%ht$ so an a%e o# 1ero means today) -o" may have "sed the +name option ith #ind be#ore$ b"t it (an do lots more) >hese options (an be (ombined$ so i# that el"sive do nload as an MP3 #ile$ yo" (o"ld narro the sear(h ith:
fin" ~ -type f -mtime & -iname '( mp)'

>he !"otation marks are needed to stop the shell tryin% to e3pand the ild(ard$ and +iname makes the mat(h (ase+insensitive) *n(orre(t permissions (an (a"se obs("re errors sometimes) -o" may$ #or e3ample$ have (reated a #ile in yo"r home dire(tory hile orkin% as root) >o #ind #iles and dire(tories that are not o ned by yo"$ "se:
fin" ~ * -user +,-./01

>he shell sets the environment variable D7EB to the ("rrent "sername$ and a F reverses the res"lt o# the ne3t test$ so this (ommand #inds anythin% in the ("rrent "ser/s dire(tory that is not o ned by that "ser) -o" (an even have #ind #i3 the permissions
fin" ~ * -user +-./0 -e!ec su"o chown +,-./012",1" 34

>he #ind man pa%e e3plains the "se o# +e3e( and many other possibilities)

5an"wi"th hogs
Aave yo" ever noti(ed that yo"r internet (onne(tion %oes really slo ly$ even tho"%h yo"/re not do nloadin% anythin%, ;e(a"se o# the ay most asymmetri( broadband (onne(tions are set "p$ i# yo" sat"rate the "pload band idth$ do nloads be(ome almost impossible) >his is be(a"se o# the ay the tra##i( is !"e"ed by the modem and the *7P) Even the slo est and lo est+band idth operations$ like "sin% a remote shell or lookin% "p a =N7 address$ be(ome pain#"lly slo or time o"t) *# yo"/re "sin% somethin% like a ;it>orrent (lient to "pload$ yo" (an limit the "pload rate$ hi(h avoids this problem) 7ome other pro%rams$ like rsyn($ have a similar #eat"re$ b"t many do not) 0lso$ r"nnin% t o s"(h pro%rams ill still (a"se problems$ i# ea(h has been told it (an "se 90G o# yo"r "pstream band idth) @ne sol"tion is a handy s(ript (alled 5onder 7haper$ hi(h "ses the t( 8tra##i( (ontrol< (ommand to limit overall band idth "sa%e to sli%htly belo the ma3im"m available) Het it #rom http:..lart()or%. ondershaper$ +p"t the shaper s(ript some here in yo"r path + ."sr. lo(al.bin is a %ood (hoi(e +and edit the start o# the s(ript to s"it yo"r system) 7et =@5N2*NI and DP2*NI to 6"st belo yo"r ma3im"m band idth 8in kilobits.s<

and r"n it) -o" sho"ld no #ind that heavy "ploads$ like p"ttin% photos on 4li(kr$ no lon%er dra% yo"r modem to its knees) 5hen yo"/re happy ith the settin%s$ set it to r"n at boot ith hatever method yo"r distro "ses)

Fix broken passwords with chroot


5hether yo"/re a sysadmin in (har%e o# mission+(riti(al data (entres or a home tinkerer$ 2ive C=s are onder#"l to have aro"nd #or hen yo" %et into tro"ble) *# yo" mana%e to mess somethin% "p$ yo" (an boot #rom a Inoppi3$ Db"nt"$ HBM2 or one o# several other 2ive C=s$ mo"nt yo"r hard disk partitions and edit hatever #iles are needed to re(over #rom yo"r tro"bles) Ao ever$ there are some thin%s that (an/t be #i3ed this easily$ be(a"se they need yo" to be in the system that needs #i3in%) >he sol"tion is to "se the (hroot 8(han%e root< (ommand$ hi(h sets "p a orkin% environment ithin a dire(tory) Note that the root in the name re#ers to the root dire(tory$ not the root "ser 8or s"per"ser< altho"%h the root "ser is the only one allo ed to r"n this (ommand) Chroot sets "p a /6ailed/ system ithin the spe(i#ied dire(tory$ one that has no a((ess to the rest o# the system and thinks that the %iven dire(tory is the root dire(tory) >o #i3 a broken pass ord$ #or e3ample$ yo" (o"ld boot #rom a live C=$ mo"nt yo"r disk/s root #ilesystem at .mnt.tmp and do this:
su"o -i mount --#in" /"ev /mnt/tmp/"ev mount -t proc none /mnt/tmp/proc chroot /mnt/#in/#ash

>he #irst line is needed to be(ome root on Db"nt") >he ne3t t o make the .dev. and .pro( dire(tories available inside the (hroot$ and the last one enters the (hrooted dire(tory$ r"nnin% a ;ash shell) No yo" (an r"n pass d$ or any other (ommand yo" need$ and #inish o## ith lo%o"t or press Ctrl+= to e3it)

Password-free SSH
Dsin% 77A to (onne(t to a remote (omp"ter is (onvenient$ b"t it has a (o"ple o# dra ba(ks) @ne is that yo" have to type the pass ord ea(h time yo" (onne(t$ hi(h is annoyin% in an intera(tive shell b"t "na((eptable ith a s(ript$ be(a"se yo" then need the pass ord in the s(ript) >he other is that a pass ord (an be (ra(ked) 0 (omple3$ random lon% pass ord helps$ b"t that makes intera(tive lo%ins even more in(onvenient) *t/s more se("re to set 77A "p to ork ith no pass ords at all) 4irst yo" need to set "p a pair o# keys #or 77A$ "sin% ssh+key%en like this to %enerate B70 keys 8(han%e the ar%"ment to dsa #or =70 keys<)
ssh-keygen -t rsa

>his (reates t o #iles in C.)ssh$ idJrsa 8or idJdsa< ith yo"r private key and idJrsa) p"b ith yo"r p"bli( key) No (opy the p"bli( key to the remote (omp"ter and add it to the list o# a"thorised keys ith

cat i"_rsa pu# >>~/ ssh/authori%e"_keys

-o" (an then lo% o"t o# the 77A session and start it a%ain) -o" ill not be asked #or a pass ord$ altho"%h i# yo" set a passphrase #or the key yo" ill be asked #or that) Bepeat this #or ea(h "ser and ea(h remote (omp"ter) -o" (an make this even more se("re by addin%
6asswor"7uthentication no

to /etc/ssh/sshd_config) >his (a"ses 77A to re#"se all (onne(tion itho"t a key$ makin% pass ord+ (ra(kin% impossible)

Block script kiddies


0re yo" #ed "p ith yo"r system lo% #illin% "p ith reports o# h"ndreds 8or even tho"sands< o# #ailed 77A lo%in attempts as s(ript kiddies try to %et on yo"r ma(hine, >hese do no harm as lon% as they #ail b"t they/re still annoyin%) >hank#"lly$ there are a n"mber o# ays to avoid them) >he best + provided yo" ill never need 77A a((ess #rom o"tside yo"r net ork + it to (lose port 22 on yo"r ro"ter$ then no+none (an %et in) 0nother option is to r"n a pro%ram like 4ail2ban 8http:..#ail2ban)so"r(e#or%e) net< or =enyAosts 8 )denyhosts)net<) >hese at(h yo"r lo% #iles #or repeated #ailed lo%in attempts #rom the same *P address$ then add that *P address to yo"r #ire all r"les to blo(k any #"rther (onta(t #rom there #or a hile) >he third option is ridi("lo"sly easy) 0ttempts to (ra(k 77A %enerally ass"me it r"ns on the standard port 22E (han%e that to a random$ hi%h+n"mbered port and the (ra(k attempts ma%i(ally disappear) Edit .et(.ssh.sshdJ(on#i% and (han%e the 2isten dire(tive to somethin% like this:
8isten )9)):

and restart sshd) >he only dra ba(k o# this is the in(onvenien(e o# havin% to add this port n"mber to the ssh (ommand everytime yo" lo% in$ b"t yo" (an "se an alias to take (are o# that:
alias myssh ssh -p )9)):

Reclaim disk space


4illin% a partition to 100G (an have an "npleasant e##e(t on yo"r system) 5hen servi(es and other pro%rams (annot rite to their lo% #iles$ or (annot save data in .var$ yo" (o"ld be in tro"ble) >hese pro%rams on/t be able to save their data$ and typi(ally !"it o"t 8or$ in some e3treme (ases$ (rash dramati(allyF<) >o avoid this$ the e3t2 and e3t3 #ilesystems reserve KG o# their (apa(ity #or only root pro(esses to "se) >his is a %ood idea$ b"t KG is a lot on lar%e drives ? #or instan(e$ it/s 2KH; on a K00H; drive) 0lso$ there is no need to reserve any spa(e on a #ilesystem not "sed #or root #iles$ s"(h as .home) >he %ood ne s is that not only is this KG not hard(oded into the #ilesystem$ it (an be (han%ed on the #l

y itho"t dist"rbin% the yo"r data and #iles) >"ne2#s is "sed to t"ne vario"s parameters o# an e3t2 8or e3t3< #ilesystem) *t (an be "sed to (han%e the vol"me label or the n"mber o# mo"nts bet een #or(ed e3e("tion o# #s(k and a host o# other$ more esoteri( settin%s$ b"t the options e are interested in here are +m and +r) >he #ormer (han%es the per(enta%e o# #ilesystem blo(ks reserved #or the root "ser$ hile the latter "ses an absol"te n"mber o# blo(ks) 7o:
tune;fs -m ; /"ev/s"a9

red"(es the reserved area to 2G o# the #ilesystem$ hi(h may be more appropriate #or i# yo" have a lar%e . or .var #ilesystem) *# yo"/re "sin% a drive o# K00H; or lar%er$ this is the best option) >his line o# (ode:
tune;fs -r & /"ev/s"a9

sets the #ilesystem to have no reserved blo(ks$ a %ood settin% #or .home that doesn/t need a reserved area #or the s"per"ser)

Create packa es
=o nloadin% an appli(ation/s so"r(e (ode and (ompilin% it yo"rsel#) >his is a strai%ht#or ard task ith 90G o# the so#t are o"t there$ b"t it (an (a"se problems ith dependen(ies) 5hile the vario"s pa(ka%e mana%ers have ays o# orkin% aro"nd this$ there is another ay) 5hen b"ildin% #rom so"r(e "sin% the standard a"totools method o# ).(on#i%"re LL make LL make install$ install Che(k*nstall #irst) -o" (an %et this #rom )asi(+lin"3) (om)m3.Ci1to.(he(kinstall altho"%h it may ell be in yo"r distro/s repositories) B"n this instead o# make install and$ instead o# installin% the ne #iles dire(tly to yo"r #ilesystem$ it #irst b"ilds a pa(ka%e and then installs that) Che(k*nstall orks ith =eb$ BPM and 7la(k are pa(ka%es) -o" (an spe(i#y the type in a (on#i% #ile$ or it ill ask hen yo" r"n
/configure << make << checkinstall

0part #rom the pa(ka%e type$ Che(k*nstall asks #or some other details) Most o# these are optional$ or (an be le#t at the de#a"lts$ b"t make s"re the name mat(hes the older version yo" are repla(in%$ other ise yo"r pa(ka%e mana%er ill %et (on#"sed) *nstallin% ith Che(k*nstall also makes it simpler to remove the pa(ka%e$ as there is no need to keep the so"r(e dire(tory aro"nd$ and some pro%rams don/t have a make "ninstall option any ay)

!et your cds in order


>r"e Dni3 ha(kers kno that (han%in% dire(tory (an be done in all sorts o# di##erent ays$ and ith all sorts o# di##erent #eat"res$ so soon everyone learns that the h"mble (d (ommand (an a(t"ally be their best #riend) -o" sho"ld already kno that (d C takes yo" to yo"r home dire(tory$ b"t real ha(kers don/t aste t o keystrokes on nothin%: 6"st type (d to %et the same res"lt) *# yo" 6"st strai%hten that s!"i%%le a little$ C be(omes + and yo" %et (d +$ the (ommand to navi%ate to and #rom the previo"s dire(tory) 4or more advan(ed "sers$ (d + 6"st isn/t eno"%h$ be(a"se it only lets yo" %o bet een the ("rrent dire(tory and the previo"s one) 0 better system is to "se p"shd and popd in pla(e o# (d) 7o$ rather than

typin% (d mydir "se p"shd mydir + it remembers the dire(tory here yo" ere$ then (ds into mydir) -o" (an r"n this on all sorts o# di##erent dire(tories$ and ;ash ill remember yo"r entire trail) 5hen yo" ant to step ba(k ards$ 6"st type popd to %o to the previo"s dire(tory) 4inally$ don/t yo" hate it hen yo"/re in a symlinked dire(tory and yo"/ve no idea here yo" are, 5orse$ r"nnin% p d to print the orkin% dire(tory makes it look like yo"/re not in a symlink at all) *# this happens to yo"$ 6"st "se the +P parameter 8ie p d +P< to make it resolve the symlink and sho yo" here yo" really are) 0nd i# yo" ant to (d into the real dire(tory rather than the symlink$ 6"st "se (d Mp d +PM)

Re"erse SSH
77A is one o# the most versatile tools #or 2in"3$ b"t most people only ever "se it one ay + to "se the server to send data to the (lient) 5hat yo" mi%ht not kno is that it/s also possible to s it(h the "s"al lo%i( 77A and "se the (lient to send data to the server) *t seems (o"nterint"itive$ b"t this approa(h (an save yo" havin% to re(on#i%"re ro"ters and #ire alls$ and is also handy #or a((essin% yo"r b"siness net ork #rom home itho"t NPN) -o"/ll need the @pen77A server installed on yo"r ork ma(hine$ and #rom there yo" need to type the #ollo in% to t"nnel the 77A server port to yo"r home ma(hine:
ssh -0 9;)=2localhost2;; home_machine

-o"/ll need to repla(e homeJma(hine ith the *P address o# yo"r home ma(hine) 5e/ve "sed port n"mber 123' on the home ma(hine #or the #or arded 77A session$ and this port needs to be both #ree to "se and not blo(ked by a lo(al #ire all) @n(e yo"/ve made the (onne(tion #rom ork$ yo" (an then type the #ollo in% at home to a((ess yo"r ork ma(hine:
ssh workusername@localhost -p 9;)=

>his ill open a session on yo"r ork ma(hine$ and yo" ill be able to ork as i# yo" ere at the o##i(e) *t/s not di##i("lt to modi#y the same pro(ed"re to a((ess #ile servers or even a remote desktop "sin% NNC) >he only problem yo" mi%ht #ind is that the #irst 77A session may time o"t) >o solve this$ open .et(.ssh.sshd)(on# on yo"r ork ma(hine and make s"re it (ontains /Ieep0live yes/ and /7erver0live*nterval 60/ so that the (onne(tion doesn/t a"tomati(ally drop)

Safe-delete command aliases


5e all kno that horrible #eelin%: yo" type rm O and as yo"r #in%er heads to ards the Enter key$ the horri#yin% realisation that yo" are in the ron% dire(tory hits yo"$ b"t yo" (an only at(h helplessly as yo"r #in%er (ompletes its short b"t destr"(tive 6o"rney$ sendin% yo"r #iles to a s immin% oblivion o# 1eros and ones) ;y de#a"lt$ many Dni3 (ommands are destr"(tive) rm deletes #iles$ (p and mv over rite them itho"t hesitation or mer(y) >here are options to add a level o# sa#ety + the +i or ++intera(tive ar%"ments #or the above three (ommands ill ask yo" to (on#irm yo"r intent a#ter ea(h step ? b"t i# yo" had time to stop and think abo"t "sin% them$ yo"/d have time to (he(k yo" ere in the ri%ht dire(tory or hatever) *# yo"/d like these to be the de#a"lt$ add these lines to .et(.pro#ile or C.)bashr()
alias cp='cp -i'

alias mv='mv -i'

alias rm='rm -i' to have these (ommands r"n ith the +i option by de#a"lt) -o" (an al ays "se +# i# yo" ant to enable ma3im"m destr"(tiveness) 0liasin% a (ommand to itsel# is not limited to preventin% #ile arma%eddon + yo" (an also add options that improve the o"tp"t o# a (ommandE s"(h as addin% +h to ls or d# to %ive si1es in h"man readable I;$ M; or H;)

Clarify your codecs


>he tro"ble ith havin% lots o# video #iles is that they/re o#ten "sin% lots o# di##erent #ile #ormats + and there are do1ens o# di##erent (ode(s to en(ode both the video and a"dio streams) -o"/re probably #amiliar ith the onder#"l MPlayer$ b"t hat yo" may not kno is that there/s a sister "tility (alled MEn(oder) *t/s b"ilt #rom the same (ode base as MPlayer and as a res"lt is (apable o# (onvertin% to and #rom all the same #ormats as its a((omplished siblin%) MEn(oder r"ns #rom the (ommand line$ and (an be a little less than int"itive #or the be%innerE there are 6"st so many parameters) P"st take a look at MEn(oder/s man pa%eF >he men(oder (ommand basi(ally "ses #o"r di##erent parameters to ork o"t ho yo" ant to (onvert yo"r #ile) >he #irst part is the inp"tE the se(ond is the o"tp"t video (ode($ the third #or the o"tp"t a"dio (ode( #ollo ed by the #inal parameter #or the (ommand/s o"tp"t) 0 typi(al MEn(oder (ommand looks like this:
+ menco"er input avi -ovc lavc -ovc -lavcopts vco"ec=mpeg=2vh>2v#itrate=9;&& -oac copy -o output avi

>his looks hard$ b"t it isn/t really) inp"t)avi is the #ile to be pro(essed$ and +ov( lav( tells MEn(oder hi(h o"tp"t (ode( to "se) >he parameters that #ollo are the options #or the (ode() *n this (ase$ e spe(i#y MPEH' 8e!"ivalent to =ivQ< ith a variable bit+rate o# 1200) >he +oa( (opy is here the a"dio o"tp"t (ode( sho"ld %o$ b"t in this (ase e/re simply (opyin% it into the so"r(e #ile$ hi(h is the #inal parameter) >he %reat thin% abo"t MEn(oder is that it really takes advanta%e o# yo"r 2in"3 system) 4or e3ample$ yo" (an "se a television inp"t #or the so"r(e #ile$ or pass the video thro"%h a #ilter) -o" (an even remove the bars yo" see in ides(reen #ilms "sin% the (rop (ommand)

Smart untarrin
>here/s a ro"tine to e3tra(tin% tarballs that starts ith openin% a (onsole$ (han%in% to the dire(tory o# yo"r tarball and then typin% the tar (ommand$ #ollo ed by the s it(hes #or hi(hever ar(hive yo"/re tryin% to e3tra(t) >his is here there/s a sli%ht problem) 0dmittedly$ it/s not a bi% one$ b"t hen yo" do this eno"%h times$ it starts to be(ome a real annoyan(e) >he tro"ble is that yo" need to be able to remember hat kind o# ar(hive yo"/re "n+tarrin% be#ore yo" a"to+(omplete the #ile name) *t/s "s"ally either b12 or %1$ b"t yo" need to spe(i#y either a /1/ or a /6/ be#ore yo" kno ) 5e (an s(ript o"r ay aro"nd this by "sin% the #ile (ommand to determine the #ile type$ and then passin% this thro"%h a (onditional /i#/ to determine the (orre(t (ommand #or e3tra(tion) -o" (o"ld (hoose to embed yo"r de#a"lt s it(hes into the s(ript$ b"t in this (ase they/re 6"st passed on to the

(ommand) >he s(ript starts by de#inin% the #ile type$ "sin% the #ollo in% (ode:
?*/#in/#ash @A8/_TB6/=+Cfile -# +;|awk ', print +91'D

5ith the /b/ s it(h$ the #ile (ommand ret"rns only a brie# line o# data$ ith the #irst strin% bein% the a(t"al #ile type) >his is e3tra(ted #rom this line by pipin% the o"tp"t thro"%h a k) 5e then 6"st need to "se /i#/ to e3e("te the (orre(t (ommand:
if E "+@A8/_TB6/" = "#%ip;" F4 then

tar "$1j" "$2" elif [ "$F !"_#$%"" = "g&i'" () then tar "$1&" "$2" fi @bvio"sly$ it/s easy to add yo"r o n types and make this part more (omprehensive) -o" no need to save yo"r s(ript ith a (onvenient name 8 e (hose l#3< and pla(e it in yo"r path 8s"(h as C.bin<) Dn+ tarrin% a #ile is then as easy as typin%:
+ lf! !vf ~/testfile #%;

#ld fa"ourites in Bash


*t/s al ays orth revisitin% #or%otten bash (ommands) >hree o# the most "se#"l that seem to have #allen #rom (ommon "se are ("t$ paste and the translate (ommand$ tr) ("t and paste do e3a(tly hat yo"/d e3pe(t$ and tho"%h they so"nd m"ndane it/s s"rprisin% ho po er#"l they (an be hen "sed either in the (ommand line or ithin a s(ript) ("t is %enerally a little more "se#"l than the paste (ommand) B"nnin% ("t takes part o# a line and redire(ts it to the standard o"tp"t) ;y de#a"lt$ it "ses tab as the #ield separator$ b"t this (an be (han%ed "sin% +d$ and #ields are sele(ted "sin% the +# #la%) paste e##e(tively allo s yo" to mer%e (ontents in (ol"mns$ like a verti(al (at) >he best ay to see ho this orks is to (reate t o te3t #iles$ ea(h ith three separate lines o# data) >he o"tp"t o# paste ill be the (ontents o# the #irst #ile in a (ol"mn to the le#t o# the se(ond) >he tr (ommand is "sed #or deletin% e3traneo"s o"tp"t$ s"(h as spa(es or tabs) >he most "se#"l option is +s$ hi(h removes repeated se!"en(es o# a sin%le (hara(ter) >ake the o"tp"t ls +al$ hi(h %enerates a lon% dire(tory listin% in(l"din% the si1e o# #iles padded ith spa(es #or better+lookin% o"tp"t) >he tr (ommand (an be "sed to remove these$ and provide a sin%le spa(e (hara(ter #or #ield separation) Aere/s an e3ample o# ho these (ommands (an ork to%ether:
ls -al --sort=si%e /usr/#in | tr -s ' ' | cut -" ' ' -f G$H

>he lon% o"tp"t o# ls is sorted and then piped to the translate (ommand) >his removes the paddin%$ leavin% ea(h #ield separated by a sin%le spa(e) ("t then "ses the spa(e (hara(ter as a #ield delimiter$ and takes #ields K and R #rom the o"tp"t) 5hat yo" %et is a list o# #iles$

sorted by si1e$ displayin% only the si1e and the #ilename)

Remote windows
>he Q 5indo 7ystem "ses a (lient+server model to (reate a display) Most o# the time yo" don/t noti(e$ be(a"se the (lient and the server are r"nnin% on the same ma(hine$ b"t it as developed this ay to allo remote Q (lients to (onne(t to a (entral Q server) -o" (o"ld think o# it as a thin (lient$ here the Q (lient (onsists o# 6"st a keyboard and a monitor$ (onne(ted to the server) >he positive side+e##e(t is that this remote #"n(tionality is 6"st "nderneath the s"r#a(e o# yo"r 2in"3 bo3) 77A #or ards Q indo s sessions a"tomati(ally$ hi(h means that i# yo" start an appli(ation on a remote ma(hine #rom an 77A (onsole$ the appli(ation indo ill appear lo(ally) >he indo is (omm"ni(atin% ith the remote ma(hine "sin% the Q proto(ol$ hi(h is hy there is a delay every time yo" resi1e the indo or (li(k ithin the "ser inter#a(e)
!term -"isplay 2& -e klamav <

*# the above pie(e o# (ode is r"n #rom an 77A (onsole (onne(ted to a remote ma(hine$ it o"ld open Qterm and r"n Ilam0N on the remote s(reen rather than yo"r lo(al one + yo" o"ldn/t be able to see it on yo"r s(reen) >his is "se#"l i# yo" need to start an appli(ation remotely$ s"(h as an email (lient or vir"s (he(ker) >he important part o# the (ommand is the display parameter) Aere$ this is :0$ hi(h is the #irst s(reen on the remote system) >his is be(a"se Q "ses *P addresses and ports to spe(i#y a destination$ and e/ve simply omitted the address$ hi(h implies the lo(al ma(hine) -o" (o"ld "se lo(alhost:1 to spe(i#y the se(ond s(reen) >he +e parameter that #ollo s ill r"n an appli(ation #rom the opened Qterm$ openin% Ilam0N on the same s(reen as the Qterm (onsole) -o" (o"ld also "se the noh"p (ommand$ so that hen the 77A session is terminated$ the appli(ation that/s r"nnin% remotely on/t be)

Sdrawkcab
>he "s"al (ommand #or readin% a te3t #ile is (at 8or less i# yo" ant to read it pa%e by pa%e$ b"t that/s not hat e/re talkin% abo"t here<) >his starts at the start and ends at the end$ hi(h is pretty lo%i(al b"t not al ays hat yo" ant) *# yo" ant to read a #ile ba(k ards 8say hen readin% a lo% #ile and yo" ant the most re(ent entries #irst< 6"st r"n (at ba(k ards) >hat/s ri%ht: ta( does the same as (at b"t ba(k ards) 5hat i# yo" don/t ant any parti("lar order b"t ant the lines o# the o"tp"t randomly mi3ed "p, 4or that e "se the (ommand sh"#) No this may not be parti("larly "se#"l ith lo%#iles 8@I$ it/s (ompletely "seless ith lo% #iles< b"t hat i# yo" have a list o# m"si( #iles yo" ant to pass to a m"si( player, >he inp"t doesn/t have to be a #ile$ it (an be standard inp"t$ so yo" (an play yo"r @%% Norbis #iles in random order ith one o#
ls -9 ~/music/((/( ogg | shuff | mplayer -playlist -

or

mplayer +Cls -9 ~/music/((/( ogg | shuffD

!conf wallpapers
H(on# orks more like the 5indo s re%istry editor$ and enables yo" to %et a((ess to many o# the hidden options and settin%s behind an app that aren/t editable in any other ay) -o" (an bro se all the possible parameters by #irin% "p H(on#+editor #rom a (onsole) >his is a #ront+end to the tho"sands o# settin%s that Hnome keeps in the ba(k%ro"nd) >o #ind the path to yo"r desktop ba(k%ro"nd$ open the =esktop #older$ #ollo ed by Hnome and ;a(k%ro"nd) >his ill display a list o# settin%s that are appli(able to yo"r desktop ba(k%ro"nd) >his in(l"des ho yo"r ima%e is s(aled$ opa(ity val"e et() >he path to the ima%e is #o"nd "nder the pi(t"reJ#ilename parameter) >he (lever part is that yo" (an (han%e these settin%s #rom the (onsole$ and there#ore #rom yo"r o n s(ripts) @n(e yo"/ve #o"nd the parameter yo" ant to (han%e "sin% H(on#+editor$ "se %(on#tool+2 to (han%e this and syn(hronise the (han%e so it/s "pdated immediately) >he #ollo in% (ommand ill (han%e yo"r ba(k%ro"nd to test)pn%:
gconftool-; --type str ---set /"esktop/gnome/#ackgroun"/ picture_filename test png

5e/ve "sed e3a(tly the same path to the parameter that e "sed hen navi%atin% the #olders in H(on#+ editor) >he type parameter de#ines the val"e as a strin% be(a"se the #ilename is 6"st te3t) -o" (o"ld s ap set ith %et to display the path to yo"r ("rrent desktop ima%e) No try (han%in% i(ons$ settin% the de#a"lt #ile mana%er mode$ or even addin% email a((o"nts in Evol"tion)

$ice% nice baby


Most 2in"3 "sers kno o# the ni(e (ommand b"t #e a(t"ally "se it) Ni(e is one o# those (ommands that so"nd really %ood$ b"t yo" (an never think o# a reason to "se) @((asionally tho"%h$ it (an be in(redibly "se#"l) Ni(e (an (han%e the priority o# a r"nnin% pro(ess$ %ivin% it a %reater or smaller share o# the pro(essor) Ds"ally this is handled by the 2in"3 s(hed"ler) >he s(hed"ler %"arantees that pro(esses ith a hi%her priority 8like those that involve "ser inp"t< %et their share o# the reso"r(es) >his sho"ld ens"re that even hen yo"r system is at 100G CPD$ yo" ill still be able to move the indo s and (li(k on the mo"se) >he s(hed"ler doesn/t al ays ork smoothly$ ho everE (ertain tasks (an take over yo"r (omp"ter) >his (o"ld be a ay ard #ind (ommand that/s tri%%ered by a distro/s ho"sekeepin% s(riptsE or en(odin% a %ro"p o# video #iles that makes yo"r (omp"ter %rind to a halt) -o"/d typi(ally h"nt these pro(esses o"t ith the top (omand be#ore killin% them) Ni(e presents another$ more s"btle and more "se#"l option) *t red"(es the o##endin% task/s priority so that yo"r system remains "sable hile still servin% the o##endin% pro(ess) B"nnin% a (ommand ith a di##erent priority is as easy as enterin%
nice --9& up"ate"#

>his r"ns the "pdatedb ith a red"(ed priority o# +10) *# yo" r"n top yo" (an see the ni(e val"e "nder the (ol"mn labelled /N*/)

*# yo" ish to red"(e the priority o# a pro%ram that/s already r"nnin%$ yo" need to "se the reni(e (ommand ith the pro(ess *=:
? renice -9& -p 9:&H;9:&H;2 ol"

'riorit* +, ne- 'riorit* -1+ >his ill also red"(e the pro(ess/s priority by 10$ and dependin% on the ni(e val"e o# the other pro(esses$ ill lessen the amo"nt o# CPD time it ill share ith the other tasks)
..I #y pro!y

Crypto%raphi( t"nnels are a "se#"l ay to establish a se("re (onne(tion bet een yo"r lo(al PC and a remote ma(hine or server) *# yo" "se NNC$ the remote desktop (lient$ yo"/ve probably already b"rro ed yo"r ay thro"%h a t"nnelE a sensible te(hni!"e is to "se 77A$ hi(h is (ommonly employed #or remote lo%ins) @ne o# the best "ses o# 77A t"nnelin% is to a((ess 5ebmin$ the remote (on#i% tool that r"ns on a eb server) -o" (an (han%e almost anythin% on yo"r system "sin% 5ebmin$ so it/s "n ise to leave it open to the internet) ;"t i# yo" (lose it o##$ yo" lose the ability to (on#i%"re yo"r ma(hine) -o" (an %et aro"nd this limitation by t"nnelin% ith 77A #rom the port that 5ebmin "ses to yo"r lo(al ma(hine$ like so:
ssh -8 H&J&2localhost29&&&& remotehost

P"st point yo"r eb bro ser at https:..lo(alhost:R090 to (onne(t to yo"r remote 5ebmin server) -o" (o"ld also #or ard a pro3y servi(e "sin% 77A) *# yo" ere at a lo(ation here yo" (o"ldn/t a((ess Hoo%le or e;ay$ #or e3ample$ yo" (o"ld (reate a t"nnel to the pro3y server and bro se #rom there) Most distrib"tions in(l"de a pro3y server$ s"(h as 7!"id) >his needs to be installed and r"nnin% on the remote ma(hine #irst) 7!"id "ses port 312R$ so the (ommand to t"nnel 7!"id o"ld look somethin% like this:
ssh -8 H&J&2localhost2)9;H remotehost

*t/s then 6"st a matter o# (on#i%"rin% yo"r bro ser to "se lo(alhost:R090 as the pro3y server$ and all s"bse!"ent eb re!"ests ill be passed thro"%h the 77A t"nnel) Dsin% a pro3y server in this ay enables yo" to (onne(t to other ma(hines on the pro3y/s lo(al net ork$ s"(h as 192)16R)1)1$ and that also in(l"des servi(es like ro"ter (on#i%"ration servers)

$o one can hear you screen


Nirt"al terminals are like (hildren: havin% one$ t o or even three brin%s 6oy to yo"r li#e$ b"t more than that p"ts a strain on yo"r reso"r(es) 5hen orkin% remotely$ some people miss havin% the ability to open m"tliple terminals$ so they simply open many 77A (onne(tions to the same ma(hine) Not only is this a aste o# band idth$ it/s also a si%n yo"/re a ne bie + hi(h yo"/re not$ ri%ht, Neterans kno there/s a m"(h better ay to open m"ltiple terminals$ and it (omes in the #orm o# the HND s(reen pro%ram) >o %et started$ open "p a terminal$ type s(reen$ then hit Enter) -o"r terminal ill be repla(ed ith an empty prompt and yo" may think nothin% has (han%ed$ b"t a(t"ally it has + as yo"/ll see) >ype any (ommand yo" like$ e% "ptime$ and hit Enter) No press CtrlSa then ($ and yo" sho"ld see another blank terminal) =on/t orry$ yo"r old terminal is

still there$ and still a(tiveE this one is ne ) >ype another (ommand$ e% ls) No $ press CtrlSa then 0 81ero< + yo" sho"ld see yo"r ori%inal terminal a%ain) 0s yo" (an see$ CtrlSa is the (ombination that si%nals a (ommand is (omin% + CtrlSa then ( (reates a ne terminal$ and CtrlS( then a n"mber (han%es to that terminal) -o" (an "se CtrlSa then CtrlSa to s it(h to the previo"sly sele(ted indo $ CtrlSa then CtrlSn to s it(h to the ne3t indo $ or CtrlSa then CtrlSp to s it(h to the previo"s indo ) >o (lose indo s$ 6"st type e3it) 5hen yo"r last indo is (losin%$ yo" also e3it s(reen and it ill print /s(reen is terminatin%/ to remind yo") 0lternatively + and this is the (oolest thin% abo"t s(reen + yo" (an press CtrlSa then d to deta(h yo"r s(reen session) >hen$ #rom another (omp"ter later on$ "se s(reen +r to pi(k "p here yo" le#t o##$ ith all the pro%rams and o"tp"t inta(t 6"st as yo" le#t it + ma%i(F

Better than a browser


*# yo" o#ten need to retrieve pa%es #rom the net and #ind that "sin% a bro ser is like "sin% a sled%ehammer to (ra(k an e%%$ then %et is #or yo") *ts in#o pa%e soberly des(ribes it as a "tility #or the non+intera(tive do nload o# #iles #rom the ebE b"t hat they/re tryin% to say is that sometimes it orks better than "sin% a bro ser) -o" (an "se %et in a s(ript to do nload eb pa%es or #iles$ and it/s per#e(t #or syn(hronisin% lo(al eb ar(hives) -o" don/t have to "se it in a s(ript either + it orks 6"st as ell hen e3e("ted dire(tly #rom the shell 8http:.. %et)s"nsite)dk<) >he most strai%ht#or ard "se #or %et is to simply do nload a #ile re#eren(ed by a DB2:
+ wget http2//localhost/somefile tar g%

>his sho"ld present yo" ith a te3t+based do nload bar) Dn#ort"nately$ i# the site "ses the A>>P proto(ol$ %et on/t s"pport ild(ardsE so yo" (o"ldn/t "se O)%1 #or do nloadin% m"ltiple #iles 8b"t yo" (o"ld i# the site "sed 4>P instead<) %et is "sed most o#ten to mirror a hole ebsite) Aere/s an e3ample #or do nloadin% a site:
+ wget --mirror -p --html-e!tension --convert-links http2//localhost

5%et traverses the site and do nloads the (ontent into the ("rrent dire(tory) >he mirror ar%"ment enables options s"itable #or mirrorin% a ebsite + in parti("lar$ re("rsion #or traversin% the hole ebsite tree) htmle3tension is "sed #or sites that "se either CH* s(ripts to %enerate A>M2$ or 07P #iles that need to be renamed a#ter they/re do nloaded) *# %et re(o%nises the (ontents$ it ill 6"st add the A>M2 e3tension) 0#ter the trans#er has #inished$ %et %oes thro"%h the lo(al #iles to (han%e any remote re#eren(es so the site (an be vie ed o##line)

&illin 'ombies
*# yo" spend any time lookin% at yo"r pro(ess list$ sooner or later yo"/re %oin% to (ome a(ross one that/s labelled /de#"n(t/) ;e#ore e e3plain hat a de#"n(t pro(ess is and ho to remove it$ here/s a brie# overvie o# ho to !"ery the pro(ess table "sin% the ps (ommand)

>ypin% ps "3 ill list all the pro(esses attrib"ted to the ("rrent "ser$ and yo" (an spe(i#y another "ser name ith ps D "sername) @ne o# the most (ommon "ses #or ps is to list all the pro(esses r"nnin% on the system$ and this (an be a(hieved by "sin% ps a"3) ;reakin% this (ommand do n$ the a ill list all the pro(esses rather than those o# a sin%le "ser$ the " is the level o# details ret"rned #or ea(h pro(ess$ and 3 lists pro(esses s"(h as daemons that eren/t started #rom a terminal) 0 de#"n(t pro(ess is one that as started by another pro(ess 8the parent<$ b"t has #inished itho"t the parent aitin% #or (ompletion) >his (an happen i# the parent pro(ess has h"n% or (rashed) =e#"n(t pro(esses are also kno n as 1ombies$ and listed ith a /T/ stat"s in the o"tp"t #rom ps) >hey/re not !"ite as destr"(tive as the livin% dead$ as they (ons"me almost no system reso"r(es$ b"t on a system that/s al ays t"rned on$ s"(h as a server$ they (an be(ome e!"ally distra(tin%) >he key to killin% a de#"n(t pro(ess is to #irst kill the parent$ hi(h ill be listed in the o"tp"t o# ps ith the addition o# +l #or lon% o"tp"t) Parent pro(esses (an be identi#ied "nder the PP*= (ol"mn$ as opposed to the P*= (ol"mn #or the pro(ess *=) >hese are identi#iers atta(hed to ea(h pro(ess r"nnin% on yo"r system) >hey (an be killed "sin% another (ommon shell (ommand$ kill +9$ #ollo ed by the PP*=) @bvio"sly this ill stop the parent task$ so #irst make s"re it/s not essential) @n(e the parent pro(ess has been killed$ the system init pro(ess sho"ld send the (orre(t si%nal to the de#"n(t pro(ess$ hi(h sho"ld terminate a"tomati(ally)

Safe keys
5e rely on en(ryption to keep o"r data sa#e$ b"t this means e have vario"s keys and passphrases e need to look a#ter) 0 HPH key has a passphrase to prote(t it$ b"t hat abo"t #ilesystem keys or 77A a"thenti(ation keys) Ieepin% (opies o# them on a D7; sti(k may seem like a %ood idea$ "ntil yo" lose the sti(k and all yo"r keys enter the p"bli( domain) Even the HPH key is not sa#e$ as it is obvio"s hat it is and the passphrase (o"ld be (ra(ked ith a di(tionary atta(k) 0n en(rypted ar(hive o# yo"r sensitive data has a (o"ple o# advanta%es: it prote(ts everythin% ith a pass ord 8addin% a se(ond layer o# en(ryption in the (ase o# a HPH key< and it dis%"ises the (ontents o# the #ile) 7omeone #indin% yo"r D7; key o"ld see a data #ile ith no indi(ation o# its (ontents) C(rypt 8http:..((rypt)so"r(e#or%e)net< is a %ood (hoi(e #or this$ as it %ive stron% en(ryption and (an be "sed to en(rypt tar streams$ s"(h as
tar -c file9 file; | ccencrypt >stuff

and e3tra(t ith


cc"ecrypt Kstuff | tar !

*# yo" really ant to hide yo"r data$ "se 7te%hide 8http:..ste%hide)so"r(e#or%e)net< to hide the data ithin another #ile$ s"(h as a photo or m"si( #ile
steghi"e em#e" --em#e"file stuff --coverfile img_9=9L Mpg

and e3tra(t it ith

steghi"e e!tract --stegofile img_9=9L Mpg

( history lesson
5hile (ommand (ompletion is one o# the most "sed and la"ded aspe(ts o# ;ash$ an e!"ally po er#"l #eat"re o#ten %ets i%nored) >he history b"##er$ (ommonly "sed #or skippin% ba(k ards and #or ards thro"%h the previo"s (ommands list$ o##ers plenty more possibilities than mi%ht other ise be ima%ined) 5hile it/s easy eno"%h to ("rsor "p thro"%h the list$ sometimes the (ommand (an be so #ar ba(k in the history as to make this approa(h "n#easible) >he history (ommand (an print the hole b"##er to the s(reen) >he most obvio"s "se #or this is to redire(t the (ommand/s o"tp"t into %rep to #ind a partially remembered (ommand)
+ history | grep "comman""

*# the (ommand is still in the b"##er it ill be displayed alon% ith the n"meri( position it o(("pies) >his (ommand (an then be re+e3e("ted simply by takin% this n"mber and pre#i3in% an e3(lamation mark to the position) 4or e3ample$ F1016 o"ld e3e("te the (ommand at point 1016 in ;ash/s history) 0nother tri(k is to #ollo the e3(lamation mark ith part o# a previo"s (ommand) >he history ill then be sear(hed and hen a mat(h o(("rs$ this ill be e3e("ted) .cd o"ld e3e("te the last (d (ommand in the b"##er) 7adly$ these (ommands don/t add themselves to the history b"##er$ so re("rsive e3e("tion isn/t possible) 0nother ay o# a((essin% the (ommand history is thro"%h the sear(h #"n(tion a((essed by pressin% Ctrl+B #rom the prompt) 0#ter pressin% this (ontrol se!"en(e$ yo" ill be presented ith a sear(h prompt) >ypin% in the sear(h term brin%s "p the #irst (ommand that mat(hes the (riteria$ and pressin% ret"rn ill e3e("te the (ommand) C"rsor le#t or ri%ht ill insert the (ommand on to the prompt$ hile "p and do n takes yo" to the previo"s and ne3t (ommands as "s"al)

)innin ar uments
5e (an/t tea(h yo" ho to be the vi(tor in every debate$ b"t e (an sho yo" ho to master another type o# ar%"ment: those o# the (ommand+line) -o" probably already kno abo"t s"pplyin% m"ltiple ar%"ments to a pro%ram$ #or instan(e:
someprog file9 file; file)

>hat/s all %ood and ell hen yo"/re enterin% (ommands by hand$ b"t hat i# yo" ant to "se a list %enerated by another pro%ram, 7ay$ #or e3ample$ yo" ant Himp to open the ten lar%est ima%es in a dire(tory) -o" (o"ld do an ls +l to #ind o"t hat/s bi%%est$ and then laborio"sly enter ea(h #ilename as in %imp #ile1 #ile2))) @r yo" (o"ld "se the ma%i( that is 3ar%s 8in(l"ded in every distro<)
ls -. --color=never | hea" -n9& | !args gimp

3ar%s assembles (ompli(ated (ommand+line operations "sin% lists o# #iles$ so yo" don/t have to type them man"ally) -o" pass 3ar%s a list o# #iles$ and it arran%es them into #ile1 #ile2 #ile3))) as e sa be#ore) Makes sense, Consider the above e3ample) 4irst e "se ls to %enerate a list o# #iles in the ("rrent dire(tory 8makin% s"re e disable the (olo"r (odes as they prod"(e "n anted (hara(ters<) >hen$ "sin% the %ood old pipe 8U<$ e send the res"lts to the head "tility$ hi(h %rabs the #irst ten entries)

7o no e have a list o# ten #iles) Ao do e tell Himp that e ant it to open them, 5e (an/t 6"st dire(tly pipe the res"lts o# head thro"%h$ be(a"se Himp o"ld think it as ra %raphi(s data) >his is here the addition o# 3ar%s saves the day: it ens"res that the #ollo in% pro%ram name "ses the data as a #ilename list$ rather than tryin% to ork ith it dire(tly) *n o"r e3ample above$ it says &>ake the top ten list as %enerated by head$ and then pass it to Himp in the traditional #ile1 #ile2 #ile3))) #ormat) -o" (an do lots o# thin%s ith 3ar%s$ all it e3pe(ts is a list o# #iles$ separated by ne line (hara(ters or spa(es) 0s another e3ample$ this line deletes all #iles older than ten days 8/se -ith ca/tionF<:
fin" -mtime N9& | !args rm

Con"ert and mo rify


*ma%eMa%i(k is as perennial as the %rass) 4o"nd in everythin% #rom PAP s(ripts to I=E appli(ations$ it/s a #eat"re#"l bitmap editin% and ima%e pro(essin% library ith a battalion o# small tools) >here are ell over 100 o# these tools$ and t o o# the most "se#"l are (onvert and mo%ri#y + (ommands that rival Himp #or #eat"res) Dsin% either$ yo" (an resi1e$ bl"r$ (rop$ sharpen and trans#orm an ima%e itho"t ever needin% to look at it) >he di##eren(e bet een (onvert and mo%ri#y is that ith the latter yo" (an over rite the ori%inal ima%es$ rather than (reate a ne #ile #or yo"r modi#ied ima%es) =espite all this (ompli(ated #"n(tionality$ one o# the best reasons #or "sin% (onvert is its most obvio"s #"n(tion + and that is to (han%e the #ormat o# an ima%e) 0t its simplest$ yo" 6"st need to in(l"de a so"r(e and destination #ilename$ and (onvert ill ork o"t all the details) >he #ollo in% e3ample ill (onvert a PPEH #ile to a PNH:
convert pic Mpg pic png

*ma%eMa%i(k is per#e(t #or bat(hes o# ima%es$ b"t it doesn/t "se ild(ards in !"ite the ay yo" e3pe(t) (onvert O)6p% O)pn% on/t ork$ #or e3ample) >he ans er is to "se a sli%htly di##erent synta3$ and repla(e the (onvert (ommand ith mo%ri#y) Bather than "se a ild(ard #or the destination #ormat$ e de#ine the type e ant ith the #ormat e3tension) >o (onvert a %ro"p o# PPEH ima%es into the PNH #ormat$ e o"ld 6"st type the #ollo in%:
mogrify -format png ( Mpg

>his approa(h (an be "sed ith any o# the (onvert and mo%ri#y parameters) -o" (o"ld "se repla(e #ormat ith (rop or s(ale on a bat(h o# ima%es$ #or e3ample$ i# yo" needed to s(ale or (rop a hole dire(tory o# ima%es)

Bash it up
>he ;ash prompt is one o# those "n(han%in% thin%s that most people take #or %ranted) >his is odd$ be(a"se it/s a simple matter to (han%e the te3t that %reets yo" hen yo" open a ne shell to somethin% more "se#"l) >he ;ash prompt "sed by most distros is the "sername #ollo ed by the domain name 8%rahamVlo(alhost<) >he key to (han%in% the prompt lies in dynami( variables$ hi(h allo yo" to embed "se#"l in#ormation into the prompt) >o (reate the "sername.domain prompt that many distrib"tions "se$ yo"

(o"ld type the #ollo in%E or add it to C.)bashJpro#ile or C.)bashr( to make it permanent:
e!port 6.9="E3u@3hF+"

>he e3port (ommand is settin% the P71 environment variable) >his is "sed to hold the val"e o# the prompt$ hi(h is (ontained ithin the do"ble !"otes a#ter the e!"als symbol) >his val"e is t o dynami( variables either side o# the VE W" displays the "sername$ hile Wh ill insert the #irst part o# the hostname) ;ash in(l"des many other dynami( variables$ in(l"din% W #or the ("rrent orkin% dire(tory$ Wd #or the ("rrent date$ or WF #or the ("rrent position in the history b"##er) *# they look #amiliar$ that/s be(a"se they/re es(ape se!"en(es$ #o"nd in many appli(ations and pro%rammin% lan%"a%es and "sed to #ormat strin%s) >here are other se!"en(es$ s"(h as the #ollo in%$ hi(h (han%es the name o# the (onsole indo :
"3eF;4title3a"

>here as a time hen a ha(ker/s mettle o"ld depend on an obt"se and (omple3 (ommand prompt$ and there really isn/t a limit to hat yo" (an add) Many "sed simple (onditional loops to (han%e the (ontent o# the prompt henever a ne shell session as started$ b"t "sin% a #e simple es(ape se!"en(es$ it/s simple to (han%e the de#a"lt prompt to somethin% #ar more "se#"l)

*ext messa in
0s e all kno $ (omp"ters are here to do the monotono"s st"## that e (an/t be bothered to do) >hat means thin%s like addin% and s"btra(tin% n"mbers$ dra in% thin%s in pretty (olo"rs and remindin% their all+po er#"l masters o# "p(omin% appointments) >here are do1ens o# reminder appli(ations that pop "p ith annoyin% little messa%es hen yo"/re late #or the dentist) ;"t yo" don/t really need to "se one: yo" (an %et the same kind o# #"n(tionality "sin% a (o"ple o# (ommands #rom the tr"sty old terminal) >he (ommands in !"estion are at and 3messa%e) >he at (ommand is one o# those essential tools that most "sers have (ome a(ross at one point or another) *n essen(e$ yo" type at ':K7 pm today into the (ommand line$ and at ill open a primitive te3t editor) *n the editor$ yo" simply add ea(h (ommand yo" ant e3e("ted at ':K7$ #ollo ed by pressin% Ctrl and = at the same time to si%nal the end o# the #ile) Ea(h (ommand sho"ld look 6"st as it o"ld on the (ommand line$ and yo" (an even "se di##erent #ormats #or the time$ s"(h as 16K7 #or the 2'+ho"r (lo(k 8military time #or o"r 0meri(an readers<$ or /no S 2 ho"rs/$ as ell as spe(i#yin% the day as /7at"rday/ or /tomorro /) >he (ommand e need to "se to pop an alert to the s(reen is 3messa%e) >his is a very simple tool that dates ba(k to the earliest years o# Q 5indo s$ and ill simply open a small indo on yo"r s(reen that in(l"des a te3t messa%e and a (lose b"tton) >he synta3 #or the 3messa%e (ommand %oes like this:
!message -"isplay 2& & "Bou are late"

>he display part o# the (ommand is important be(a"se it lets 3messa%e kno hi(h s(reen to "se) *# yo" add the (ommand to at$ yo"/ve %ot a #le3ible method #or %ivin% yo"rsel# reminders itho"t havin% to resort to a separate appli(ation) >his is o#ten easier than "sin% somethin% like Cron$ hi(h (an be a

little da"ntin% hen all yo" ant to do is r"n a simple (ommand s"(h as 3messa%eF

+inux sux
Most o# the time people "se s" on the (ommand line$ to %ive themselves system administrator privile%es so they (an edit an important (on#i%"ration #ile) ;"t yo"/ll also #ind yo"rsel# "sin% s" hen yo"/re orkin% in a (onsole and ant to "se a %raphi(al appli(ation) 5hen yo" try to r"n a %raphi(al app hile lo%%ed in as a di##erent "ser$ yo" %et the #ollo in% error:
Oli#2 connection to "2& &" refuse" #y server

>he problem is that the keys "sed to a"thenti(ate yo"r Q session aren/t valid hen yo" s it(h to a di##erent "ser) >his se("rity meas"re may seem over1ealo"s$ b"t it/s a system inherited #rom a time hen Q sessions ere desi%ned to be r"n over a net ork) >here are t o ans ers to this problem) >he #irst is to make s"re that the ne a((o"nt (an be a"thenti(ated on the Q server) >his "ses the 3host (ommand to a"thenti(ate ne "sers on the lo(al Q 5indo s session) P"st type
!host Nlocal2local

as the normal "ser be#ore yo" s it(h to a ne "ser$ and yo" sho"ld see /non+net ork lo(al (onne(tions bein% added to a((ess (ontrol list/ as o"tp"t) -o" ill no be able to la"n(h any %raphi(al appli(ation on the same s(reen on(e yo"/ve s it(hed to another "ser) *# yo" ant to have this ability every time yo" r"n yo"r Q session$ 6"st add the 3host (ommand to yo"r )bashr( #ile: it ill be e3e("ted a"tomati(ally henever yo" open a ne ;ash shell) @ne important problem ith this sol"tion is that it be(omes possible to introd"(e an a"thenti(ation loop i# yo" "se s" to s it(h ba(k to the ori%inal "ser) Make s"re yo" "se the e3it (ommand instead) >he se(ond sol"tion is even easier) *t/s a repla(ement #or the s" (ommand that trans#ers yo"r Q (redentials to a ne session a"tomati(ally) >he (ommand is s"3$ hi(h yo" ill need to install man"ally #rom yo"r distrib"tion/s pa(ka%e mana%er) @n(e installed$ type
su! newuser

to s it(h to the "ser yo" ant to "se yo"r Q display) -o"/ll #ind that yo" (an r"n %raphi(al appli(ations #rom the ne session itho"t any #"rther (on#i%"ration)

Rename and prename


@n the (ommand line$ there (an/t be many people ho don/t "se the mv (ommand to rename a #ile or dire(tory) *t/s o#ten the !"i(kest ay to or%anise #iles$ altho"%h pressin% 42 in most #ile mana%es ill let yo" do the same thin% %raphi(ally) >he letters /mv/ are a(t"ally short #or /move/ + a m"(h more a(("rate des(ription o# hat this tool a(t"ally does be(a"se yo" (an in(l"de the path is the part yo" ant to rename) >his means that typin% mv ."sr.bin.s"3 .sbin.s"3 on/t a(t"ally (han%e the name o# the #ile$ b"t ill move it #rom the ."sr.bin dire(tory to the . sbin dire(tory) @# (o"rse$ yo" (o"ld still rename the #ile as ell i# yo" anted$ perhaps movin% it to a ba(k"p dire(tory and addin% the Jbak e3tension) *t/s #le3ible eno"%h #or renamin% and movin% sin%le #iles and dire(tories$ b"t mv doesn/t (ater #or more than one at a time$ and

this %ets tedio"s very !"i(kly i# yo" need to (han%e more than a #e #iles) >hank#"lly$ 2arry 5all is at hand to save the day ith another (ommand yo" (ommonly #ind installed on the avera%e 2in"3 distrib"tion) >he (ommand is (ommonly (alled rename$ b"t yo" mi%ht have took look #or somethin% (alled prename i# rename is "sed by somethin% else on yo"r system) 0s yo" mi%ht have %"essed$ i# 2arry 5all has somethin% to do ith this$ it m"st be a Perl s(ript) -o" (an even open the #ile ith a te3t editor and (he(k yo"rsel#) >he %reat thin% abo"t prename is that it/s in(redibly #le3ible #or renamin% %ro"ps o# #iles) ;"t its #le3ibility is thanks to Perl/s po er#"l e3pression parser hi(h isn/t all that int"itive i# yo"/re ne to e3pressions) 4or e3ample$ to add Jbak to the end o# every #ile startin% ith #ile$ yo" o"ld type the #ollo in%:
prename 's/3_#ak+//' file(

>his is 6"st the be%innin% tho"%h$ and a#ter yo"/ve mastered the pro(ess o# %eneratin% re%"lar e3pressions$ yo" (an a(hieve virt"ally any rename. move task ith prename + it/s espe(ially handy #or (han%in% "pper+(ase ima%e #ile names #rom indo s$ to lo er (ase #ile names on 2in"3)
Prash test "ummy

>here are times hen a ba(k%ro"nd pro(ess that yo" rely on silently (rashes$ hi(h (an be di##i("lt to dete(t and re(ti#y) Myth>N$ #or e3ample$ relies on a ba(k+end server to re(ord television pro%rams) *# the ba(k+end (rashes$ re(ordin%s ill be missed) @I$ so it sho"ld never have (rashed in the #irst pla(e$ b"t "ntil the developers %et ro"nd to #i3in% b"%s in their appli(ation$ all e (an do is a"tomati(ally restart the pro(ess) >he tri(k is to la"n(h the pro(ess yo" ant to restart #rom ithin a simple s(ript) *# it (rashes$ (ontrol passes ba(k to the s(ript$ hi(h (an start it a%ain) >his (an be done "sin% a simple hile loop) *t/s not %reat pro%rammin%$ b"t it does ork) hile tr"eE do mythba(kendE done >he bea"ty o# this is that hen mythba(kend (rashes$ the hile loop ill simply start it a%ain) 5e (an make the s(ript a little less (r"de by (he(kin% to see i# mythba(kend is r"nnin% be#ore the #irst la"n(h) 5e 6"st need to (o"nt the n"mber o# instan(es o# the pro(esses$ hi(h e do by %reppin% the n"mber o# instan(es o# the pro(ess #o"nd "sin% ps) *# mythba(kend is already r"nnin%$ the n"mber o# instan(es ill be a val"e other than 1ero$ hi(h e (an test #or "sin% an i# statement) 4inally$ on(e the s(ript has been ritten$ e need to make s"re that it on/t be a((idently stopped itsel#) 5e (an do this by r"nnin% the s(ript #rom a shell "sin% the no han%"p (ommand$ noh"p s(riptname L)
?*/#in/#ash

mco/nt = ''s a0 1 gre' "m*th2ackend" 1 gre' -v gre' 1 -c -l' if [$mco/nt == +() then 3. nsert '-hile tr/e' code here else e0it 1 fi

Resol"in resol",conf
Aavin% more than one net ork devi(e (an lead to problems o# priority) Normally only one devi(e has a((ess to the internet) 0ny s"pportin% devi(es share that (onne(tion or serve data held on the ma(hine behind a #ire all) >he problem is that the (ard itho"t the internet (onne(tion (an take priority over the (ard ith the internet (onne(tion) >his happens hen both net ork (ards are (onne(ted to other ma(hines or ro"ters that are a(tin% as independent =ACP servers and provide (on#li(tin% in#ormation) >he res"lt is that net ork pa(kets destined #or the internet are ro"ted thro"%h the ron% net ork devi(e$ droppin% the (onne(tivity) >he tro"ble (an o#ten be tra(ed to a sin%le (on#i% #ile$ .et(.resolv)(on#) >his is here yo"r system %ets the address o# the nameserver that it needs to translate the h"man readable server name 8s"(h as lin"3#ormat)(o)"k< to a n"meri( *P address that the net ork "nderstands 8212)113)202)71<) *# the nameserver (an/t translate the address$ yo" on/t be able to a((ess the internet "sin% readable server names$ b"t yo" (an "se the *P address #or a((ess) -o" (an solve this by editin% resolv)(on# to add the *P addresses o# yo"r *7P/s nameservers) Even so$ a#ter an ho"r or t o the (onne(tion mi%ht drop) *# yo" look at resolv)(on# a%ain$ the nameserver addresses ill have (han%ed) 0 =ACP (lient is r"nnin% on yo"r ma(hine and is attemptin% to determine the (onne(tion settin%s every (o"ple o# ho"rs) >he problem is that this (lient is prioritisin% the ron% net ork (ard and over ritin% the ron% nameserver address into resolv)(on#) >his is hy the sol"tion (an be #o"nd in .et(.dh(p3.dh(lient)(on#) >o add the (orre(t server name$ open this #ile in a te3t editor and add prepend domain+name+servers 9nameserver:E 8Bepla(e 9nameserver: ith the *P address o# yo"r *P/s nameserver<) 4rom no on$ this address ill al ays be added to resolv)(on# #irst$ be#ore any o# the ron%ly (on#i%"red =ACP servers) 0ll #i3ed)

-ditor redirection
*# yo" r"n a =ebian+based distrib"tion like Db"nt"$ have yo" ever ondered hat the mysterio"s .et(.alternatives dire(tory is #or, *# yo" take a look at its (ontents$ yo"/ll #ind that it/s #"ll o# some o# the most (ommon system (ommands) ;"t i# yo" look (losely$ ea(h #ile is really a symboli( link to the real lo(ation o# the (ommand else here in the #ilesystem) >his dire(tory is #"ll o# links be(a"se the ori%inal =ebian developers didn/t ant to ass"me one tool o"ld be "sed over any other) >hey "sed the (ron "tility to hi%hli%ht the problem) Cron is "sed to s(hed"le events to r"n at a (ertain dates and time$ and it does this by openin% a te3t editor #rom hi(h yo" need to add yo"r o n 6obs) ;"t the bi% !"estion #or the =ebian developers as /5hi(h te3t editor,/ 4or 2in"3 "sers$ there/s no simple ans er and it/s a !"estion that/s (a"sed too many Monty Python+es!"e P"dean Pop"lar People/s 4ront+style #lame ars and too m"(h asted time to provide a de#initive ans er) 5hether "sers pre#er Ema(s$ Ni or Nano$ a mandate to (hoose one over the other is al ays %oin% to (a"se problems) >he sol"tion as .et(.alternatives) *# yo" type (ron on Db"nt"$ it a(t"ally loads the ne bie+#riendly Nano editor) ;"t i# yo" look (losely$ (ron is a(t"ally la"n(hin% the editor (ommand lo(ated in ."sr.bin/ hi(h is itsel# a link to .et(.alternatives.editor) 0s yo" mi%ht have %"essed$ this #ile is a link to the real editor ? in this (ase it/s ."sr.bin.nano) >his is a (are#"l sidestep o# the iss"e o# hi(h editor to (hoose as all yo" have to do to (han%e the de#a"lt editor is (han%e the link to point at yo"r #avo"rite rather than Nano) >here/s even a (ommand that (an

per#orm this task #or yo") >ype


up"ate-alternatives --set e"itor /usr/#in/vim

to s it(h the editor to vim$ #or instan(e) -o" (an also list the available editors that are a((eptable "sin% the +display editor parameter$ and its e3a(tly the same #or all o# the other (ommands that reside ithin the .et(.alternatives dire(tory)

Playin with time


Ao many o# yo"r ma(hines mana%ed to s"((ess#"lly ne%otiate the transition o"t o# dayli%ht savin%s time in @(tober, *t/s important$ be(a"se there/s more to time than the shiny (lo(k sittin% in the (orner o# yo"r desktop panel + yo"r system is re%"lated by r"nnin% thin%s at a (ertain time) ;e it the time embedded in a sent email or the timestamp on a #ile$ everythin% depends on yo"r system (lo(k) >he simplest ay o# (he(kin% yo"r system (lo(k is to "se the date (ommand) 5hen date is e3e("ted on the (ommand line$ yo" %et a sin%le line o# o"tp"t that (ontains the date and time in an abbreviated #orm) -o" (an "se this o"tp"t #ormat to set the date and time as inp"t #or the date (ommand$ b"t it/s also easily ("stomised) Nario"s options (an be "sed to inp"t or o"tp"t anythin% #rom the time in nanose(onds to hi(h (ent"ry e/re in) >he last #ield in the o"tp"t #rom the date (ommand ill tell yo" hi(h time 1one yo"r ma(hine is (on#i%"red #or) *# yo"/re in the DI$ hope#"lly this reads /;7>/ #or ;ritish 7"mmer >ime at this time o# year) >he (on#i%"ration #ile #or this (an be tra(ed to .et(.time1one$ hi(h ill (ontain a des(ription #or yo"r lo(ation) 4or ;7>$ this is likely to be /E"rope. 2ondon/) *# this is ron%$ yo" (an (hoose a more s"itable time 1one #rom the ."sr.share.1onein#o. dire(tory) >his dire(tory in(l"des a list o# many o# the more pop"lar pla(es to live on the planet$ sorted by (ontinent and (o"ntry) >here are t o (lo(ks on board yo"r system) @ne is in the system (lo(k$ and this is the one probed by date) >he other is the hard are (lo(k) >his resides in yo"r system ;*@7 and keeps the time hile yo"r (omp"ter is t"rned o##) >he system (lo(k takes the time #rom the hard are (lo(k as part o# the boot pro(ed"re) -o" (an !"ery$ and set$ the hard are (lo(k "sin% the h (lo(k (ommand$ and by typin%
hwclock --systohc

yo" (an set the hard are (lo(k to the same time as the system (lo(k)

&illin time
@n(e yo" start "sin% the (ommand line$ yo" "se ps time and a%ain #or mana%in% yo"r pro(ess list) P"st typin% ps ill list the pro(esses that belon% to the ("rrent session$ hi(h "nless yo"/re r"nnin% anythin% in the ba(k%ro"nd ill 6"st be t o: the ;ash shell 8i# that/s yo"r (hoi(e<$ and the ps (ommand itsel#) >his isn/t m"(h "se: most people "se ps "3 to display all the pro(esses they o n$ and ps a"3 #or listin% every system pro(ess) *t/s easy to #ind the pro(ess yo"/re lookin% #or by passin% the o"tp"t o# ps into %rep$ as ith ps a"3U%rep kon!"eror) 5ith 1ombie pro(esses$ yo" typi(ally %o h"ntin% #or pro(esses hen they start to misbehave$ be#ore iss"in% a kill +9 pid to kill o## the o##ender) pid is the pro(ess identi#i(ation n"mber$ as listed in the o"tp"t #rom the ps (ommand) ;"t there is another option + "sin% a (ommand (alled pido# to %et the pro(ess *= o# a pro(ess yo" kno is r"nnin%) Dsin% Ion!"eror as an e3ample$ yo" o"ld 6"st type

pi"of kon>ueror

>he o"tp"t ill look somethin% like the #ollo in%:


pi"of kon>ueror 9H)H& 9H&;9 ;=H;G 9)&H9 L=:H L=:) L=:;

>his means that there are seven instan(es o# Ion!"eror ("rrently r"nnin%$ and ea(h n"mber is the pro(ess *= #or ea(h instan(e) >he lar%er the n"mber$ the more re(ent the pro(ess) 4or e3ample$ yo" (o"ld kill the last e3e("ted Ion!"eror by typin% kill +9 1R3R0) @ne o# the most "se#"l aspe(ts to pido# is that yo" (an "se it to ork o"t the pro(ess *= hen yo" (an/t man"ally si#t thro"%h the o"tp"t #rom ps) >his is per#e(t #or s(ripts that need to #ind and kill a pro(ess$ or maybe %ive them a hi%her or lo er system priority$ itho"t havin% to aste time lookin% thro"%h the o"tp"t o# ps)

+a'arus raised
>here/s nothin% !"ite like that #eelin% o# horror yo" see hen yo" %et the No "sable partitions.No @7 #o"nd messa%e #rom the over help#"l ;*@7) *t "s"ally takes a #e se(onds to sink in + yo"r hard drive has #ailed$ or is #ailin%$ and it no lon%er boots to the operatin% system) >here are many reasons hy this (o"ld have happened and ea(h varies in the severity o# potential data loss) 5ith a broken hard drive$ yo" mi%ht lose everythin%) ;"t it (o"ld also be the res"lt o# a simple boot loader error or over1ealo"s distro installation) *n these (ases$ there/s a %ood (han%e yo"r data may s"rvive inta(t + b"t hat do yo" do, >hose o# yo" ho keep timely ba(k"ps o# yo"r data (an sit ba(k$ smile sm"%ly and restore their hard ork #rom the latest ba(k"p) ;"t despite kno in% ho important it is$ most o# "s never seem to %et aro"nd to ba(kin% "p the data e spend o"r lives (ollatin%) *# ever there as a time #or the 2in"3 2ive C=$ this is it) 2ive C=s are st"##ed #"ll o# tools that (an be "sed to res"rre(t a hard drive$ and many o# these 2in"3 tools rival or s"rpass the #"n(tionality o# most (ommer(ial sol"tions) >he #irst thin% to do is mo"nt the lost drive #rom the 2ive C=) 5e/d s"%%est "sin% PC2in"3@7 as e/ve #o"nd this the best distro e/ve seen #or #indin% and mo"ntin% ay ard partitions) *t also does a %ood 6ob o# #indin% 5indo s N>47 partitions on the same drive) PC2in"3@7 ill a"tomati(ally dete(t any it #inds and mo"nt them onto the desktop) -o" sho"ld then be able to (opy yo"r data to a sa#e pla(e) *# this doesn/t ork$ yo"r savio"r is %oin% to be typin% testdisk #rom a root (onsole) >estdisk is one o# the most "nderrated 2in"3 tools and (an really make the di##eren(e bet een losin% or keepin% everythin%) *t/s per#e(t #or restorin% broken M;B re(ords and #or reb"ildin% partitions tables)

Paint by numbers
5e all like a little bit o# (olo"r in o"r lives$ and 6"st be(a"se the 2in"3 (ommand line is a te3t inter#a(e to the inner+ orkin%s o# yo"r system$ it doesn/t mean that it needs to s"##er the same mono(hrome #ate as printed te3t) >his tip ill sho yo" ho to es(apeF >here are vario"s ays to add (olo"r$ and one o# the most pop"lar is a((omplished ith the help o# a (ommand (alled dir(olors) *# this spellin% o##ends yo" 80meri(an readers$ look a ay<$ yo" (o"ld

al ays "se a symlink similar to the #ollo in% to amend it:


su"o ln -s /usr/#in/"ircolors /usr/#in/"ircolours

=ir(olors ill make di##erent #ile types appear as a rainbo o# (olo"rs hen yo" r"n the h"mble ls (ommand) *# yo" e3e("te the dir(olors (ommand on its o n$ the o"tp"t is a (on#"sion o# #ile types and se(ret (odes) >hese ill look somethin% like piX'0E33: or O)o%%X01E3K:) >he #irst part o# ea(h entry is the #ile type$ and the se(ond part 8a#ter the X symbol< (onsists o# t o val"es that represent a #ore%ro"nd and ba(k%ro"nd (olo"r) *# yo"/re (on#"sed by some o# the (rypti( abbreviations in the #irst part$ typin% /dir(olors ++print+database/ provides more verbose o"tp"t + revealin% that piX'0E33: ill (olo"r the /pipe/ symbol 8pi< ith a bla(k ba(k%ro"nd 8'0< and a yello #ore%ro"nd 833<$ #or e3ample) *# yo" look (losely at the o"tp"t #rom dir(olors$ yo" ill see that it starts ith 27JC@2@B7X and ends ith e3port 27JC@2@B7) >his is be(a"se the (ommand is doin% nothin% more than settin% a lar%e environment variable ith its list o# #ile types and (olo"rs) -o" (o"ld save this o"tp"t$ and add it to the end o# yo"r )bashr( #ile in yo"r home dire(tory to set these (olo"rs a"tomati(ally) ;"t on(e yo"/ve r"n the dir(olors (ommand$ yo"r (ommand prompt sho"ld start lookin% like a honey sat"rated tennis ball in a b"(ket o# h"ndreds and tho"sands 8aka sprinkles<) @h$ i# yo" don/t see any (olo"r a#ter that$ try typin%
ls --color=auto

!uaranteed screenshots
5e o#ten have a problem ith ill"stratin% %ame s be(a"se the %ame takes over the display and keyboard and$ "nless the developers have in(l"ded an internal s(reenshot #"n(tion$ it (an be hard to %rab the (ontents o# the s(reen and save it to a #ile) Even hen there is a indo ed %ame mode$ as ith Cold 5ar$ yo" still need to #ind a ay to break the keyboard a ay #rom the %ame and %ive (ontrol ba(k to the desktop be#ore yo" (an "se Hnome or I=E/s s(reenshot "tilities) >here is a sol"tion #or hen yo" (an/t es(ape the (l"t(hes o# an appli(ation that/s taken over yo"r Q 5indo s session) >he (l"e is that even hen yo" (an/t %et ba(k to yo"r desktop$ yo" (an nearly al ays %et ba(k to one o# the virt"al terminals aitin% patiently in the ba(k%ro"nd) Pressin% Ctrl45lt4F1 ill s it(h #rom yo"r desktop to the te3t+based lo%in o# the #irst virt"al terminal) >hese terminals hark ba(k to hen Dni3 as a predominantly m"lti+"ser environment$ and the /virt"al/ re#ers to the #a(t they are on the lo(al ma(hine rather than a remote d"mb terminal) @ther virt"al terminals are a((essible by s"bstit"tin% 41 ith 42+46$ and yo" (an %et ba(k to yo"r desktop by s it(hin% to the seventh virt"al terminal$ CtrlS0ltS47$ hi(h happens to be r"nnin% yo"r Q session) 5hat does this have to do ith takin% s(reenshots, 5ell$ as yo" (an %et to a (ommand line$ yo" are no able to take a s(reenshot "sin% one o# the many *ma%eMa%i(k tools yo" #ind installed on yo"r system by de#a"lt) Aere/s the (ommand to e3e("te:
chvt :4sleep 9&4import -"isplay 2& & -win"ow root image png

>his s it(hes to the virt"al terminal r"nnin% Q 8(hvt 7<$ aits ten se(onds$ then "ses *ma%eMa%i(k/s import (ommand to d"mp the (ontents o# the s(reen to ima%e)pn%) 7ortedF

*he reat SSH escape


@ne aspe(t o# 77A that (an make thin%s a lot easier hen yo" open a (onne(tion$ start a series o# 6obs and realise yo" need to #or ard a port thro"%h the ("rrent session) >he ans er is to "se an es(ape se!"en(e hile (onne(ted ith 77A to (han%e (ertain settin%s itho"t needin% to re(onne(t) 0n es(ape se!"en(e is 6"st a series o# (hara(ters that instr"(t the "tility yo"/re "sin% 8in this (ase 77A< to es(ape #rom hat it/s doin% and per#orm a "tility+spe(i#i( task) -o"/re most likely to have (ome a(ross es(ape se!"en(es hile "sin% the shell) >he most "se#"l es(ape se!"en(e #or 77A is e3e("ted by yo" pressin% the tilde 86< symbol$ #ollo ed by a ca'ital C 72* holding 8hift do n at the same time as /(/<) -o" on/t see anythin% in the session "ntil yo"/ve (ompleted the es(ape se!"en(e$ at hi(h point the prompt ill (han%e to /ssh:/) >his is to si%ni#y that yo"/ve been dropped into the 77A (ommand line) 4rom here yo" (an (onne(t a port on the remote ma(hine ith a port on the lo(al ma(hine$ and t"nnel the data bet een the t o thro"%h the se("re 77A (onne(tion) -o" (an "se this te(hni!"e to t"nnel the data #rom a 7!"id pro3y server thro"%h 77A to a lo(al port on yo"r ma(hine "sin% the +2 ar%"ment$ so typin% +2R090:lo(alhost:312R o"ld t"nnel 7!"id to lo(al port R090 itho"t restartin% the 77A session) -o" (an also list #or arded ports by "sin% the CY$ es(ape se!"en(e$ and (an(el #or arded ports by typin%
-Qrhostport To cancel the .>ui" tunnel we Must create"$ Must type -Q0)9;H

Redirect the masses


Even i# yo"/re a 2in"3 be%inner$ it/s likely yo"/ve already "sed some #orm o# redire(tion hile "sin% the (ommand line) Bedire(tion "ses the : and 9 symbols to pass data ba(k and #orth bet een (ommands yo"/re e3e("tin%) *t/s most (ommonly "sed to redire(t o"tp"t #rom one (ommand into another #ile) 4or e3ample$ i# yo" type dmes% :lo(al) lo%$ the (ontents o# the kernel rin% b"##er 8the o"tp"t #rom the dmes% (ommand< ill be redire(ted into the lo(al)lo% #ile rather than displayed on the s(reen) *# yo" "se t o : symbols$ the o"tp"t o# the dmes% (ommand o"ld be (on(atenated on to the end o# the #ile rather than "sed to over rite it) Dsin% the 9 symbol ill "se the ar%"ment as the inp"t$ rather than the destination + most (ommonly "sed ith %rep) >ypin%
grep -i -.5 Klocal log

o"ld sear(h #or /D7;/ in the lo(al)lo% #ile$ #or instan(e) Bedire(tion orks be(a"se every 2in"3 appli(ation$ bi% or small$ has three possible /#ile des(riptors/ to ork ith #or its inp"t and o"tp"t) >hese are the standard inp"t$ the standard o"tp"t and standard error) -o" don/t normally noti(e$ be(a"se the devi(es "sed #or standard inp"t and o"tp"t are "s"ally yo"r s(reen and keyboard) *n o"r earlier e3amples$ e ere addressin% the standard inp"t and o"tp"t des(riptors "sin% the :.9 symbols) ;"t ho do yo" address the standard error #ile des(riptor, >his is a(hieved by pla(in% the n"mber /2/ be#ore the : symbol + the 2 (omes #rom the priority %iven to ea(h #ile des(riptor) Nat"rally$ 0 is standard inp"t$ 1 is standard o"tp"t and 2 is the standard error o"tp"t) >his is "se#"l be(a"se it enables yo" to #ilter o"t error (onditions %enerated by a (ommand$ hile still sendin% o"tp"t to a lo% #ile)

Aere/s an e3ample "sin% #ind) >he (ommon permission errors that res"lt #rom #ind not havin% a((ess ri%hts are sent to the bla(k hole o# the n"ll devi(e$ hereas s"((ess#"l res"lts are o"tp"t to the s(reen:
fin" / -name ( Mpg ;>/"ev/null

-o" sho"ld #ollo "s on *denti)(a 9http:..identi)(a.t"3radar: or > itter 9http:..t itter)(om.t"3radar: 9http:.. )st"mble"pon)(om.s"bmit,"rlXhttp:.. )t"3radar)(om.(ontent.(ommand+line+tri(ks+ smart+%eeksLtitleXCommandSlineStri(ksS#orSsmartS%eeksSUS>"3BadarS2in"3:

Das könnte Ihnen auch gefallen