Nguyễn Vũ Hưng The Unix and GNU/Linux command line - power tools

2,398 views

Published on

Basic command lines under Unix and GNU/Linux

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
2,398
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
37
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide
  • No longer mentioned here [un]compress <file> Traditional Unix compression utility. Creates .Z files. Obsolete. Only kept for compatibility. Poor performance.
  • Nguyễn Vũ Hưng The Unix and GNU/Linux command line - power tools

    1. 1. The Unix and GNU/ Linux  Command Line  Nguyen Vu Hung   Vuhung16plus (at) gmail.com 2012/05/12 Based on “intro_unix_linux” by Michael Opdenacker  from Free Electrons
    2. 2. 12/05/12 Best viewed with...T h i s d o c u m e n t i s b e s t vi e w e d w i th aL i b re O ffi c e .T ak e ad van tag e o f i n te rn al o r e x te rn alh yp e rli n k s .S o , d o n ’t h e s i tate to c li c k o n th e m !F i n d p ag e s q u i c k ly th an k s toau to m ati c s e arc hU s e th u m b n ai ls to n avi g ate i n th ed o c u m e n t i n a q u i c k w ay 2
    3. 3. 12/05/12Command memento sheetI t i s a u s e fu l c o m p an i o n to th i s p re s e n tati o n .E x am p le s fo r th e m o s t u s e fu l c o m m an d s are g i ve n i nju s t o n e s h e e t.S u g g e s ti o n s fo r u s eS ti c k th i s s h e e t o n yo u r w all, u s e i t as d e s k to pw allp ap e r, m ak e i t a m o u s e m at, p ri n t i t o n c lo th i n g ,s li c e i t i n to b o o k m ark s ...C au ti o nS to re aw ay fro m m i c e ! 3
    4. 4. 12/05/12 Training Contents (1)S h e lls , fi le s ys te m an d fi le h an d li n gE ve ryth i n g i s a fi leG N U / L i n u x fi le s ys te m s tru c tu reC o m m an d li n e i n te rp re te rsH an d li n g fi le s an d d i re c to ri e sD i s p layi n g , s c an n i n g an d s o rti n g fi le sS ym b o li c an d h ard li n kF i le ac c e s s ri g h ts 4
    5. 5. 12/05/12 Training contents (2)S tan d ard I /O , re d i re c ti o n s , p i p e sS tan d ard i n p u t an d o u tp u t, re d i re c ti n g to fi le sP i p e s : re d i re c ti n g s tan d ard o u tp u t too th e r c o m m an d sS tan d ard e rro r 5
    6. 6. 12/05/12 Training Contents (3) T as k c o n tro lF u ll c o n tro l o n tas k s E x e c u ti n g i n b ac k g ro u n d , s u s p e n d i n g , re s u m i n g an d ab o rti n gL i s t o f ac ti ve tas k sK i lli n g p ro c e s s e sE n vi ro n m e n t vari ab le sP AT H e n vi ro n m e n t vari ab le sS h e ll ali as e s , .b as h rc fi le 6
    7. 7. 12/05/12 Training contents (4)M i s c e llan e o u sT e x t e d i to rsC o m p re s s i n g an d arc h i vi n gP ri n ti n g fi le sC o m p ari n g fi le s an d d i re c to ri e sL o o k i n g fo r fi le sG e tti n g i n fo rm ati o n ab o u t u s e rs 7
    8. 8. 12/05/12 Training contents (5) S ys te m ad m i n i s trati o n b as i c sF i le o w n e rs h i pS e tti n g u p n e tw o rk i n gF i le s ys te m s : c re ati n g an d m o u n ti n g G o i n g fu rth e rG e tti n g h e lp , ac c e s s i n g m an u al p ag e sS e arc h i n g th e I n te rn e t fo r re s o u rc e s 8
    9. 9. 12/05/12 Training contents (6)B as i c L AM P Ad m i n i s trati o nL in u xAp ac h eM yS Q LPH PG o i n g fu rth e rL AM P o n U b u n tuL AM P o n C e n tO S (R e d h at) 9
    10. 10. 12/05/12 GNU/Linux and Free Software“F re e ” as i n R M S s d e fi n i ti o n .T h i s p re s e n tati o n i n c lu d e d ani n tro d u c ti o n to F O S S : o p e rati n gs ys te m s , ap p li c ati o n s , k e y p ro je c ts an dru le s fo r s u c c e s s .P h i lo s o p h y o f th e G N U P ro je c t D e fi n i ti o n (fre e to ru n , s tu d y, d i s tri b u te , c h an g e & & c o p y) W h y S o ftw are S h o u ld N o t H ave O w n e rs C o p yle ft F re e D o c u m e n tati o n S e lli n g F re e S o ftw are i s O K 10
    11. 11. 12/05/12The Unix and GNU/Linux command line Unix/Linux Filesystem 11
    12. 12. 12/05/12 Everything is a fileAlm o s t e ve ryth i n g i n U n i x i s a fi le ! R e g u lar fi le s D e vi c e s an d p e ri p h e rals R e ad an d w ri te fro m D i re c to ri e s d e vi c e s as w i th re g u lar D i re c to ri e s are ju s t fi le s fi le s li s ti n g a s e t o f fi le s Pip e s S ym b o li c li n k s U s e d to c as c ad e F i le s re fe rri n g to th e p ro g ram s n am e o f an o th e r fi le cat *.log | grep  error S o c k e ts I n te r p ro c e s s c o m m u n i c ati o n /d e v/< s th > 12
    13. 13. 12/05/12 File names F i le n am e fe atu re s s i n c e th e b e g i n n i n g o f L i n u xC as e s e n s i ti ve (i n d e x .h tm l ! = I n d e x .H T M L )N o o b vi o u s le n g th li m i tC an c o n tai n an y c h arac te r(i n c lu d i n g w h i te s p ac e ,e x c e p t /).- F i le typ e s s to re d i n th e fi le (“m ag i c n u m b e rs ”).- F i le n am e e x te n s i o n s n o t n e e d e d an d n o ti n te rp re te d . - Ju s t u s e d fo r u s e r c o n ve n i e n c e .F i le n am e e x am p le s :README .bashrc Windows Buglistindex.htm index.html index.html.old 13
    14. 14. 12/05/12 File pathsA p ath i s a s e q u e n c e o f n e s te d d i re c to ri e s w i th a fi le o rd i re c to ry at th e e n d , s e p arate d b y th e / c h arac te rR e lati ve p ath : Documents/fun/microsoft_jokes.htmlR e lati ve to th e c u rre n t d i re c to ryAb s o lu te p ath : /home/vuhung/work/present.odt/ : ro o t d ire cto ry.S tart o f ab s o lu te p ath s fo r all fi le s o n th e s ys te m (e ve nfo r fi le s o n re m o vab le d e vi c e s o r n e tw o rk s h are d ). 14
    15. 15. 12/05/12GNU/Linux filesystem structure (1)N o t i m p o s e d b y th e s ys te m . C an vary fro m o n es ys te m to th e o th e r, e ve n b e tw e e n tw o G N U /L i n u xi n s tallati o n s !/ R o o t d i re c to ry/bin/ B as i c , e s s e n ti al s ys te m c o m m an d s/boot/ K e rn e l i m ag e s , i n i trd , c o n fi g u rati o nfi le s/dev/ F i le s re p re s e n ti n g d e vi c e s/dev/hda: fi rs t I D E h ard d i s k/etc/ S ys te m c o n fi g u rati o n fi le s/home/ U s e r d i re c to ri e s/lib/ B as i c s ys te m s h are d li b rari e s 15
    16. 16. 12/05/12GNU/Linux filesystem structure (2)/lost+found C o rru p t fi le s th e s ys te m tri e d tore c o ve r/media M o u n t p o i n ts fo r re m o vab le m e d i a: /media/usbdisk, /media/cdrom/mnt/ T e m p o rary m o u n t p o i n ts/opt/ S p e c i fi c to o ls i n s talle d b y th es ys ad m i n /usr/local/ o fte n u s e d i n s te ad/proc/ Ac c e s s to s ys te m i n fo rm ati o n /proc/cpuinfo, /proc/version .../root/ ro o t u s e r h o m e d i re c to ry/sbin/ Ad m i n i s trato r-o n ly c o m m an d s/sys/ System and device controls (cpu frequency, device power, etc.) 16
    17. 17. 12/05/12 GNU/Linux filesystem structure (3)/tmp/ T e m p o rary fi le s/usr/ R e g u lar u s e r to o ls (n o t e s s e n ti al to th es ys te m ) /usr/bin/, /usr/lib/, /usr/sbin.../usr/local/      S p e c i fi c s o ftw are i n s talle d b y th es ys ad m i n (o fte n p re fe rre d to /opt/)/var/ D ata u s e d b y th e s ys te m o r s ys te ms e rve rs/var/log/, /var/spool/mail (i n c o m i n g m ai l), /var/spool/lpd (p ri n t jo b s )...T h e U n i x fi le s ys te m s tru c tu re i s d e fi n e db y th e F i le s ys te m H i e rarc h y S tan d ard (F H S ):h ttp ://w w w .p ath n am e .c o m /fh s / 17
    18. 18. 12/05/12The Unix and GNU/Linux command line Shells and File Handling 18
    19. 19. 12/05/12Command line interpreters S h e lls : to o ls to e x e c u te u s e r c o m m an d s C alle d “s h e lls ” b e c au s e th e y h i d e th e d e tai ls o n th e u n d e rlyi n g o p e rati n g s ys te m u n d e r th e s h e lls s u rfac e . C o m m an d s are i n p u t i n a te x t te rm i n al, e i th e r a w i n d o w i n a g rap h i c al e n vi ro n m e n t o r a te x t- o n ly c o n s o le . R e s u lts are als o d i s p laye d o n th e te rm i n al. N o g rap h i c s are n e e d e d at all. S h e lls c an b e s c ri p te d : p ro vi d e all th e re s o u rc e s to w ri te c o m p le x p ro g ram s (vari ab le , c o n d i ti o n als , i te rati o n s ...) 19
    20. 20. 12/05/12 Well known shells M o s t fam o u s an d p o p u lar s h e llsbash: The Bourne Again shell (most popular)An improved implementation of sh with lots of added features too.sh: T h e B o u rn e s h e ll (o b s o le te )T rad i ti o n al, b as i c s h e ll fo u n d o n U n i x s ys te m s , b yS te ve B o u rn e .csh: T h e C s h e ll (o b s o le te )O n c e p o p u lar s h e ll w i th a C -li k e s yn taxtcsh: T h e T C s h e ll (s ti ll ve ry p o p u lar)A C s h e ll c o m p ati b le i m p le m e n tati o n w i th e vo lve dfe atu re s (c o m m an d c o m p le ti o n , h i s to ry e d i ti n g an dm o re ...) 20
    21. 21. 12/05/12fish: a great new shell T h e F ri e n d ly I n te rac ti ve S H e ll h ttp ://w w w .fi s h s h e ll.o rg /S tan d ard fe atu re s : h i s to ry, c o m m an d an d fi lec o m p le ti o n ...B ran d n e w fe atu re s : c o m m an d o p ti o n c o m p le ti o n ,c o m m an d c o m p le ti o n w i th s h o rt d e s c ri p ti o n , s yn taxh i g h li g h ti n g ..E as i e r to an y o p e n fi le s : open b u i lt-i n c o m m an d .M u c h s i m p le r an d c o n s i s te n t s yn tax (n o t P O S I Xc o m p li an t)M ak e s i t e as i e r to c re ate s h e ll s c ri p ts . C o m m an d li n e b e g i n n e rs c an le arn m u c h fas te r! E ve n e x p e ri e n c e d u s e rs s h o u ld fi n d th i s s h e ll ve ry c o n ve n i e n t. 21
    22. 22. 12/05/12 ls commandLists the files in the current directory, in alphanumeric order,except files starting with the “.” character. ls ­a (all) ls ­S (s i z e ) L i s ts all th e fi le s L i s ts th e b i g g e s t fi le s fi rs t (i n c lu d i n g .* fi le s ) ls ­r (re ve rs e ) ls ­l (lo n g ) R e ve rs e s th e s o rt o rd e r L o n g li s ti n g (typ e , d ate , s i z e , o w n e r, p e rm i s s i o n s ) ls ­ltr (o p ti o n s c an b e com b in e d ) ls ­t (time) L o n g li s ti n g , m o s t re c e n t Lists the most recent files first fi le s at th e e n d 22
    23. 23. 12/05/12File name pattern substitutions B e tte r i n tro d u c e d b y e x am p le s ! ls *txt T h e s h e ll fi rs t re p lac e s *txt b y all th e fi le an d d i re c to ry n am e s e n d i n g b y txt (i n c lu d i n g .txt), e x c e p t th o s e s tarti n g w i th ., an d th e n e x e c u te s th e ls c o m m an d li n e . ls ­d .* L i s ts all th e fi le s an d d i re c to ri e s s tarti n g w i th . ­d te lls ls n o t to d i s p lay th e c o n te n ts o f d i re c to ri e s . cat ?.log D i s p lays all th e fi le s w h i c h n am e s s tart b y 1 c h arac te r an d e n d b y .log 23
    24. 24. 12/05/12 Special directories (1) ./The current directory. Useful for commands taking a directory argument. Also sometimes useful to run commands in the current directory (see later).So ./readme.txt and readme.txt are equivalent. ../T h e p are n t (e n c lo s i n g ) d i re c to ry. Alw ays b e lo n g s toth e  . d i re c to ry (s e e ls ­a). O n ly re fe re n c e to th ep are n t d i re c to ry.T yp i c al u s ag e :cd .. 24
    25. 25. 12/05/12 Special directories (2) ~/N o t a s p e c i al d i re c to ry i n d e e d . S h e lls ju s ts u b s ti tu te i t b y th e h o m e d i re c to ry o f th ec u rre n t u s e r.C an n o t b e u s e d i n m o s t p ro g ram s , as i t i s n o t are al d i re c to ry. ~vuhung/S i m i larly, s u b s ti tu te d b y s h e lls b y th e h o m ed i re c to ry o f th e vuhung u s e r. 25
    26. 26. 12/05/12The cd and pwd commands cd <dir> C h an g e s th e c u rre n t d i re c to ry to <dir>. cd ­ Gets back to the previous current directory. pwd D i s p lays th e c u rre n t d i re c to ry ("w o rk i n g d i re c to ry"). 26
    27. 27. 12/05/12 The cp commandcp <source_file> <target_file>C o p i e s th e s o u rc e fi le to th e targ e t.cp file1 file2 file3 ... dirC o p i e s th e fi le s to th e targ e t d i re c to ry (las targ u m e n t).cp ­i (i n te rac ti ve )As k s fo r u s e r c o n fi rm ati o n i f th e targ e t fi lealre ad y e x i s tscp ­r <source_dir> <target_dir> (re c u rs i ve )C o p i e s th e w h o le d i re c to ry. 27
    28. 28. 12/05/12Smart directory copy with rsync rsync (re m o te s yn c ) h as b e e n d e s i g n e d to k e e p i n s yn c d i re c to ri e s o n 2 m ac h i n e s w i th a lo w b an d w i d th c o n n e c ti o n . O n ly c o p i e s fi le s th at h ave c h an g e d . F i le s w i th th e s am e s i z e are c o m p are d b y c h e c k s u m s . O n ly tran s fe rs th e b lo c k s th at d i ffe r w i th i n a fi le ! C an c o m p re s s th e tran s fe rre d b lo c k s P re s e rve s s ym b o li c li n k s an d fi le p e rm i s s i o n s : als o ve ry u s e fu l fo r c o p i e s o n th e s am e m ac h i n e . C an w o rk th ro u g h s s h (s e c u re re m o te s h e ll). Ve ry u s e fu l to u p d ate th e c o n te n ts o f a w e b s i te , fo r e x am p le . 28
    29. 29. 12/05/12 rsync examples (1)rsync ­a /home/vuhung/work /home/backup/work ­a: archive mode. Equivalent to ­rlptgoD... easy way to tell  you want recursion and want to preserve almost everything.rsync ­Pav ­­delete /home/vuhung/work/ /home/backup/work/ ­P: ­­partial (keep partially transferred files) and  ­­progress (show progress during transfer) ­­delete: delete files in the target which dont exist in the  source.Caution: directory names should end with / .  Otherwise, you get abackup/work/ directory at the destination. 29
    30. 30. 12/05/12 rsync examples (2)Copying to a remote machine rsync ­Pav /home/vuhung/work/001.svn/   vuhung@192.168.52.123:/home/work/002.ipad/ User vuhung will be prompted for a password.Copying from a remote machine through ssh rsync ­Pav ­e ssh  vuhung@192.168.52.123:/home/vuhung/work/  tmp/work/ User vuhung will be prompted for his ssh key password. 30
    31. 31. 12/05/12 mv and rm commandsmv <old_name> <new_name>  (move)R e n am e s th e g i ve n fi le o r d i re c to ry.mv ­i (interactive)I f th e n e w fi le alre ad y e x i ts , as k s fo r u s e r c o n fi rmrm file1 file2 file3 ... (remove)R e m o ve s th e g i ve n fi le s .rm ­i (i n te rac ti ve )Alw ays as k fo r u s e r c o n fi rm .rm ­r dir1 dir2 dir3 (re c u rs i ve )R e m o ve s th e g i ve n d i re c to ri e s w i th all th e i rc o n te n ts . 31
    32. 32. 12/05/12Create and delete directories mkdir dir1 dir2 dir3 ... (make dir) C re ate s d i re c to ri e s w i th th e g i ve n n am e s . rmdir dir1 dir2 dir3 ... (remove dir) R e m o ve s th e g i ve n d i re c to ri e s S afe : o n ly w o rk s w h e n d i re c to ri e s an d e m p ty. Alte rn ati ve : rm ­r (doesnt need empty directories). mkdir ­p /dir1/dir2/dir3 32
    33. 33. 12/05/12Displaying file contents S e ve ral w ays o f d i s p layi n g th e c o n te n ts o f fi le s .cat file1 file2 file3 ... (concatenate)C o n c ate n ate s an d o u tp u ts th e c o n te n ts o f th e g i ve nfi le s .more file1 file2 file3 ...Afte r e ac h p ag e , as k s th e u s e r to h i t a k e y toc o n ti n u e .C an als o ju m p to th e fi rs t o c c u rre n c e o f a k e yw o rd(/ c o m m an d ).less file1 file2 file3 ...D o e s m o re th an more w i th le s s .D o e s n t re ad th e w h o le fi le b e fo re s tarti n g .S u p p o rts b ac k w ard m o ve m e n t i n th e fi le (?c o m m an d ). 33
    34. 34. 12/05/12The head and tail commands head [­<n>] <file> D i s p lays th e fi rs t < n > li n e s (o r 1 0 b y d e fau lt) o f th e g i ve n fi le . D o e s n t h ave to o p e n th e w h o le fi le to d o th i s ! tail [­<n>] <file> D i s p lays th e las t < n > li n e s (o r 1 0 b y d e fau lt) o f th e g i ve n fi le . N o n e e d to lo ad th e w h o le fi le i n R AM ! Ve ry u s e fu l fo r h u g e fi le s . tail ­f <file> (follow) D i s p lays th e las t 1 0 li n e s o f th e g i ve n fi le an d c o n ti n u e s to d i s p lay n e w li n e s w h e n th e y are ap p e n d e d to th e fi le . Ve ry u s e fu l to fo llo w th e c h an g e s i n a lo g fi le , fo r e x am p le . E x am p le s head /etc/passwd tail ­f /var/log/httpd/acces_log 34
    35. 35. 12/05/12 The grep commandgrep <pattern> <files>S c an s th e g i ve n fi le s an d d i s p lays th e li n e s w h i c h m atc hth e g i ve n p atte rn .grep URGETN *.logD i s p lays all th e li n e s c o n tai n i n g error i n th e *.log fi le sgrep ­i URGENT *.logS am e , b u t c as e i n s e n s i ti vegrep ­ri URGENT .S am e , b u t re c u rs i ve ly i n all th e fi le s i n . an d i tss u b d i re c to ri e sgrep ­v INFO *.logO u tp u ts all th e li n e s i n th e fi le s e x c e p t th o s e c o n tai n i n ginfo. 35
    36. 36. 12/05/12 The sort commandsort <file>S o rts th e li n e s i n th e g i ve n fi le i n c h arac te ro rd e r an d o u tp u ts th e m .sort ­r <file>S am e , b u t i n re ve rs e o rd e r.sort ­ru <file>u: u n i q u e . S am e , b u t ju s t o u tp u ts i d e n ti c alli n e s o n c e .M o re p o s s i b i li ti e s d e s c ri b e d late r! 36
    37. 37. 12/05/12 Symbolic links A s ym b o li c li n k i s a s p e c i al fi le w h i c h i s ju s t a re fe re n c e to th e n am e o f an o th e r o n e (fi le o r d i re c to ry):U s e fu l to re d u c e d i s k u s ag e an dc o m p le x i ty w h e n 2 fi le s h ave th e s am ec o n te n t.E x am p le :/var/lo g /s e c u re  →/home/var.log.secureH o w to i d e n ti fy s ym b o li c li n k s :ls ­l d i s p lays ­> an d th e li n k e d fi le n am e . 37G N U ls d i s p lays li n k s w i th a d i ffe re n t c o lo r.
    38. 38. 12/05/12Creating symbolic linksT o c re ate a s ym b o li c li n k (s am e o rd e r as i n cp):ln ­s file_name link_nameT o c re ate a li n k w i th to a fi le i n an o th e rd i re c to ry, w i th th e s am e n am e :ln ­s ../README.txtT o c re ate m u lti p le li n k s at o n c e i n a g i ve nd i re c to ry:ln ­s file1 file2 file3 ... dirT o re m o ve a li n k :rm link_nameOf course, this doesnt remove the linked file! 38
    39. 39. 12/05/12 Hard linksT h e d e fau lt b e h avi o r fo r ln i s to c re ate h ardlin ksA h ard lin k to a fi le i s a re g u lar fi le w i th e x ac tlyth e s am e p h ys i c al c o n te n tsW h i le th e y s ti ll s ave s p ac e , h ard li n k s c an t b ed i s ti n g u i s h e d fro m th e o ri g i n al fi le s .I f yo u re m o ve th e o ri g i n al fi le , th e re i s n oi m p ac t o n th e h ard li n k c o n te n ts .T h e c o n te n ts are re m o ve d w h e n th e re are n om o re fi le s (h ard li n k s ) to th e m . 39
    40. 40. 12/05/12Files names and inodesM ak e s h ard an d s ym b o li c (s o ft) li n k s Userse as i e r to u n d e rs tan d ! File name interface Soft link File Hard link rm rm Inode Inode interface Filesystem 40
    41. 41. 12/05/12 File access rightsUse ls ­l to check file access rights 3 typ e s o f ac c e s s ri g h ts 3 typ e s o f ac c e s s le ve ls R e ad ac c e s s (r) U s e r (u): fo r th e o w n e r W ri te ac c e s s (w) o f th e fi le G ro u p (g): e ac h fi le als o E x e c u te ri g h ts (x) h as a “g ro u p ” attri b u te , c o rre s p o n d i n g to a g i ve n li s t o f u s e rs O th e rs (o): fo r all o th e r u s e rs 41
    42. 42. 12/05/12Access right constraints x w i th o u t r i s le g al b u t i s u s e le s s Yo u h ave to b e ab le to re ad a fi le to e x e c u te i t. B o th r an d x p e rm i s s i o n s n e e d e d fo r d i re c to ri e s : x to e n te r, r to li s t i ts c o n te n ts . Yo u c an t re n am e , re m o ve , c o p y fi le s i n a d i re c to ry i f yo u d o n t h ave w ac c e s s to th i s d i re c to ry. I f yo u h ave w ac c e s s to a d i re c to ry, yo u C AN re m o ve a fi le e ve n i f yo u d o n t h ave w ri te ac c e s s to th i s fi le (re m e m b e r th at a d i re c to ry i s ju s t a fi le d e s c ri b i n g a li s t o f fi le s ). T h i s e ve n le ts yo u m o d i fy (re m o ve + re c re ate ) a fi le e ve n w i th o u t w ac c e s s to i t. 42
    43. 43. 12/05/12Access rights examples­rw­r­­r­­R e ad ab le an d w ri tab le fo r fi le o w n e r, o n ly re ad ab lefo r o th e rs­rw­r­­­­­R e ad ab le an d w ri tab le fo r fi le o w n e r, o n ly re ad ab lefo r u s e rs b e lo n g i n g to th e fi le g ro u p .drwx­­­­­­D i re c to ry o n ly ac c e s s i b le b y i ts o w n e r­­­­­­­r­xF i le e x e c u tab le b y o th e rs b u t n e i th e r b y yo u rfri e n d s n o r b y yo u rs e lf. N i c e p ro te c ti o n s fo r atrap ... 43
    44. 44. 12/05/12 chmod: changing permissionschmod <permissions> <files>2 fo rm ats fo r p e rm i s s i o n s :O c tal fo rm at (ab c ):a,b,c = r*4+w*2+x (r, w, x: b o o le an s )E x am p le : chmod 644 <file>(rw fo r u, r fo r g an d o)O r s ym b o li c fo rm at. E as y to u n d e rs tan d b ye x am p le s :chmod go+r: ad d re ad p e rm i s s i o n s to g ro u p an do th e rs .chmod u­w: re m o ve w ri te p e rm i s s i o n s fro mu s e r.chmod a­x: (a: all) re m o ve e x e c u te p e rm i s s i o n 44fro m all.
    45. 45. 12/05/12 More chmod (1)chmod ­R a+rX folder/ M ak e s folder an d e ve ryth i n g i n i t avai lab le to e ve ryo n e !R: ap p ly c h an g e s re c u rs i ve lyX: x, b u t o n ly fo r d i re c to ri e s an d fi le salre ad y e x e c u tab leVe ry u s e fu l to o p e n re c u rs i ve ac c e s s tod i re c to ri e s , w i th o u t ad d i n g e x e c u ti o n ri g h tsto all fi le s . 45
    46. 46. 12/05/12 More chmod (2)chmod a+t /tmpt: (sticky). Special permission for directories, allowing only the directory and file owner to delete a file in a directory.Useful for directories with write access to anyone,like /tmp.Displayed by ls ­l with a t character. 46
    47. 47. 12/05/12The Unix and GNU / Linux command line Standard I/O, Redirections, Pipes 47
    48. 48. 12/05/12 Standard outputM o re ab o u t c o m m an d o u tp u tAll th e c o m m an d s o u tp u tti n g te x t o nyo u r te rm i n al d o i t b y w ri ti n g to th e i rstan d ard o u tp u t.S tan d ard o u tp u t c an b e w ri tte n(re d i re c te d ) to a fi le u s i n g th e >s ym b o lS tan d ard o u tp u t c an b e ap p e n d e d toan e x i s ti n g fi le u s i n g th e >> s ym b o l 48
    49. 49. 12/05/12 Standard output redirection examplesls ~vuhung/* > ~vuhung/vuhung­home­list.txtcat america.txt > the_earth.txtcat america.txt >> the_world.txtecho “README: No such file or directory” > READMEUseful way of creating a file without a text editor.Nice Unix joke too in this case. 49
    50. 50. 12/05/12 Standard inputM o re ab o u t c o m m an d i n p u tL o ts o f c o m m an d s , w h e n n o t g i ve n i n p u targ u m e n ts , c an tak e th e i r i n p u t fro m stan d ardin p u t. sort takes its input from the standard input: in this case,sort what you type in the terminalwindows (ended by [Ctrl][D])linux[Ctrl][D]linuxwindowssort < participants.txtT h e s tan d ard i n p u t o f s o rt i s tak e n fro m th e g i ve nfi le . 50
    51. 51. 12/05/12 PipesU n i x p i p e s are ve ry u s e fu l to re d i re c t th e s tan d ardo u tp u t o f a c o m m an d to th e s tan d ard i n p u t o fan o th e r o n e .E x am p le scat *.log | grep ­i error | sortgrep ­ri error . | grep ­v “ignored” | sort ­u  > serious_errors.logcat /home/*/homework.txt | grep grade | moreT h i s o n e o f th e m o s t p o w e rfu l fe atu re s i n U n i xs h e lls ! 51
    52. 52. 12/05/12 The tee commandtee [­a] fileT h e tee c o m m an d c an b e u s e d to s e n ds tan d ard o u tp u t to th e s c re e n an d to a fi les i m u ltan e o u s ly.make | tee build.logR u n s th e make c o m m an d an d s to re s i tso u tp u t to build.log.make install | tee ­a build.logR u n s th e make install c o m m an d an dap p e n d s i ts o u tp u t to build.log. 52
    53. 53. 12/05/12 Standard errorE rro r m e s s ag e s are u s u ally o u tp u t (i f th e p ro g rami s w e ll w ri tte n ) to stan d ard e rro r i n s te ad o fs tan d ard o u tp u t.S tan d ard e rro r c an b e re d i re c te d th ro u g h 2> o r 2>>E x am p le :cat f1 f2 nofile > newfile 2> error_fileNote: 1 is the descriptor for standard output, so 1> is equivalent to >.C an re d i re c t b o th s tan d ard o u tp u t an d s tan d arde rro r to th e s am e fi le u s i n g &> :cat f1 f2 nofile &> whole_file 53
    54. 54. 12/05/12 The yes commandU s e fu l to fi ll s tan d ard i n p u t w i th alw ays th e s am e s tri n g .yes <string> | <command>K e e p s fi lli n g th e s tan d ard i n p u t o f<command> w i th <string> (y b yd e fau lt).E x am p le syes | rm ­r dir/bank> yes no | credit_applicantyes "" | make oldconfig (equivalent to hitting [Enter] to accept all  54default settings)
    55. 55. 12/05/12 Special devices (1)D e vi c e fi le s w i th a s p e c i al b e h avi o r o r c o n te n ts/dev/nullT h e d ata s i n k ! D i s c ard s all d ata w ri tte n to th i s fi le .U s e fu l to g e t ri d o f u n w an te d o u tp u t, typ i c ally lo gi n fo rm ati o n :mplayer big_buck_bunny_1080p_surround.avi &> /dev/null/dev/zeroR e ad s fro m th i s fi le alw ays re tu rn 0 c h arac te rsU s e fu l to c re ate a fi le fi lle d w i th z e ro s :dd if=/dev/zero of=disk.img bs=1k count=2048S e e man null o r man zero fo r d e tai ls 55
    56. 56. 12/05/12 Special devices (2)/dev/randomR e tu rn s ran d o m b yte s w h e n re ad . M ai n ly u s e d b yc ryp to g rap h i c p ro g ram s . U s e s i n te rru p ts fro ms o m e d e vi c e d ri ve rs as s o u rc e s o f tru e ran d o m n e s s(“e n tro p y”).R e ad s c an b e b lo c k e d u n ti l e n o u g h e n tro p y i sg ath e re d ./dev/urandomF o r p ro g ram s fo r w h i c h p s e u d o ran d o m n u m b e rsare fi n e .Alw ays g e n e rate s ran d o m b yte s , e ve n i f n o t e n o u g he n tro p y i s avai lab le (i n w h i c h c as e i t i s p o s s i b le ,th o u g h s ti ll d i ffi c u lt, to p re d i c t fu tu re b ytes e q u e n c e s fro m p as t o n e s ). S e e man random fo r d e tai ls . 56
    57. 57. 12/05/12 Special devices (3)/dev/fullM i m i c s a fu ll d e vi c e .U s e fu l to c h e c k th at yo u r ap p li c ati o n p ro p e rlyh an d le sth i s k i n d o f s i tu ati o n . S e e man full fo r d e tai ls . 57
    58. 58. 12/05/12The Unix and GNU / Linux command line Task Control 58
    59. 59. 12/05/12Full control on tasksS i n c e th e b e g i n n i n g , U n i x s u p p o rtstru e p re e m p ti ve m u lti tas k i n g .Ab i li ty to ru n m an y tas k s i n p aralle l,an d ab o rt th e m e ve n i f th e y c o rru p tth e i r o w n s tate an d d ata.Ab i li ty to c h o o s e w h i c h p ro g ram s yo uru n .Ab i li ty to c h o o s e w h i c h i n p u t yo u rp ro g ram s tak e s , an d w h e re th e i ro u tp u t g o e s . 59
    60. 60. 12/05/12 Processes “E ve ryth i n g i n U n i x i s a fi le E ve ryth i n g i n U n i x th at i s n o t a fi le i s a p ro c e s s ” P ro c e s s e sI n s tan c e s o f a ru n n i n g p ro g ram sS e ve ral i n s tan c e s o f th e s am e p ro g ram c an ru n atth e s am e ti m eD ata as s o c i ate d to p ro c e s s e s :O p e n fi le s , allo c ate d m e m o ry, s tac k , p ro c e s s i d ,p are n t, p ri o ri ty, s tate ... 60
    61. 61. 12/05/12Running jobs in background S am e u s ag e th ro u g h o u t all th e s h e lls U s e fu l F o r c o m m an d li n e jo b s w h i c h o u tp u t c an b e e x am i n e d late r, e s p e c i ally fo r ti m e con su m in g on e s. T o s tart g rap h i c al ap p li c ati o n s fro m th e c o m m an d li n e an d th e n c o n ti n u e w i th th e m ou se . S tarti n g a tas k : ad d & at th e e n d o f yo u r li n e : find_prince_charming ­­cute ­­clever ­­rich  61
    62. 62. 12/05/12Background job controljobsR e tu rn s th e li s t o f b ac k g ro u n d jo b s fro m th e s am es h e ll [1]­  Running ~/bin/find_meaning_of_life ­­without­ god & [2]+  Running make mistakes &fgfg %<n>P u ts th e las t / n th b ac k g ro u n d jo b i n fo re g ro u n dm od eM o vi n g th e c u rre n t tas k i n b ac k g ro u n d m o d e :[Ctrl] Zbgkill %<n> 62Aborts the nth job.
    63. 63. 12/05/12 Job control example> jobs[1]­  Running ~/bin/find_meaning_of_life ­­without­god &[2]+  Running make mistakes &> fgmake mistakes> [Ctrl] Z[2]+  Stopped make mistakes> bg[2]+ make mistakes &> kill %1[1]+  Terminated ~/bin/find_meaning_of_life ­­without­god 63
    64. 64. 12/05/12 Listing all processes ... whatever shell, script or process they are started fromps ­uxLists all the processes belonging to the current userps ­aux (N o te : ps ­edf o n S ys te m V s ys te m s )L i s ts all th e p ro c e s s e s ru n n i n g o n th e s ys te mps ­aux | grep bart | grep bashUSER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMANDbart      3039  0.0  0.2  5916 1380 pts/2    S    14:35   0:00 /bin/bashbart      3134  0.0  0.2  5388 1380 pts/3    S    14:36   0:00 /bin/bashbart      3190  0.0  0.2  6368 1360 pts/4    S    14:37   0:00 /bin/bashbart      3416  0.0  0.0     0    0 pts/2    RW   15:07   0:00 [bash]PID:  Process idVSZ:  Virtual process size (code + data + stack)RSS:  Process resident size: number of KB currently in RAMTTY:  TerminalSTAT:  Status: R (Runnable), S (Sleep), W (paging), Z (Zombie)... 64
    65. 65. 12/05/12 Live process activitytop ­ D i s p lays m o s t i m p o rtan t p ro c e s s e s , s o rte db y c p u p e rc e n tag etop ­ 15:44:33 up  1:11,  5 users,  load average: 0.98, 0.61, 0.59Tasks:  81 total,   5 running,  76 sleeping,   0 stopped,   0 zombieCpu(s): 92.7% us,  5.3% sy,  0.0% ni,  0.0% id,  1.7% wa,  0.3% hi,  0.0% siMem:    515344k total,   512384k used,     2960k free,    20464k buffersSwap:  1044184k total,        0k used,  1044184k free,   277660k cached  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND3809 vuhung      25   0  6256 3932 1312 R 93.8  0.8   0:21.49 bunzip22769 root      16   0  157m  80m  90m R  2.7 16.0   5:21.01 Xh to p : I m p ro ve d to pYo u c an c h an g e th e s o rti n g o rd e r b y typ i n gM: M e m o ry u s ag e , P: % C P U , T: T i m e . 65Yo u c an k i ll a tas k b y typ i n g k an d th e p ro c e s s i d .
    66. 66. 12/05/12 Live process activityhtop ­ D i s p lays m o s t i m p o rtan t p ro c e s s e s , s o rte db y c p u p e rc e n tag etop ­ 15:44:33 up  1:11,  5 users,  load average: 0.98, 0.61, 0.59Tasks:  81 total,   5 running,  76 sleeping,   0 stopped,   0 zombieCpu(s): 92.7% us,  5.3% sy,  0.0% ni,  0.0% id,  1.7% wa,  0.3% hi,  0.0% siMem:    515344k total,   512384k used,     2960k free,    20464k buffersSwap:  1044184k total,        0k used,  1044184k free,   277660k cached  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND3809 jdoe      25   0  6256 3932 1312 R 93.8  0.8   0:21.49 bunzip22769 root      16   0  157m  80m  90m R  2.7 16.0   5:21.01 X3006 jdoe      15   0 30928  15m  27m S  0.3  3.0   0:22.40 kdeinit3008 jdoe      16   0  5624  892 4468 S  0.3  0.2   0:06.59 autorun3034 jdoe      15   0 26764  12m  24m S  0.3  2.5   0:12.68 kscd3810 jdoe      16   0  2892  916 1620 R  0.3  0.2   0:00.06 topYo u c an c h an g e th e s o rti n g o rd e r b y typ i n gM: M e m o ry u s ag e , P: % C P U , T: T i m e . 66Yo u c an k i ll a tas k b y typ i n g k an d th e p ro c e s s i d .
    67. 67. 12/05/12 Killing processes (1)kill <pids>S e n d s an ab o rt s i g n al to th e g i ve n p ro c e s s e s .L e ts p ro c e s s e s s ave d ata an d e x i t b yth e m s e lve s . S h o u ld b e u s e d fi rs t. E x am p le :kill 3039 3134 3190 3416kill ­9 <pids>S e n d s an i m m e d i ate te rm i n ati o n s i g n al. T h es ys te m i ts e lf te rm i n ate s th e p ro c e s s e s . U s e fu lw h e n a p ro c e s s i s re ally s tu c k (d o e s n t an s w e rto kill ­1).kill ­9 ­1K i lls all th e p ro c e s s e s o f th e c u rre n t u s e r. ­1:m e an s all p ro c e s s e s . 67
    68. 68. 12/05/12 Killing processes (2)killall [­<signal>] <command>K i lls all th e jo b s ru n n i n g <command>. E x am p le :killall bashxkillLets you kill a graphical application by clicking on it!Very quick! Convenient when you dont know the application command name. 68
    69. 69. 12/05/12Recovering from stuck graphicsI f yo u r g rap h i c al s e s s i o n i s s tu c k an d yo u c an n o lo n g e rtyp e i n yo u r te rm i n als , d o n t re b o o t!I t i s ve ry li k e ly th at yo u r s ys te m i s s ti ll fi n e . T ry toac c e s s a te x t c o n s o le b y p re s s i n g th e [Ctrl][Alt][F1]k e ys(o r [F2],[F3] fo r m o re te x t c o n s o le s )I n th e te x t c o n s o le , yo u c an try to k i ll th e g u i ltyap p li c ati o n .O n c e th i s i s d o n e , yo u c an g o b ac k to th e g rap h i cs e s s i o n b y p re s s i n g [Ctrl][Alt][F5] or [Ctrl][Alt][F7] (depending on your distribution)If you cant identify the stuck program, you can also kill all your processes: kill ­9 ­1You are then brought back to the login screen. 69
    70. 70. 12/05/12 Sequential commandsC an typ e th e n e x t c o m m an d i n yo u r te rm i n al e ve nw h e n th e c u rre n t o n e i s n o t o ve r.C an s e p arate c o m m an d s w i th th e ; s ym b o l:echo “I love thee”; sleep 10;  echo “ not”C o n d i ti o n als : u s e || (o r) o r && (an d ):more God || echo “Sorry, God doesnt exist”Runs echo only if the first command failsls ~sd6 && cat ~sd6/* > ~sydney/recipes.txtOnly cats the directory contents if the ls command succeeds (means read access). 70
    71. 71. 12/05/12 Quoting (1)D o u b le (") q u o te s c an b e u s e d to p re ve n t th e s h e llfro m i n te rp re ti n g s p ac e s as arg u m e n t s e p arato rs ,as w e ll as to p re ve n t fi le n am e p atte rn e x p an s i o n .> echo "Hello World" Hello World> echo "You are logged as $USER" You are logged as vuhung> echo *.log find_prince_charming.log  cosmetic_buys.log> echo "*.log" *.log 71
    72. 72. 12/05/12 Quoting (2)S i n g le q u o te s b ri n g a s i m i lar fu n c ti o n ali ty, b u tw h at i s b e tw e e n q u o te s i s n e ve r s u b s ti tu te d> echo You are logged as $USER You are logged as $USERBack quotes (`) can be used to call a command within another> cd /lib/modules/`uname ­r`; pwd /lib/modules/2.6.9­1.6_FC2Back quotes can be used within double quotes> echo "You are using Linux `uname ­r`" You are using Linux 2.6.9­1.6_FC2 72
    73. 73. 12/05/12Measuring elapsed timetime find_expensive_housing ­­near<...command output...>real    0m2.304s (actual elapsed time)user    0m0.449s (CPU time running program code)sys     0m0.106s (CPU time running system calls)real = user + sys + waitingwaiting = I/O waiting time + idle time (running  73other tasks)
    74. 74. 12/05/12Environment variablesS h e lls le t th e u s e r d e fi n e variab le s.T h e y c an b e re u s e d i n s h e ll c o m m an d s .C o n ve n ti o n : lo w e r c as e n am e s e x p o rt M YN AM E = vu h u n g Yo u c an als o d e fi n e e n viro n m e n t variab le s:vari ab le s th at are als o vi s i b le w i th i n s c ri p tso r e x e c u tab le s c alle d fro m th e s h e ll.C o n ve n tio n : u p p e r case n am e s.$setL i s ts all d e fi n e d e n vi ro n m e n t vari ab le s an dth e i r valu e . 74
    75. 75. 12/05/12Shell variables examples S h e ll vari ab le s (b as h ) projdir=/home/vuhung/coolstuff ls ­la $projdir; cd $projdir E n vi ro n m e n t vari ab le s (b as h ) cd $HOME export DEBUG=1 ./find_extraterrestrial_life (displays debug information if DEBUG is set) 75
    76. 76. 12/05/12Main standard environment variables Extremely widely used! MANPATH LD_LIBRARY_PATH M an u al p ag e s e arc h p ath S h are d li b rary s e arc h p ath PATH DISPLAY C o m m an d s e arc h p ath S c re e n i d to d i s p lay X PRINTER (g rap h i c al) ap p li c ati o n s o n . D e fau lt p ri n te r n am e EDITOR SHELL D e fau lt e d i to r (vi , e m ac s ...) C u rre n t s h e ll n am e HOME TERM C u rre n t u s e r h o m e C u rre n t te rm i n al typ e d i re c to ry USER HOSTNAME C u rre n t u s e r n am e N am e o f th e lo c al m ac h i n e 76
    77. 77. 12/05/12 PATH environment variablesPATHS p e c i fi e s th e s h e ll s e arc h o rd e r fo r c o m m an d s /home/vuhung/bin:/usr/local/bin:/usr/kerberos/ bin: /usr/bin:/bin:/usr/X11R6/bin:/bin:/usr/binLD_LIBRARY_PATHS p e c i fi e s th e s h are d li b rary (b i n ary c o d e li b rari e ss h are d b y ap p li c ati o n s , li k e th e C li b rary) s e arc h o rd e rfo r ld /usr/local/lib:/usr/lib:/lib:/usr/X11R6/libMANPATHS p e c i fi e s th e s e arc h o rd e r fo r m an u al p ag e s /usr/local/man:/usr/share/man 77
    78. 78. 12/05/12 PATH usage warning I t i s s tro n g ly re c o m m e n d e d n o t to h ave th e “.” d i re c to ry i n yo u r PATH e n vi ro n m e n t vari ab le , i n p arti c u lar n o t at th e b e g i n n i n g :A c rac k e r c o u ld p lac e a m ali c i o u s ls fi le i n yo u rd i re c to ri e s . I t w o u ld g e t e x e c u te d w h e n yo u ru n ls i nth i s d i re c to ry an d c o u ld d o n au g h ty th i n g s to yo u r d ata.I f yo u h ave an e x e c u tab le fi le c alle d test i n a d i re c to ry,th i s w i ll o ve rri d e th e d e fau lt test p ro g ram an d s o m es c ri p ts w i ll s to p w o rk i n g p ro p e rly.E ac h ti m e yo u cd to a n e w d i re c to ry, th e s h e ll w i ll w as teti m e u p d ati n g i ts li s t o f avai lab le c o m m an d s . C all yo u r lo c al c o m m an d s as fo llo w s : ./test 78
    79. 79. 12/05/12 Alias S h e lls le t yo u d e fi n e c o m m an d aliase s: s h o rtc u ts fo r c o m m an d s yo u u s e ve ry fre q u e n tly. E x am p le salias ls=ls ­laUseful to always run commands with default arguments.alias rm=rm ­iU s e fu l to m ak e rm alw ays as k fo r c o n fi rm ati o n .alias frd=find_rambaldi_device ­­asap ­­riskyUseful to replace very long and frequent commands.alias cia=. /home/vuhung/env/cia.shUseful to set an environment in a quick way(. is a shell command to execute the content of a shell script). 79
    80. 80. 12/05/12 The which commandB e fo re yo u ru n a c o m m an d , which te lls yo u w h e re i t i s fo u n dbash> which lsalias ls=ls ­­color=tty        /bin/lstcsh> which lsls:      aliased to ls ­­color=ttybash> which alias/usr/bin/which: no alias in (/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin)tcsh> which aliasalias: shell built­in command. 80
    81. 81. 12/05/12 ~/.bashrc file~/.bashrc, ~/.bash_profile, /etc/bashrcS h e ll s c ri p t re ad e ac h ti m e a bashs h e ll i s s tarte dYo u c an u s e th i s fi le to d e fi n eYo u r d e fau lt e n vi ro n m e n t vari ab le s (PATH,EDITOR...).Yo u r ali as e s .Yo u r p ro m p t (s e e th e bash m an u al fo r d e tai ls ).A g re e ti n g m e s s ag e . 81
    82. 82. 12/05/12 Command editingYo u c an u s e th e le ft an d ri g h t arro wk e ys to m o ve th e c u rs o r i n th e c u rre n tc o m m an d .Yo u c an u s e [Ctrl][a] to g o to th eb e g i n n i n g o f th e li n e , an d [Ctrl][e]to g o to th e e n d .Yo u c an u s e th e u p an d d o w n arro w sto s e le c t e arli e r c o m m an d s . 82
    83. 83. 12/05/12 Command history (1)historyD i s p lays th e late s t c o m m an d s th at yo u ran an dth e i r n u m b e r. Yo u c an c o p y an d p as te c o m m an ds tri n g s .Yo u c an re c all th e late s t c o m m an d :!!Yo u c an re c all a c o m m an d b y i ts n u m b e r!1003Yo u c an re c all th e late s t c o m m an d m atc h i n g as tarti n g s tri n g :!catCtrl­RSearch bash history backward 83
    84. 84. 12/05/12 Command history (2)Yo u c an m ak e s u b s ti tu ti o n s o n th elate s t c o m m an d :^more^lessYo u c an ru n an o th e r c o m m an d w i thth e s am e arg u m e n ts :more !* 84
    85. 85. 12/05/12The Unix and GNU / Linux command line Miscellaneous Text Editors 85
    86. 86. 12/05/12 Text editors G rap h i c al te x t e d i to rs F i n e fo r m o s t n e e d sn e d itE m ac s , X e m ac s T e x t-o n ly te x t e d i to rs O fte n n e e d e d fo r s ys ad m i n s an d g re at fo r p o w e r u s e rsvi (vi m )n an o 86
    87. 87. 12/05/12 The gedit text editorG n om e ge d itB e s t te x t e d i to r fo r n on vi or emacs expertsF e atu re h i g h li g h ts :Ve ry e as y te x t s e le c ti o n an d m o vi n gS yn tax h i g h li g h ti n g fo r m o s t lan g u ag e s an dfo rm ats . C an b e tai lo re d fo r yo u r o w n lo gfi le s , to h i g h li g h t p arti c u lar e rro rs an dw arn i n g s .E as y to c u s to m i z e th ro u g h m e n u sI n s talle d b y d e fau lt b y m o s t 87d i s tri b u ti o n s
    88. 88. 12/05/12gedit screenshot 88
    89. 89. 12/05/12 Emacs/XemacsE m ac s an d X e m ac s are p re ttys i m i lar (u p to yo u rp re fe re n c e )E x tre m e ly p o w e rfu l te x te d i to r fe atu re sG re at fo r p o w e r u s e rsL e s s e rg o n o mic than neditN o n s tan d ard s h o rtc u tsM u c h m o re th an a te x t e d i to r(g am e s , e -m ai l, s h e ll,b ro w s e r).S o m e p o w e r c o m m an d s h aveto b e le arn t. 89
    90. 90. 12/05/12 vi (vim)T e x t-m o d e te x t e d i to r avai lab le i n allU n i x s ys te m s . C re ate d b e fo rec o m p u te rs w i th m i c e ap p e are d .D i ffi c u lt to le arn fo r b e g i n n e rs u s e d tog rap h i c al te x t e d i to rs .Ve ry p ro d u c ti ve fo r p o w e r u s e rs .O fte n c an t b e re p lac e d to e d i t fi le s i ns ys te m ad m i n i s trati o n o r i n E m b e d d e dS ys te m s , w h e n yo u ju s t h ave a te x tc o n s o le . 90
    91. 91. 12/05/12 vim - vi improvedvi i m p le m e n tati o n n o w fo u n d i n m o s t G N U /L i n u x h o s t s ys te m sI m p le m e n ts lo ts o f fe atu re s avai lab le i nm o d e rn e d i to rs : s yn tax h i g h li g h ti n g ,c o m m an d h i s to ry, h e lp , u n li m i te d u n d o an dm u c h m u c h m o re .C o o l fe atu re e x am p le : c an d i re c tly o p e nc o m p re s s e d te x t fi le s .C o m e s w i th a G T K g rap h i c al i n te rfac e (gvim)U n fo rtu n ate ly, n o t fre e s o ftw are (b e c au s e o fa s m all re s tri c ti o n i n fre e d o m to m ak ec h an g e s ) 91
    92. 92. 12/05/12vi basic commands Though vi is extremely  powerful, its main 30  commands are easy to learn  and are sufficient for 99% of  everyones needs! You can also take the quick  tutorial by running  vimtutor. 92
    93. 93. 12/05/12 GNU nanoAn o th e r s m all te x t-o n ly, m o u s e fre e te x te d i to r.An e n h an c e d P i c o c lo n e (n o n fre e e d i to r i nPin e )F ri e n d ly an d e as i e r to le arn fo r b e g i n n e rsth an k s to o n s c re e n c o m m an d s u m m ari e s .Avai lab le i n b i n ary p ac k ag e s fo r s e ve ralp latfo rm s .An alte rn ati ve to vi i n e m b e d d e d s ys te m s .H o w e ve r, n o t avai lab le as a b u s yb o x b u i lt-in . 93
    94. 94. 12/05/12GNU nano screenshot 94
    95. 95. 12/05/12The Unix and GNU/Linux command line Miscellaneous Compressing and Archiving 95
    96. 96. 12/05/12 Measuring disc usageC au ti o n : d i ffe re n t fro m fi le s i z e !du ­h <file> (d i s k u s ag e )­h: re tu rn s s i z e o n d i s k o f th e g i ve n fi le , i nh u m an re ad ab le fo rm at: K (k i lo b yte s ), M(m e g ab yte s ) o r G (g i g ab yte s ), . W i th o u t ­h, dure tu rn s th e raw n u m b e r o f d i s k b lo c k s u s e d b yth e fi le (h ard to re ad ).N o te th at th e ­h o p ti o n o n ly e x i s ts i n G N U du.du ­sh <dir>­s: re tu rn s th e s u m o f d i s k u s ag e o f all th e fi le si n th e g i ve n d i re c to ry. 96
    97. 97. 12/05/12 Measuring disk spacedf ­h <dir>R e tu rn s d i s k u s ag e an d fre e s p ac e fo r th efi le s ys te m c o n tai n i n g th e g i ve n d i re c to ry.S i m i larly, th e ­h o p ti o n o n ly e x i s ts i n G N U df.Example:> df ­h .Filesystem            Size  Used Avail Use% Mounted on/dev/hda5             9.2G  7.1G  1.8G  81% /df ­hR e tu rn s d i s k s p ac e i n fo rm ati o n fo r allfi le s ys te m s avai lab le i n th e s ys te m . W h e ne rro rs h ap p e n , u s e fu l to lo o k fo r fu llfi le s ys te m s . 97
    98. 98. 12/05/12 Compressing Ve ry u s e fu l fo r s h ri n k i n g h u g e fi le s an d s avi n g s p ac eg[un]zip <file>G N U z i p c o m p re s s i o n u ti li ty. C re ate s .gz fi le s .O rd i n ary p e rfo rm an c e (s i m i lar to Z i p ).b[un]zip2 <file>M o re re c e n t an d e ffe c ti ve c o m p re s s i o n u ti li ty.C re ate s .bz2 fi le s . U s u ally 2 0-2 5 % b e tte r th an gzip.U s i n g 7 -z i pM u c h b e tte r c o m p re s s i o n rati o th an b z i p 2 (u p to 1 0to 2 0% ).S e e th e 7 -z i p p ag e fo r d e tai ls . 98
    99. 99. 12/05/12 Archiving (1)U s e fu l to b ac k u p o r re le as e a s e t o f fi le s w i th i n 1 fi letar: o ri g i n ally “tap e arc h i ve ”C re ati n g an arc h i ve :tar cvf <archive> <files or directories>c: c re atev: ve rb o s e . U s e fu l to fo llo w arc h i vi n g p ro g re s s .f: fi le . Arc h i ve c re ate d i n fi le (tap e u s e do th e rw i s e ).E x am p le :tar cvf /backup/home.tar /homebzip2 /backup/home.tar 99
    100. 100. 12/05/12 Archiving (2)Vi e w i n g th e c o n te n ts o f an arc h i ve o ri n te g ri ty c h e c k :tar tvf <archive>t: te s tE x trac ti n g all th e fi le s fro m an arc h i ve :tar xvf <archive>E x trac ti n g ju s t a fe w fi le s fro m an arc h i ve :tar xvf <archive> <files or directories>F i le s o r d i re c to ri e s are g i ve n w i th p ath sre lati ve to th e arc h i ve ro o t d i re c to ry. 100
    101. 101. 12/05/12Extra options in GNU tar tar = gtar = G N U tar o n G N U / L i n u x C an c o m p re s s an d u n c o m p re s s arc h i ve s o n th e fly. U s e fu l to avo i d c re ati n g h u g e i n te rm e d i ate fi le s M u c h s i m p le r to d o th an w i th tar an d bzip2!j o p ti o n : [u n ]c o m p re s s e s o n th e fly w i th bzip2z o p ti o n : [u n ]c o m p re s s e s o n th e fly w i th gzipE x am p le s tar cvjf work.tar.bz2 work/ tar cvzf work.tar.gz work/ tar xvzf work.tar.bz2; tar xvjf  101 work.tar.gz
    102. 102. 12/05/12 7-zip (1) h ttp ://w w w .7 -z i p .o rg / N o w th e b e s t s o lu ti o n fo r yo u r arc h i ve s !L ice n se : G N U L G PL7 -z i p c o m p re s s e s m u c h b e tte r th an b z i p 2 (u p to 1 0 o r2 0% )an d o f c o u rs e z i p (30 to 5 0 % ).B e n c h m ark c o m p re s s i n g K n o p p i x 5 .0.1 : -2 2 % (vs .b z i p 2 )!C au ti o n : 7 -z i p c an n o t re p lac e tar fo r arc h i vi n g o n U n i x .I t d o e s n t k e e p fi le o w n e r an d g ro u p i n fo rm ati o n ,b u t o f c o u rs e k e e p s fi le p e rm i s s i o n s .U s e i t to c o m p re s s tar arc h i ve s !I n vo k e : 7 z , 7 z a 102
    103. 103. 12/05/12 7-zip (2)7 -z i p s u p p o rts s tro n g AE S -2 5 6 e n c ryp ti o n .N o n e e d to e n c ryp t i n a s e p arate p as s .At las t a s o lu ti o n avai lab le fo r U n i x an d W i n d o w s !T h e to o l s u p p o rts m o s t o th e r c o m p re s s i o n fo rm ats :zip, c ab , arj, g z i p , b z i p 2 , tar, c p i o , rp m an d d e b . 103
    104. 104. 12/05/12 Using 7-zip Arc h i ve fi le s are u s u ally c re ate d w i th a .7z e x te n s i o nC re ati n g an arc h i ve : (a: ad d )7z a <archive> <files or directories>L i s t fi le s i n arc h i ve : (l: li s t)7z l <archive>E x trac ti n g fro m a 7 -z i p arc h i ve : (e: e x trac t)7z x <archive>Backup a directory (keeping owner and group information):tar cf ­ <dir> | 7z a ­si dir.tar.7z standard inputRestore this backup:7z x ­so dir.tar.7z | tar xf ­ standard output 104
    105. 105. 12/05/12Checking file integrityVery low cost solution to check file integritymd5sum FC3­i386­disk*.iso > MD5SUMComputes a MD5 (Message Digest Algorithm 5) 128 bit checksum of the given files. Usually redirected to a file.Example output:db8c7254beeb4f6b891d1ed3f689b412 FC3­i386­disc1.iso2c11674cf429fe570445afd9d5ff564e FC3­i386­disc2.isof88f6ab5947ca41f3cf31db04487279b FC3­i386­disc3.iso6331c00aa3e8c088cc365eeb7ef230ea FC3­i386­disc4.isomd5sum ­c MD5SUMChecks the integrity of the files in MD5SUM by comparing their actual 105MD5 checksum with their original one.
    106. 106. 12/05/12The Unix and GNU / Linux command line Miscellaneous Printing 106
    107. 107. 12/05/12 Unix printingM u lti -u s e r, m u lti -jo b , m u lti -c li e n t, m u lti -p ri n te rI n U n i x / L i n u x , p ri n ti n g c o m m an d s d o n t re allyp ri n t. T h e y s e n d jo b s to p ri n ti n g q u e u e s , p o s s i b lyo n th e lo c al m ac h i n e , o n n e tw o rk p ri n ti n g s e rve rso r o n n e tw o rk p ri n te rs .P ri n te r i n d e p e n d e n t s ys te m :P ri n t s e rve rs o n ly ac c e p t jo b s i n P o s tS c ri p to r te x t. P ri n te r d ri ve rs o n th e s e rve r tak ec are o f th e c o n ve rs i o n to e ac h p ri n te rs o w n fo rm at.R o b u s t s ys te m :R e b o o t a s ys te m , i t w i ll c o n ti n u e to p ri n t p e n d i n gjo b s . 107
    108. 108. 12/05/12 Printing commandsU s e fu l e n vi ro n m e n t vari ab le : PRINTERS e ts th e d e fau lt p ri n te r o n th e s ys te m . E x am p le :export PRINTER=lplpr [­P<queue>] <files>S e n d s th e g i ve n fi le s to th e s p e c i fi e d p ri n ti n g q u e u eT h e fi le s m u s t b e i n te x t o r P o s tS c ri p t fo rm at.O th e rw i s e , yo u o n ly p ri n t g arb ag e .a2ps [­P<queue>] <files>“An y to P o s tS c ri p t” c o n ve rts m an y fo rm ats toP o s tS c ri p t an d s e n d th e o u tp u t to th e s p e c i fi e dq u e u e . U s e fu l fe atu re s : s e ve ral p ag e s / s h e e t, p ag en u m b e ri n g , i n fo fram e ... 108
    109. 109. 12/05/12 Print job controllpq [­P<queue>]Lists all the print jobs in the given or default queue.lp is not readyRank    Owner   Job     File(s)                         Total Size1st     asloane 84      nsa_windows_backdoors.ps        60416 bytes2nd     amoore  85      gw_bush_iraq_mistakes.ps        65024000 bytescancel <job#> [<queue>]Removes the given job number from the default queue. 109
    110. 110. 12/05/12Using PostScript and PDF files Vi e w i n g a P o s tS c ri p t fi le P o s tS c ri p t vi e w e rs e x i s t, b u t th e i r q u ali ty i s p re tty p o o r. B e tte r c o n ve rt to P D F w i th ps2pdf: ps2pdf decss_algorithm.ps xpdf decss_algorithm.pdf & P ri n ti n g a P D F fi le Yo u d o n t n e e d to o p e n a P D F re ad e r! B e tte r c o n ve rt to P o s tS c ri p t w i th pdf2ps: pdf2ps rambaldi_artifacts_for_dummies.pdf lpr rambaldi_artifacts_for_dummies.ps 110
    111. 111. 12/05/12The Unix and GNU / Linux command line Miscellaneous Comparing Files and Directories 111
    112. 112. 12/05/12 Comparing files and directoriesdiff file1 file2R e p o rts th e d i ffe re n c e s b e tw e e n 2 fi le s , o rn o th i n g i f th e fi le s are i d e n ti c al.diff ­r dir1/ dir2/R e p o rts all th e d i ffe re n c e s b e tw e e n fi le s w i thth e s am e n am e i n th e 2 d i re c to ri e s .T o i n ve s ti g ate d i ffe re n c e s i n d e tai l, b e tte r u s eg rap h i c al to o ls !vi m d i ff 112
    113. 113. 12/05/12 vimdiffvimdiff file1 file2 113
    114. 114. 12/05/12meld 114
    115. 115. 12/05/12 kdiffAn o th e r n i c e to o l to c o m p are fi le s an d m e rg ed i ffe re n c e s 115
    116. 116. 12/05/12 gvimdiffAn o th e r n i c e to o l to vi e w d i ffe re n c e s i n fi le sAvailable in most distributions with gvimApparently not using diff.No issue with files with binary sections! 116
    117. 117. 12/05/12The Unix and GNU / Linux command line Miscellaneous Looking for Files 117
    118. 118. 12/05/12 The find commandB e tte r e x p lai n e d b y a fe w e x am p le s !find . ­name “*.pdf”L i s ts all th e *.pdf fi le s i n th e c u rre n t (.)d i re c to ry o r s u b d i re c to ri e s . Yo u n e e d th ed o u b le q u o te s to p re ve n t th e s h e ll fro me x p an d i n g th e * c h arac te r.find docs ­name "*.pdf" ­exec xpdf {} ;F i n d s all th e *.pdf fi le s i n th e docs d i re c to ryan d d i s p lays o n e afte r th e o th e r.M an y m o re p o s s i b i li ti e s avai lab le ! H o w e ve r,th e ab o ve 2 e x am p le s c o ve r m o s t n e e d s . 118
    119. 119. 12/05/12 The locate command M u c h fas te r re g u lar e x p re s s i o n s e arc h alte rn ati ve to findlocate keysLists all the files on your system with keys in their name.locate “*.pdf”L i s ts all th e *.pdf fi le s avai lab le o n th e w h o lem ac h i n elocate “/home/fridgevuhung/*eer*”Lists all the *beer* files in the given directory (absolutep ath )locate i s m u c h fas te r b e c au s e i t i n d e x e s all fi le s i na d e d i c ate d d atab as e , w h i c h i s u p d ate d o n are g u lar b as i s .updatedb 119
    120. 120. 12/05/12The Unix and GNU / Linux command line Miscellaneous Various Commands 120
    121. 121. 12/05/12Getting information about users who L i s ts all th e u s e rs lo g g e d o n th e s ys te m . whoami T e lls w h at u s e r I am lo g g e d as . groups T e lls w h i c h g ro u p s I b e lo n g to . groups <user> T e lls w h i c h g ro u p s <user> b e lo n g s to . finger <user> T e lls m o re d e tai ls (re al n am e , e tc ) ab o u t <user> D i s ab le d i n s o m e s ys te m s (s e c u ri ty re as o n s ). 121
    122. 122. 12/05/12 Changing usersYo u d o n o t h ave to lo g o u t to lo g o n an o th e r u s e r ac c o u n t!su vuhung(R are ) C h an g e to th e hyde ac c o u n t, b u tk e e p i n g th e e n vi ro n m e n t vari ab le s e tti n g so f th e o ri g i n al u s e r.su ­ vuhung(M o re fre q u e n t) L o g o n th e jekyll ac c o u n t,w i th e x ac tly th e s am e s e tti n g s as th i s n e wu s e r.su ­W h e n n o arg u m e n t i s g i ve n , i t m e an s th e 122root u s e r.
    123. 123. 12/05/12 The wget command I n s te ad o f d o w n lo ad i n g fi le s fro m yo u r b ro w s e r, ju s t c o p y an d p as te th e i r U R L an d d o w n lo ad th e m w i th wget! wget m ai n fe atu re sh ttp an d ftp s u p p o rtC an re s u m e i n te rru p te d d o w n lo ad sC an d o w n lo ad e n ti re s i te s o r at le as t c h e c k fo r b adli n k sVe ry u s e fu l i n s c ri p ts o r w h e n n o g rap h i c s areavai lab le(s ys te m ad m i n i s trati o n , e m b e d d e d s ys te m s )P ro x y s u p p o rt (http_proxy an d ftp_proxy e n v. 123(s e t) vari ab le s )
    124. 124. 12/05/12 wget exampleswget ­c  http://microsoft.com/customers/dogs/winxp4dogs.zipContinues an interrupted download.wget ­m https://www.facebook.com/nguyenvuhungMirrors a site.wget ­r ­np http://virror.hanoilug.org/Recursively downloads an on­line book for off­line access.­np: "no­parent". Only follows links in the current directory. 124
    125. 125. 12/05/12 Misc commands (1)sleep 60W ai ts fo r 60 s e c o n d s(d o e s n t c o n s u m e s ys te m re s o u rc e s ).wc report.txt (word count)438  2115 18302 report.txtC o u n ts th e n u m b e r o f li n e s , w o rd s an dc h arac te rs i n a fi le o r i n s tan d ard i n p u t. 125
    126. 126. 12/05/12 Misc commands (2)bc ("basic calculator?")bc is a handy but full­featured calculator. Even includes a programming language! Use the ­l option to have floating point support. python as a calculatordateR e tu rn s th e c u rre n t d ate . U s e fu l i n s c ri p tsto re c o rd w h e n c o m m an d s s tarte d o rc o m p le te d . 126
    127. 127. 12/05/12The Unix and GNU/Linux command line System Administration Basics 127
    128. 128. 12/05/12 File ownershipP arti c u larly u s e fu l i n (e m b e d d e d ) s ys te md e ve lo p m e n tw h e n yo u c re ate fi le s fo r an o th e r s ys te m .chown ­R vuhung /home/linux/src (­R: recursive)M ak e s u s e r vuhung th e n e w o w n e r o f all th e fi le s i n/home/linux/src. chgrp ­R core /home/vuhungM ak e s core th e n e w g ro u p o f e ve ryth i n g i n/home/vuhung.chown ­R vuhung:core /FileServer/Backup/chown c an b e u s e d to c h an g e th e o w n e r an d g ro u pat th e s am e ti m e . 128
    129. 129. 12/05/12 Shutting downshutdown ­h +5 (­h: halt)S h u ts th e s ys te m d o w n i n 5 m i n u te s .U s e rs g e t a w arn i n g i n th e i r c o n s o le s .shutdown ­h now (0)shutdown ­r now (­r: re b o o t)init 0Another way to shutdown(init is used internally by shutdown).init 6Another way to reboot.[Ctrl][Alt][Del]Also works on GNU/Linux (at least on PCs!). 129
    130. 130. 12/05/12 Network setup (1)/sbin/ifconfig ­a; echo $PATHP ri n ts d e tai ls ab o u t all th e n e tw o rk i n te rfac e savai lab le o n yo u r s ys te m .ifconfig eth0L i s ts d e tai ls ab o u t th e eth0 i n te rfac eifconfig eth0 192.168.52.100As s i g n s th e 192.168.52.100 I P ad d re s sto eth0 (1 I P ad d re s s p e r i n te rfac e ).ifconfig eth0 downS h u ts d o w n th e eth0 i n te rfac e(fre e s i ts I P ad d re s s ). 130
    131. 131. 12/05/12 Network setup (2)route add default gw 192.168.52.3S e ts th e d e fau lt ro u te fo r p ac k e ts o u ts i d e th elo c al n e tw o rk . T h e g ate w ay (h e re192.168.52.3) i s re s p o n s i b le fo r s e n d i n g th e mto th e n e x t g ate w ay, e tc ., u n ti l th e fi n ald e s ti n ati o n .routeL i s ts th e e x i s ti n g ro u te sroute del defaultroute del <IP>D e le te s th e g i ve n ro u teU s e fu l to re d e fi n e a n e w ro u te . 131
    132. 132. 12/05/12 Network testingping freshmeat.net (external)ping 192.168.52.16 (internal)ping 192.168.51.1 (semi­internal)T ri e s to s e n d p ac k e ts to th e g i ve n m ac h i n e an d g e tac k n o w le d g m e n t p ac k e ts i n re tu rn .PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.64 bytes from 192.168.51.1: icmp_seq=0 ttl=150 time=2.51 ms64 bytes from 192.168.51.1: icmp_seq=1 ttl=150 time=3.16 ms64 bytes from 192.168.51.1: icmp_seq=2 ttl=150 time=2.71 ms64 bytes from 192.168.51.1: icmp_seq=3 ttl=150 time=2.67 msW h e n yo u c an p i n g yo u r g ate w ay, yo u r n e tw o rki n te rfac e w o rk s fi n e . 132
    133. 133. 12/05/12Network setup summaryO n ly fo r s i m p le c as e s w i th 1 i n te rfac e , n o d h c p s e rve r...C o n n e c t to th e n e tw o rk (c ab le , w i re le s s c ard o rd e vi c e ...)I d e n ti fy yo u r n e tw o rk i n te rfac e :ifconfig ­aAs s i g n an I P ad d re s s to yo u r i n te rfac e(as s u m i n g eth0)ifconfig eth0 192.168.52.100 (e x am p le )Ad d a ro u te to yo u r g ate w ay (as s u m i n g192.168.52.3) fo r p ac k e ts o u ts i d e th e n e tw o rk :route add default gw 192.168.52.3 133
    134. 134. 12/05/12 Name resolutionYo u r p ro g ram s n e e d to k n o w w h at I P ad d re s sc o rre s p o n d s to a g i ve n h o s t n am e (s u c h askernel.org)D o m ai n N am e S e rve rs (D N S ) tak e c are o f th i s .Yo u ju s t h ave to s p e c i fy th e I P ad d re s s o f 1 o rm o re D N S s e rve rs i n yo u r /etc/resolv.conffi le :nameserver 192.168.52.3 (Internal DNS server)nameserver 8.8.8.8 (Google Open DNS)nameserver 8.8.4.4 (Google Open DNS)nameserver 203.162.4.190 (Viettel) 134
    135. 135. 12/05/12 Creating filesystems E x am p le smkfs.ext2 /dev/sda1 stF o rm ats yo u r U S B k e y (/dev/sda1: 1 p arti ti o n rawd ata) i n ext2 fo rm at.mkfs.ext2 ­F disk.imgF o rm ats a d i s k i m ag e fi le i n e x t2 fo rm atmkfs.vfat ­v ­F 32 /dev/sda1 (­v: ve rb o s e )F o rm ats yo u r U S B k e y b ac k to F AT 32 fo rm at.mkfs.vfat ­v ­F 32 disk.imgF o rm ats a d i s k i m ag e fi le i n F AT 32 fo rm at. B lan k d i s k i m ag e s c an b e c re ate d as i n th e b e lo w e x am p le : dd if=/dev/zero of=disk.img bs=1024 count=65536 135
    136. 136. 12/05/12 Mounting devices (1) raw dataT o m ak e fi le s ys te m s o n an y d e vi c e (file descriptors and file contents)(i n te rn al o r e x te rn al s to rag e )vi s i b le o n yo u r s ys te m , yo u h ave to m o u n t th e m .T h e fi rs t ti m e , c re ate a m o u n t p o i n t i n yo u r s ys temount m:mkdir /mnt/usbdisk (e x am p le )N o w , m o u n t i t:mount ­t vfat /dev/sda1 /mnt/usbdisk/dev/sda1: physical device­t: specifies the filesystem (format) type file(ext2, ext3, vfat, reiserfs, iso9660...) structure 136
    137. 137. 12/05/12 Mounting devices (2)L o ts o f mount o p ti o n s are avai lab le , i n p arti c u lar toc h o o s e p e rm i s s i o n s o r th e fi le o w n e r an d g ro u p ... S e eth e mount m an u al p ag e fo r d e tai ls .M o u n t o p ti o n s fo r e ac h d e vi c e c an b e s to re d i n th e/etc/fstab fi le .T h an k s to th i s fi le , yo u ju s t n e e d to s tate th e m o u n tp o i n t: # /etc/fstab: static file system information. # <file system> <mount point>   <type>  <options>        <dump> <pass> proc            /proc           proc    defaults         0       0 /dev/hda3       /               ext3    defaults,errors=remount­ ro 0       1 /dev/hda4       /home           ext3    defaults         0       2 /dev/hda2       /root2          ext3    defaults         0       2 Device list:  /dev/hda1       none            swap    sw               0       0 /proc/partitions /dev/hdc        /media/cdrom0   udf,iso9660 user,noauto      137 0       0
    138. 138. 12/05/12 Mounting devices (3) Yo u c an als o m o u n t a fi le s ys te m i m ag e s to re d i n a re g u lar fi le (lo o p d e vice s)U s e fu l to d e ve lo p fi le s ys te m s fo r an o th e r m ac h i n eU s e fu l to ac c e s s th e c o n te n ts o f an I S O c d ro mi m ag e w i th o u t h avi n g to b u rn i t.U s e fu l to c re ate a L i n u x p arti ti o n o n a h ard d i s kw i th o n ly W i n d o w s p arti ti o n s cp /dev/sda1 usbkey.img mount ­o loop ­t vfat usbkey.img  /mnt/usbdisk  138
    139. 139. 12/05/12Listing mounted filesystems Ju s t u s e th e mount c o m m an d w i th n o arg u m e n t: /dev/hda6 on / type ext3 (rw,noatime) none on /proc type proc (rw,noatime) none on /sys type sysfs (rw) none on /dev/pts type devpts  (rw,gid=5,mode=620) usbfs on /proc/bus/usb type usbfs (rw) /dev/hda4 on /data type ext3 (rw,noatime) none on /dev/shm type tmpfs (rw) /dev/hda1 on /win type vfat  (rw,uid=501,gid=501) none on /proc/sys/fs/binfmt_misc type  binfmt_misc (rw) O r d i s p lay th e /etc/mtab fi le (s am e re s u lt, u p d ate d b y mount an d umount e ac h 139 ti m e th e y are ru n )
    140. 140. 12/05/12 Unmounting devicesumount /mnt/usbdiskC o m m i ts all p e n d i n g w ri te s an d u n m o u n ts th e g i ve nd e vi c e , w h i c h c an th e n b e re m o ve d i n a s afe w ay.T o b e ab le to u n m o u n t a d e vi c e , yo u h ave to c lo s eall th e o p e n fi le s i n i t:C lo s e ap p li c ati o n s o p e n i n g d ata i n th e m o u n te dp arti ti o nM ak e s u re th at n o n e o f yo u r s h e lls h ave a w o rk i n gd i re c to ry i n th i s m o u n t p o i n t.Yo u c an ru n th e lsof c o m m an d (li s t o p e n fi le s ) tovi e w w h i c h p ro c e s s e s s ti ll h ave o p e n fi le s i n th em o u n te d p arti ti o n . 140
    141. 141. 12/05/12Beware of the dark side of root root u s e r p ri vi le g e s are o n ly n e e d e d fo r ve ry s p e c i fi c tas k s w i th s e c u ri ty ri s k s : m o u n ti n g , c re ati n g d e vi c e fi le s , lo ad i n g d ri ve rs , s tarti n g n e tw o rk i n g , c h an g i n g fi le o w n e rs h i p , p ac k ag e u p g rad e s ... E ve n i f yo u h ave th e root p as s w o rd , yo u r re g u lar ac c o u n t s h o u ld b e s u ffi c i e n t fo r 99.9 % o f yo u r tas k s (u n le s s yo u are a s ys te m ad m i n i s trato r). I n a trai n i n g s e s s i o n , i t i s ac c e p tab le to u s e root. I n re al li fe , yo u m ay n o t e ve n h ave ac c e s s to th i s ac c o u n t, o r p u t yo u r s ys te m s an d d ata at ri s k i f yo u d o. 141
    142. 142. 12/05/12Using the root account I n c as e yo u re ally w an t to u s e root...I f yo u h ave th e root p as s w o rd :su ­ (s w i tc h u s e r)I n m o d e rn d i s tri b u ti o n s , th e sudo c o m m an d g i ve s yo uac c e s s to s o m e root p ri vi le g e s w i th yo u r o w n u s e rp as s w o rd .E x am p le : sudo mount /dev/hda4 /homeD i s tri b u ti o n s li k e D e b i an o r U b u n tu b y d e fau lt d o n tallo w th e root u s e r to u s e g rap h i c al ap p li c ati o n s (m o rec o m p le x an d th u s m o re vu ln e rab le ).I f yo u re ally w an t to s tart a g rap h i c al ap p li c ati o n w i throot, typ e :As root: export DISPLAY=:0 (i .e . u s e th e fi rs t d i s p lay)As yo u r re g u lar u s e r: xhost + (i .e . allo w o th e r u s e rs ) 142
    143. 143. 12/05/12The Unix and GNU/Linux command line GNU/Linux: Distribution Packages 143
    144. 144. 12/05/12 How to find packagesU b u n tu , D e b i an ap t-c ac h e s e arc h m e ld h ttp ://w w w .d e b i an .o rg /d i s tri b /p ac k ag e s S e arc h b y p ac k ag e o r fi le n am erp m fi n d : h ttp ://rp m fi n d .n e t/L o ts o f R P M p ac k ag e s fo r R e d H at,M an d ri va, S u s e ...R e d h at vari an ts yu m s e arc h “k e yw o rd ” 144
    145. 145. 12/05/12 Identifying packagesW h at p ac k ag e d o e s a fi le b e lo n g to ?U s e fu l to g e t m o re i n fo rm ati o n , g e t th e c o d e ,fi n d n e w e r ve rs i o n s , re p o rt i s s u e s ...D i s tri b u ti o n w i th R P M p ac k ag e s :(R e d H at, F e d o ra, M an d ri va, S u s e ...) > rpm ­qf /bin/ls coreutils­5.2.1­7D e b i an , U b u n tu : > dpkg ­S /bin/ls fileutils: /bin/ls 145
    146. 146. 12/05/12Information about packages Ac c e s s p ac k ag e d e s c ri p ti o n , ve rs i o n n u m b e r, s o u rc e s , e tc . R P M b as e d d i s tri b u ti o n s : rpm ­qi <package­name> D e b i an : dpkg ­s <package­name> 146
    147. 147. 12/05/12The Unix and GNU/Linux command line Going Further 147
    148. 148. 12/05/12 Command helpS o m e U n i x c o m m an d s an d m o s t G N U / L i n u xc o m m an d s o ffe r at le as t o n e h e lp arg u m e n t:­h(­ i s m o s tly u s e d to i n tro d u c e 1 -c h arac te ro p ti o n s )­­help(­­ i s alw ays u s e d to i n tro d u c e th ec o rre s p o n d i n g “lo n g ” o p ti o n n am e , w h i c hm ak e s s c ri p ts e as i e r to u n d e rs tan d )Yo u als o o fte n g e t a s h o rt s u m m ary o f o p ti o n sw h e n yo u i n p u t an i n vali d arg u m e n t. 148
    149. 149. 12/05/12 Manual pagesman <keyword>D i s p lays o n e o r s e ve ral m an u al p ag e s fo r<keyword>man manM o s t avai lab le m an u al p ag e s are ab o u t U n i xc o m m an d s , b u t s o m e are als o ab o u t C fu n c ti o n s ,h e ad e rs o r d ata s tru c tu re s , o r e ve n ab o u t s ys te mc o n fi g u rati o n fi le s !man stdio.hman fstab (for /etc/fstab)M an u al p ag e fi le s are lo o k e d fo r i n th e d i re c to ri e ss p e c i fi e d b y th e MANPATH e n vi ro n m e n t vari ab le . 149
    150. 150. 12/05/12 Info pagesI n G N U , m an p ag e s are b e i n g re p lac e d b y i n fop ag e s . S o m e m an u al p ag e s e ve n te ll to re fe r toi n fo p ag e s i n s te ad .info <command>info fe atu re s :D o c u m e n tati o n s tru c tu re d i n s e c ti o n s(“n o d e s ”) an d s u b s e c ti o n s (“s u b n o d e s ”)P o s s i b i li ty to n avi g ate i n th i s s tru c tu re : to p ,n e x t, p re v, u pI n fo p ag e s g e n e rate d fro m th e s am e te x i n fos o u rc e as th e H T M L d o c u m e n tati o n p ag e s 150
    151. 151. 12/05/12Searching the Internet for resources (1) I n ve s ti g ati n g i s s u e s M o s t fo ru m s an d m ai li n g li s t arc h i ve s are p u b li c , an d are i n d e x e d o n a ve ry fre q u e n t b as i s b y G o o g le . I f yo u i n ve s ti g ate an e rro r m e s s ag e , c o p y i t ve rb ati m i n th e s e arc h fo rm , e n c lo s e d i n d o u b le q u o te s (“e rro r m e s s ag e ”). L o ts o f c h an c e s th at s o m e b o d y e ls e alre ad y fac e d th e s am e i s s u e . D o n t fo rg e t to u s e G o o g le G ro u p s : h ttp ://g ro u p s .g o o g le .c o m / T h i s s i te i n d e x e s m o re th an 2 0 ye ars o f n e w s g ro u p s m e s s ag e s . 151

    ×