+ All Categories
Home > Documents > Linux Tips_2-Command Line Tricks

Linux Tips_2-Command Line Tricks

Date post: 04-Jun-2018
Category:
Upload: crstnscribid
View: 248 times
Download: 3 times
Share this document with a friend

of 26

Transcript
  • 8/13/2019 Linux Tips_2-Command Line Tricks

    1/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 #orthe #irst time e (an reveal the !"estion) *t is this: ho many (ommand+line tri(ks m"st a manmemorise, -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)

    nd so$ e s!"ee ed o"r brain (ells$ d"% thro"%h d"sty piles o# old iss"es o# in" 4ormat$ andsat 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 areeasier #or o"r$ er$ less+e perien(ed readers to en oy$ b"t e think even the most hardened in" veteran

    ill learn somethin% ne over the ne t 12$000 ords)o$ 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)))

    .8P : i# yo"/re lookin% #or %eneral in" tips$ (he(k o"t o"r previo"s t o arti(les: in" tips every%eek sho"ld kno

    http:.. )t" radar)(om.(ontent.lin" +tips+every+%eek+sho"ld+kno and

    More in" tips every %eek sho"ld kno )

    http:.. )t" radar)(om.(ontent.more+lin" +tips+every+%eek+sho"ld+kno5e also have an arti(le ith more ;ash tips #or po er "sers

    http:..t" radar)(om.(ontent.bash+tips+po er+"sers

    i# yo"/re ea%er to the nei%hbo"rhood ;ash i ard$ and an e (ellent %"ide to masterin% the in"(ommand line

    http:.. )t" radar)(om.(ontent.master+lin" +(ommand+line )

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    2/26

    >hey/re most o#ten "sed to stream the o"tp"t o# one pro%ram into the inp"t o# another) (ommon "se ishen there is too m"(h te t"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 e e("tion o# a sin%le (ommand$ b"t "sin% mk#i#oit/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 alsopossible 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" "st need to ro"te data into it) Aere/s a brie# e ample)

    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 ne t step$ "s"ally #rom another (onsole or "ser a((o"nt 8i# the permissions have been setypin%

    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 thehistory o# e isten(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 evenover the net ork) >he se(ret to this tri(k is in MPlayer/s +slave option$ hi(h tells the pro%ram toa((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

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    3/26

    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 .mplayer+(ontrol alone on/t ork)

    >here are plenty o# other (ommands yo" (an send to MPlayer + indeed$ any keyboard operation in thepro%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% A or Net(at) ee this e ample:

    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"seto the remote ma(hine/s MPlayer (ontrol #ile) @# (o"rse$ this (an be made m"(h #aster i# yo" have Akey 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 amba or N4 is easy on(e yo"/ve %ot it set "p on both (omp"ters$ b"t hat i# yo" "st ant to trans#er a #ile to another (omp"ter on the net ork itho"t the hassle o# settin% "pso#t are, *# the #ile is small$ yo" may be able to email it) *# the (omp"ters are in the same room$ andD ; devi(es are permitted on both$ yo" (an "se a D ; #lash drive$ b"t there is also another option)

    5oo# is a Python s(ript that ill r"n on any in" 8or similar< (omp"ter) >he name is an a(ronym #or5eb @##er @ne 4ile$ hi(h s"ms it "p #airly ell$ as it is a one+hit eb server) >here/s nothin% to

    install "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 DB 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 e it 8yo"(an "se the +( option to have it served more timeshis ill send a % ipped tarball o# the dire(tory$ and yo" (an repla(e + ith + or +" to %et a b ipped 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 filesAave yo" ever saved a #ile$ maybe a do nload$ then been "nable to #ind it, Maybe yo" saved it in a

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    4/26

    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# ero 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 e pand the ild(ard$ and +iname makes themat(h (ase+insensitive)

    *n(orre(t permissions (an (a"se obs("re errors sometimes) -o" may$ #or e ample$ have (reated a #ile inyo"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 D EB to the ("rrent "sername$ and a F reverses the res"lt o# thene t 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 #i the permissions

    fin ~ * -user + . 0 -e!ec su o chown +, . 012",1" 34

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

    5an wi th hogsAave yo" ever noti(ed that yo"r internet (onne(tion %oes really slo ly$ even tho"%h yo"/re notdo 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 * P) Even the slo est andlo est+band idth operations$ like "sin% a remote shell or lookin% "p a =N address$ be(ome pain#"llyslo or time o"t) *# yo"/re "sin% somethin% like a ;it>orrent (lient to "pload$ yo" (an limit the "ploadrate$ hi(h avoids this problem) ome other pro%rams$ like rsyn($ have a similar #eat"re$ b"t many donot) lso$ 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 haper$ hi(h "ses the t(

    8tra##i( (ontrol< (ommand to limit overall band idth "sa%e to sli%htly belo the ma im"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)

    et =@5N *NI and DP *NI to "st belo yo"r ma im"m band idth 8in kilobits.s

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    5/26

    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 toits 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 chroot5hether yo"/re a sysadmin in (har%e o# mission+(riti(al data (entres or a home tinkerer$ ive C=s are

    onder#"l to have aro"nd #or hen yo" %et into tro"ble) *# yo" mana%e to mess somethin% "p$ yo" (anboot #rom a Inoppi $ Db"nt"$ HBM or one o# several other ive C=s$ mo"nt yo"r hard diskpartitions and edit hatever #iles are needed to re(over #rom yo"r tro"bles) Ao ever$ there are somethin%s that (an/t be #i ed this easily$ be(a"se they need yo" to be in the system that needs #i in%)

    >he sol"tion is to "se the (hroot 8(han%e root< (ommand$ hi(h sets "p a orkin% environment ithina dire(tory) Note that the root in the name re#ers to the root dire(tory$ not the root "ser 8or s"per"sero #i a broken pass ord$ #or e ample$ yo" (o"ld boot #rom a live C=$ mo"nt yo"r disk/s root#ilesystem at .mnt.tmp and do this:

    su o -imount --#in / ev /mnt/tmp/ evmount -t proc none /mnt/tmp/procchroot /mnt/#in/#ash

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

    Password-free SSHDsin% A to (onne(t to a remote (omp"ter is (onvenient$ b"t it has a (o"ple o# dra ba(ks) @ne is thatyo" 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 apass ord (an be (ra(ked) (omple $ random lon% pass ord helps$ b"t that makes intera(tive lo%inseven more in(onvenient) *t/s more se("re to set A "p to ork ith no pass ords at all) 4irst yo"

    need to set "p a pair o# keys #or A$ "sin% ssh+key%en like this to %enerate B keys 8(han%e thear%"ment to dsa #or = keyshis (reates t o #iles in .)ssh$ idJrsa 8or idJdsa< ith yo"r private key and idJrsa) p"b ith yo"rp"bli( key) No (opy the p"bli( key to the remote (omp"ter and add it to the list o# a"thorised keys

    ith

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    6/26

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

    -o" (an then lo% o"t o# the A 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 andea(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 A to re#"se all (onne(tion itho"t a key$ makin% pass ord+(ra(kin% impossible)

    Block script kiddiesre yo" #ed "p ith yo"r system lo% #illin% "p ith reports o# h"ndreds 8or even tho"sands< o# #ailed

    A 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# aysto avoid them)

    >he best + provided yo" ill never need A 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)

    nother option is to r"n a pro%ram like 4ail2ban 8http:..#ail2ban)so"r(e#or%e) net< or

    =enyAosts 8 )denyhosts)nethese at(h yo"r lo% #iles #or repeated #ailed lo%in attempts #rom thesame *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) ttempts to (ra(k A %enerally ass"me it r"ns on thestandard port 22 (han%e that to a random$ hi%h+n"mbered port and the (ra(k attempts ma%i(allydisappear) Edit .et(.ssh.sshdJ(on#i% and (han%e the isten 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 (ommandeverytime yo" lo% in$ b"t yo" (an "se an alias to take (are o# that:

    alias myssh ssh -p )9)):

    Reclaim disk space4illin% a partition to 100G (an have an "npleasant e##e(t on yo"r system) 5hen servi(es and otherpro%rams (annot rite to their lo% #iles$ or (annot save data in .var$ yo" (o"ld be in tro"ble) >hesepro%rams on/t be able to save their data$ and typi(ally !"it o"t 8or$ in some e treme (ases$ (rashdramati(allyFo avoid this$ the e t2 and e t3 #ilesystems reserve KG o# their (apa(ity #or only rootpro(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 aK00H; drive) lso$ there is no need to reserve any spa(e on a #ilesystem not "sed #or root #iles$ s"(has .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

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    7/26

    y itho"t dist"rbin% the yo"r data and #iles)

    >"ne2#s is "sed to t"ne vario"s parameters o# an e t2 8or e t3< #ilesystem)

    *t (an be "sed to (han%e the vol"me label or the n"mber o# mo"nts bet een #or(ed e e("tion o# #s(kand 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) o:

    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 alar%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 reservedarea #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 ith90G o# the so#t are o"t there$ b"t it (an (a"se problems ith dependen(ies) 5hile the vario"s pa(ka%emana%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 LLmake install$ install Che(k*nstall #irst) -o" (an %et this #rom )asi(+lin" )(om)m . i to.(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 la(k are pa(ka%es) -o" (an spe(i#y thetype in a (on#i% #ile$ or it ill ask hen yo" r"n

    /configure

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    8/26

    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"rentire trail) 5hen yo" ant to step ba(k ards$ "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"$ "st "se the +P parameter 8ie p d +P< to make it resolve the symlink and sho yo"

    here yo" really are) nd i# yo" ant to (d into the real dire(tory rather than the symlink$ "st "se (dp d +P )

    Re"erse SSHA is one o# the most versatile tools #or in" $ 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"allo%i( A and "se the (lient to send data to the server) *t seems (o"nterint"itive$ b"t this approa(h (ansave 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 PN)

    -o"/ll need the @pen A server installed on yo"r ork ma(hine$ and #rom there yo" need to type the#ollo in% to t"nnel the A 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 portn"mber 123' on the home ma(hine #or the #or arded A session$ and this port needs to be both #reeto "se and not blo(ked by a lo(al #ire all) @n(e yo"/ve made the (onne(tion #rom ork$ yo" (an thentype 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 theo##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% NC) >he only problem yo" mi%ht #ind is that the #irst A 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 /Ieep live yes/and / erver live*nterval 60/ so that the (onne(tion doesn/t a"tomati(ally drop)

    Safe-delete command aliases5e all kno that horrible #eelin%: yo" type rm O and as yo"r #in%er heads to ards the Enter key$ thehorri#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 o"rney$ sendin% yo"r #iles to a s immin% oblivion o#eros and ones)

    ;y de#a"lt$ many Dni (ommands are destr"(tive) rm deletes #iles$ (p and mv over rite them itho"thesitation or mer(y) >here are options to add a level o# sa#ety + the +i or ++intera(tive ar%"ments #or theabove three (ommands ill ask yo" to (on#irm yo"r intent a#ter ea(h step ? b"t i# yo" had time to stopand 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 .)bashr()

    alias cp='cp -i'alias mv='mv -i'

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    9/26

    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 enablema im"m destr"(tiveness)

    liasin% a (ommand to itsel# is not limited to preventin% #ile arma%eddon + yo" (an also add optionsthat improve the o"tp"t o# a (ommand s"(h as addin% +h to ls or d# to %ive si es in h"man readableI;$ 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 + andthere are do ens 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%inner there are "st so many parameters)

    "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 (onvertyo"r #ile) >he #irst part is the inp"t 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) typi(al MEn(oder (ommand lookslike this:

    + menco er input avi -ovc lavc -ovc -lavcopts vco ec=mpeg 2vh 2v#itrate=9;&& -oac copy -ooutput 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$ espe(i#y MPEH' 8e!"ivalent to =ivQ< ith a variable bit+rate o# 1200) >he +oa( (opy is here the a"dioo"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 #inalparameter)

    >he %reat thin% abo"t MEn(oder is that it really takes advanta%e o# yo"r in" system) 4or e ample$yo" (an "se a television inp"t #or the so"r(e #ile$ or pass the video thro"%h a #ilter) -o" (an evenremove the bars yo" see in ides(reen #ilms "sin% the (rop (ommand)

    Smart untarrin

    >here/s a ro"tine to e tra(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"/retryin% to e tra(t) >his is here there/s a sli%ht problem) dmittedly$ it/s not a bi% one$ b"t hen yo" dothis eno"%h times$ it starts to be(ome a real annoyan(e) >he tro"ble is that yo" need to be able toremember hat kind o# ar(hive yo"/re "n+tarrin% be#ore yo" a"to+(omplete the #ile name) *t/s "s"allyeither b 2 or % $ b"t yo" need to spe(i#y either a / / or a / / 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 thenpassin% this thro"%h a (onditional /i#/ to determine the (orre(t (ommand #or e tra(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 "st passed on to the

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    10/26

    (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% thea(t"al #ile type) >his is e tra(ted #rom this line by pipin% the o"tp"t thro"%h a k) 5e then "st need to"se /i#/ to e e("te the (orre(t (ommand:

    if E "+ A8 _TB6 " = "#%ip;" F4 then

    tar "$1j" "$2"elif [ "$F ! _# % " = "g&i " () thentar "$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# < and pla(e it in yo"r path 8s"(h as .binhe tr (ommand is "sed #or deletin% e traneo"s o"tp"t$ s"(h as spa(es or tabs) >he most "se#"loption 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 si e 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 #ieldseparation)

    Aere/s an e ample 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$

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    11/26

    sorted by si e$ displayin% only the si e and the #ilename)

    Remote windows>he Q 5indo ystem "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 toallo remote Q (lients to (onne(t to a (entral Q server) -o" (o"ld think o# it as a thin (lient$ here theQ (lient (onsists o# "st a keyboard and a monitor$ (onne(ted to the server) >he positive side+e##e(t isthat this remote #"n(tionality is "st "nderneath the s"r#a(e o# yo"r in" bo )

    A #or ards Q indo s sessions a"tomati(ally$ hi(h means that i# yo" start an appli(ation on aremote ma(hine #rom an A (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" resi e the indo or (li(k ithin the "ser inter#a(e)

    !term - isplay 2& -e klamav he important part o# the (ommand is the display parameter) Aere$ this is :0$ hi(h is the #irst s(reen onthe remote system) >his is be(a"se Q "ses *P addresses and ports to spe(i#y a destination$ and e/vesimply omitted the address$ hi(h implies the lo(al ma(hine) -o" (o"ld "se lo(alhost:1 to spe(i#y these(ond s(reen)

    >he +e parameter that #ollo s ill r"n an appli(ation #rom the opened Qterm$ openin% Ilam on thesame s(reen as the Qterm (onsole) -o" (o"ld also "se the noh"p (ommand$ so that hen the Asession is terminated$ the appli(ation that/s r"nnin% remotely on/t be)

    Sdrawkcab>he "s"al (ommand #or readin% a te t #ile is (at 8or less i# yo" ant to read it pa%e by pa%e$ b"t that/snot hat e/re talkin% abo"t herehis 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 #irsthat/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 mi ed "p, 4orthat 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 @%% orbis#iles in random order ith one o#

    ls -9 ~/music/((/( ogg | shuff | mplayer -playlist -

    or

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    12/26

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

    !conf wallpapersH(on# orks more like the 5indo s re%istry editor$ and enables yo" to %et a((ess to many o# thehidden options and settin%s behind an app that aren/t editable in any other ay) -o" (an bro se all thepossible 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) >hisin(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 thepi(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 ns(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 e a(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 "st te t) -o" (o"lds ap set ith %et to display the path to yo"r ("rrent desktop ima%e) No try (han%in% i(ons$ settin% thede#a"lt #ile mana%er mode$ or even addin% email a((o"nts in Evol"tion)

    $ice% nice babyMost in" "sers kno o# the ni(e (ommand b"t #e a(t"ally "se it) Ni(e is one o# those (ommandsthat 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 in" 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 evenhen 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 ever (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(ripts 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 presentsanother$ more s"btle and more "se#"l option) *t red"(es the o##endin% task/s priority so that yo"r systemremains "sable hile still servin% the o##endin% pro(ess) B"nnin% a (ommand ith a di##erent priorityis 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 "nderthe (ol"mn labelled /N*/)

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    13/26

    *# 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 otherpro(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 aremote ma(hine or server) *# yo" "se NC$ the remote desktop (lient$ yo"/ve probably alreadyb"rro ed yo"r ay thro"%h a t"nnel a sensible te(hni!"e is to "se A$ hi(h is (ommonly

    employed #or remote lo%ins)

    @ne o# the best "ses o# A t"nnelin% is to a((ess 5ebmin$ the remote (on#i% tool that r"ns on a ebserver) -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"ndthis limitation by t"nnelin% ith A #rom the port that 5ebmin "ses to yo"r lo(al ma(hine$ like so:

    ssh -8 H&J&2localhost29&&&& remotehost

    "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 pro y servi(e "sin% A)

    *# yo" ere at a lo(ation here yo" (o"ldn/t a((ess Hoo%le or e;ay$ #or e ample$ yo" (o"ld (reate at"nnel to the pro y server and bro se #rom there) Most distrib"tions in(l"de a pro y server$ s"(h as

    !"id) >his needs to be installed and r"nnin% on the remote ma(hine #irst) !"id "ses port 312R$ so the(ommand to t"nnel !"id o"ld look somethin% like this:

    ssh -8 H&J&2localhost2)9;H remotehost

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

    $o one can hear you screenirt"al terminals are like (hildren: havin% one$ t o or even three brin%s oy 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 A (onne(tions to the same ma(hine) Not only isthis a aste o# band idth$ it/s also a si%n yo"/re a ne bie + hi(h yo"/re not$ ri%ht, eterans knothere/s a m"(h better ay to open m"ltiple terminals$ and it (omes in the #orm o# the HND s(reenpro%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

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    14/26

    still there$ and still a(tive this one is ne )

    >ype another (ommand$ e% ls)

    No $ press CtrlSa then 0 8 ero< + yo" sho"ld see yo"r ori%inal terminal a%ain) s yo" (an see$ CtrlSa isthe (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"slysele(ted indo $ CtrlSa then CtrlSn to s it(h to the ne t indo $ or CtrlSa then CtrlSp to s it(h tothe previo"s indo ) >o (lose indo s$ "st type e it)

    5hen yo"r last indo is (losin%$ yo" also e it s(reen and it ill print /s(reen is terminatin%/ to remindyo")

    lternatively + and this is the (oolest thin% abo"t s(reen + yo" (an press CtrlSa then d to deta(h yo"rs(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 "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% asled%ehammer to (ra(k an e%%$ then %et is #or yo") *ts in#o pa%e soberly des(ribes it as a "tility #or thenon+intera(tive do nload o# #iles #rom the eb 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/sper#e(t #or syn(hronisin% lo(al eb ar(hives) -o" don/t have to "se it in a s(ript either + it orks "st as

    ell hen e e("ted dire(tly #rom the shell 8http:.. %et)s"nsite)dkhe most strai%ht#or ard "se #or %et is to simply do nload a #ile re#eren(ed by a DB :

    + wget http2//localhost/somefile tar g%

    >his sho"ld present yo" ith a te t+based do nload bar) Dn#ort"nately$ i# the site "ses the A>>P

    proto(ol$ %et on/t s"pport ild(ards so yo" (o"ldn/t "se O)% #or do nloadin% m"ltiple #iles 8b"tyo" (o"ld i# the site "sed 4>P insteadhe mirror ar%"mentenables options s"itable #or mirrorin% a ebsite + in parti("lar$ re("rsion #or traversin% the hole

    ebsite tree) htmle tension is "sed #or sites that "se either CH* s(ripts to %enerate A>M $ or P #ilesthat need to be renamed a#ter they/re do nloaded) *# %et re(o%nises the (ontents$ it ill "st add theA>M e tension)

    #ter the trans#er has #inished$ %et %oes thro"%h the lo(al #iles to (han%e any remote re#eren(es so thesite (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/slabelled /de#"n(t/) ;e#ore e e plain 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)

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    15/26

    >ypin% ps " ill list all the pro(esses attrib"ted to the ("rrent "ser$ and yo" (an spe(i#y another "sername ith ps D "sername) @ne o# the most (ommon "ses #or ps is to list all the pro(esses r"nnin% onthe system$ and this (an be a(hieved by "sin% ps a" ) ;reakin% this (ommand do n$ the a ill list allthe pro(esses rather than those o# a sin%le "ser$ the " is the level o# details ret"rned #or ea(h pro(ess$and lists pro(esses s"(h as daemons that eren/t started #rom a terminal)

    de#"n(t pro(ess is one that as started by another pro(ess 8the parenthis (an happen i# the parent pro(ess has h"n% or (rashed)=e#"n(t pro(esses are also kno n as ombies$ and listed ith a /T/ stat"s in the o"tp"t #rom ps) >hey/renot !"ite as destr"(tive as the livin% dead$ as they (ons"me almost no system reso"r(es$ b"t on asystem 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 illstop the parent task$ so #irst make s"re it/s not essential) @n(e the parent pro(ess has been killed$ thesystem 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 keys5e rely on en(ryption to keep o"r data sa#e$ b"t this means e have vario"s keys and passphrases eneed to look a#ter) HPH key has a passphrase to prote(t it$ b"t hat abo"t #ilesystem keys or A

    a"thenti(ation keys) Ieepin% (opies o# them on a D ; sti(k may seem like a %ood idea$ "ntil yo" losethe 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 hatit is and the passphrase (o"ld be (ra(ked ith a di(tionary atta(k)

    n en(rypted ar(hive o# yo"r sensitive data has a (o"ple o# advanta%es: it prote(ts everythin% ith apass ord 8addin% a se(ond layer o# en(ryption in the (ase o# a HPH key< and it dis%"ises the (ontentso# the #ile) omeone #indin% yo"r D ; 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 e tra(t ith

    cc ecrypt Kstuff | tar !

    *# yo" really ant to hide yo"r data$ "se te%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 e tra(t it ith

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    16/26

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

    ( history lesson5hile (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 ardsthro"%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 thehistory as to make this approa(h "n#easible) >he history (ommand (an print the hole b"##er to thes(reen) >he most obvio"s "se #or this is to redire(t the (ommand/s o"tp"t into %rep to #ind a partiallyremembered (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+e e("ted simply by takin% this n"mber and pre#i in% an e (lamationmark to the position) 4or e ample$ F1016 o"ld e e("te the (ommand at point 1016 in

    ;ash/s history) nother tri(k is to #ollo the e (lamation mark ith part o# a previo"s (ommand) >hehistory ill then be sear(hed and hen a mat(h o(("rs$ this ill be e e("ted)

    .cd o"ld e e("te the last (d (ommand in the b"##er) adly$ these (ommands don/t add themselvesto the history b"##er$ so re("rsive e e("tion isn/t possible) nother ay o# a((essin% the (ommandhistory is thro"%h the sear(h #"n(tion a((essed by pressin% Ctrl+B #rom the prompt) #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 e e("te the (ommand) C"rsor le#t orri%ht ill insert the (ommand on to the prompt$ hile "p and do n takes yo" to the previo"s and ne t(ommands as "s"al)

    )innin ar uments5e (an/t tea(h yo" ho to be the vi(tor in every debate$ b"t e (an sho yo" ho to master anothertype 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, ay$ #or e ample$ yo" ant Himp to open the ten lar%est ima%es in adire(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 ar%s 8in(l"ded in every distro

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    17/26

    o no e have a list o# ten #iles) Ao do e tell Himp that e ant it to open them, 5e (an/t "stdire(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# ar%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 e ample above$ it says &>ake the top tenlist 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 ar%s$ all it e pe(ts is a list o# #iles$ separated by ne line (hara(ters or

    spa(es)s another e ample$ this line deletes all #iles older than ten days 8 se ith ca tion Fhere are

    ell over 100 o# these tools$ and t o o# the most "se#"l are (onvert and mo%ri#y + (ommands that rivalHimp #or #eat"res) Dsin% either$ yo" (an resi e$ bl"r$ (rop$ sharpen and trans#orm an ima%e itho"tever needin% to look at it) >he di##eren(e bet een (onvert and mo%ri#y is that ith the latter yo" (anover 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) t its simplest$ yo" "st need to in(l"de aso"r(e and destination #ilename$ and (onvert ill ork o"t all the details) >he #ollo in% e ample ill(onvert a PEH #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" e pe(t)(onvert O) p% O)pn% on/t ork$ #or e ample) >he ans er is to "se a sli%htly di##erent synta $ and

    repla(e the (onvert (ommand ith mo%ri#y) Bather than "se a ild(ard #or the destination #ormat$ ede#ine the type e ant ith the #ormat e tension) >o (onvert a %ro"p o# PEH ima%es into the PNH#ormat$ e o"ld "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 e ample$ i# yo" needed to s(ale or (rop a holedire(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 te t 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 name8%rahamVlo(alhosthe key to (han%in% the prompt lies in dynami( variables$ hi(h allo yo" to embed "se#"lin#ormation into the prompt) >o (reate the "sername.domain prompt that many distrib"tions "se$ yo"

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    18/26

    (o"ld type the #ollo in% or add it to .)bashJpro#ile or .)bashr( to make it permanent:

    e!port 6.9="E3u@3hF+"

    >he e port (ommand is settin% the P 1 environment variable) >his is "sed to hold the val"e o# theprompt$ 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 V W" displays the "sername$ hile Wh ill insertthe #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 #ormatstrin%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 (omple (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(apese!"en(es$ it/s simple to (han%e the de#a"lt prompt to somethin% #ar more "se#"l)

    *ext messa ins 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 do ens o# reminder appli(ations that pop "pith 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 messa%e)

    >he at (ommand is one o# those essential tools that most "sers have (ome a(ross at one point oranother) *n essen(e$ yo" type at ':K7 pm today into the (ommand line$ and at ill open a primitive te teditor) *n the editor$ yo" simply add ea(h (ommand yo" ant e e("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 "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 meri(an readershe (ommand e need to "se to pop an alert to the s(reen is messa%e)

    >his is a very simple tool that dates ba(k to the earliest years o# Q 5indo s$ and ill simply open asmall indo on yo"r s(reen that in(l"des a te t messa%e and a (lose b"tton) >he synta #or the

    messa%e (ommand %oes like this:

    !message - isplay 2& & "Bou are late"

    >he display part o# the (ommand is important be(a"se it lets messa%e kno hi(h s(reen to "se) *#yo" add the (ommand to at$ yo"/ve %ot a #le ible 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

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    19/26

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

    +inux suxMost o# the time people "se s" on the (ommand line$ to %ive themselves system administratorprivile%es so they (an edit an important (on#i%"ration #ile) ;"t yo"/ll also #ind yo"rsel# "sin% s" henyo"/re orkin% in a (onsole and ant to "se a %raphi(al appli(ation) 5hen yo" try to r"n a %raphi(alapp 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 adi##erent "ser) >his se("rity meas"re may seem over ealo"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 "sesthe host (ommand to a"thenti(ate ne "sers on the lo(al Q 5indo s session) "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 thisability every time yo" r"n yo"r Q session$ "st add the host (ommand to yo"r )bashr( #ile: it ill bee e("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 loopi# yo" "se s" to s it(h ba(k to the ori%inal "ser) Make s"re yo" "se the e it (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" $ hi(h yo" ill need to installman"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 ordire(tory) *t/s o#ten the !"i(kest ay to or%anise #iles$ altho"%h pressin% 42 in most #ile mana%es illlet 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 toola(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" .sbin.s" on/t a(t"ally (han%e the name o# the #ile$ b"t ill move it #rom the ."sr.bindire(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 e tension) *t/s #le ible eno"%h #orrenamin% and movin% sin%le #iles and dire(tories$ b"t mv doesn/t (ater #or more than one at a time$ and

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    20/26

    this %ets tedio"s very !"i(kly i# yo" need to (han%e more than a #e #iles)

    >hank#"lly$ arry 5all is at hand to save the day ith another (ommand yo" (ommonly #ind installedon the avera%e in" distrib"tion) >he (ommand is (ommonly (alled rename$ b"t yo" mi%ht have tooklook #or somethin% (alled prename i# rename is "sed by somethin% else on yo"r system) s yo" mi%hthave %"essed$ i# arry 5all has somethin% to do ith this$ it m"st be a Perl s(ript) -o" (an even openthe #ile ith a te t editor and (he(k yo"rsel#)

    >he %reat thin% abo"t prename is that it/s in(redibly #le ible #or renamin% %ro"ps o# #iles) ;"t its#le ibility is thanks to Perl/s po er#"l e pression parser hi(h isn/t all that int"itive i# yo"/re ne

    to e pressions) 4or e ample$ 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 "st the be%innin% tho"%h$ and a#ter yo"/ve mastered the pro(ess o# %eneratin% re%"lare pressions$ 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 in" )

    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 todete(t and re(ti#y) Myth> $ #or e ample$ 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 #i in% b"%s in their appli(ation$ all e (an do is a"tomati(allyrestart 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$ (ontrolpasses 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"e do mythba(kend done

    >he bea"ty o# this is that hen mythba(kend (rashes$ the hile loop ill simply start it a%ain) 5e (anmake 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 "st need to (o"nt the n"mber o# instan(es o# the pro(esses$ hi(h e do by %reppin% the n"mbero# 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 ero$ 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 gre

    "m*th ackend" gre -v gre c -l'

    if [$mco nt == +() then

    3. nsert ' hile tr e' code here

    else

    e0it 1

    fi

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    21/26

    Resol"in resol",confAavin% more than one net ork devi(e (an lead to problems o# priority) Normally only one devi(e hasa((ess to the internet) ny s"pportin% devi(es share that (onne(tion or serve data held on the ma(hinebehind 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 otherma(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 orkdevi(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 aslin" #ormat)(o)"k< to a n"meri( *P address that the net ork "nderstands 8212)113)202)71hey "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 te teditor #rom hi(h yo" need to add yo"r o n obs) ;"t the bi% !"estion #or the =ebian developers as/5hi(h te t editor,/ 4or in" "sers$ there/s no simple ans er and it/s a !"estion that/s (a"sed too manyMonty Python+es!"e "dean Pop"lar People/s 4ront+style #lame ars and too m"(h asted time toprovide a de#initive ans er) 5hether "sers pre#er Ema(s$ i 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+#riendlyNano 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)

    s 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

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    22/26

    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 area((eptable "sin% the +display editor parameter$ and its e a(tly the same #or all o# the other (ommandsthat reside ithin the .et(.alternatives dire(tory)

    Playin with timeAo many o# yo"r ma(hines mana%ed to s"((ess#"lly ne%otiate the transition o"t o# dayli%ht savin%stime 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 timeembedded 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 e e("ted onthe (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) ario"s options (an be "sed to inp"t or o"tp"t anythin% #rom the time innanose(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 one yo"r ma(hine is(on#i%"red #or) *# yo"/re in the DI$ hope#"lly this reads /; >/ #or ;ritish "mmer >ime at this time o#year) >he (on#i%"ration #ile #or this (an be tra(ed to .et(.time one$ hi(h ill (ontain a des(ription #oryo"r lo(ation) 4or ; >$ this is likely to be /E"rope. ondon/) *# this is ron%$ yo" (an (hoose a mores"itable time one #rom the ."sr.share. onein#o. dire(tory) >his dire(tory in(l"des a list o# many o# themore 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 bydate) >he other is the hard are (lo(k) >his resides in yo"r system ;*@ 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 bootpro(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) "sttypin% 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 "st be t o: the ;ash shell 8i# that/s yo"r (hoi(ehis isn/t m"(h "se: most people "se ps " to display all the pro(esses they o n$ and ps a" #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" U%repkon!"eror) 5ith ombie pro(esses$ yo" typi(ally %o h"ntin% #or pro(esses hen they start tomisbehave$ 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" knois r"nnin%) Dsin% Ion!"eror as an e ample$ yo" o"ld "st type

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    23/26

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    24/26

    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" e e("te the dir(olors (ommand on its o n$ the o"tp"t is a (on#"sion o# #ile types andse(ret (odes)

    >hese ill look somethin% like piX'0 33: or O)o%%X01 3K:) >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 andba(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'0 33: ill (olo"r the /pipe/symbol 8pi< ith a bla(k ba(k%ro"nd 8'0< and a yello #ore%ro"nd 833his 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 theend 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"nthe 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 sprinkleshis s it(hes to the virt"al terminal r"nnin% Q 8(hvt 7

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    25/26

  • 8/13/2019 Linux Tips_2-Command Line Tricks

    26/26

    Aere/s an e ample "sin% #ind) >he (ommon permission errors that res"lt #rom #ind not havin% a((essri%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 http:..identi)(a.t" radar or > itter http:..t itter)(om.t" radarhttp:.. )st"mble"pon)(om.s"bmit,"rlXhttp:.. )t" radar)(om.(ontent.(ommand+line+tri(ks+

    smart+%eeksLtitleXCommandSlineStri(ksS#orSsmartS%eeksSUS>" BadarS in"


Recommended