Nguyễn Vũ Hưng The Unix and GNU/Linux command line - power tools
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. 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 a
L 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 al
h 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 to
au to m ati c s e arc h
U s e th u m b n ai ls to n avi g ate i n th e
d o c u m e n t i n a q u i c k w ay
2
3. 12/05/12
Command memento sheet
I 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 n
ju s t o n e s h e e t.
S u g g e s ti o n s fo r u s e
S 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 p
w 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 n
S to re aw ay fro m m i c e !
3
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 g
E ve ryth i n g i s a fi le
G N U / L i n u x fi le s ys te m s tru c tu re
C o m m an d li n e i n te rp re te rs
H an d li n g fi le s an d d i re c to ri e s
D i s p layi n g , s c an n i n g an d s o rti n g fi le s
S ym b o li c an d h ard li n k
F i le ac c e s s ri g h ts
4
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 s
S 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 s
P i p e s : re d i re c ti n g s tan d ard o u tp u t to
o th e r c o m m an d s
S tan d ard e rro r
5
6. 12/05/12
Training Contents (3)
T as k c o n tro l
F 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 g
L i s t o f ac ti ve tas k s
K i lli n g p ro c e s s e s
E n vi ro n m e n t vari ab le s
P AT H e n vi ro n m e n t vari ab le s
S h e ll ali as e s , .b as h rc fi le
6
7. 12/05/12
Training contents (4)
M i s c e llan e o u s
T e x t e d i to rs
C o m p re s s i n g an d arc h i vi n g
P ri n ti n g fi le s
C o m p ari n g fi le s an d d i re c to ri e s
L o o k i n g fo r fi le s
G e tti n g i n fo rm ati o n ab o u t u s e rs
7
8. 12/05/12
Training contents (5)
S ys te m ad m i n i s trati o n b as i c s
F i le o w n e rs h i p
S e tti n g u p n e tw o rk i n g
F 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 r
G e tti n g h e lp , ac c e s s i n g m an u al p ag e s
S 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. 12/05/12
Training contents (6)
B as i c L AM P Ad m i n i s trati o n
L in u x
Ap ac h e
M yS Q L
PH P
G o i n g fu rth e r
L AM P o n U b u n tu
L AM P o n C e n tO S (R e d h at)
9
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 an
i n tro d u c ti o n to F O S S : o p e rati n g
s ys te m s , ap p li c ati o n s , k e y p ro je c ts an d
ru 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
12. 12/05/12
Everything is a file
Alm 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. 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 x
C 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 t
C 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 t
i 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 Buglist
index.htm index.html index.html.old
13
14. 12/05/12
File paths
A 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 r
d i re c to ry at th e e n d , s e p arate d b y th e / c h arac te r
R e lati ve p ath : Documents/fun/microsoft_jokes.html
R e lati ve to th e c u rre n t d i re c to ry
Ab 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 n
fo 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. 12/05/12
GNU/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 e
s 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 x
i 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 n
fi 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. 12/05/12
GNU/Linux filesystem
structure (2)
/lost+found C o rru p t fi le s th e s ys te m tri e d to
re 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 e
s 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. 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 e
s 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 e
s 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 m
s 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 d
b 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
19. 12/05/12
Command 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 ll's 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. 12/05/12
Well known shells
M o s t fam o u s an d p o p u lar s h e lls
bash: 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 y
S 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 tax
tcsh: 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 d
fe 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 d
m o re ...)
20
21. 12/05/12
fish: 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 le
c 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 tax
h 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 X
c 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. 12/05/12
ls command
Lists 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. 12/05/12
File 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. 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 to
th e . d i re c to ry (s e e ls a). O n ly re fe re n c e to th e
p are n t d i re c to ry.
T yp i c al u s ag e :
cd ..
24
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 t
s 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 e
c 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 a
re 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 e
d i re c to ry o f th e vuhung u s e r.
25
26. 12/05/12
The 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. 12/05/12
The cp command
cp <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 ... dir
C o p i e s th e fi le s to th e targ e t d i re c to ry (las t
arg 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 le
alre ad y e x i s ts
cp 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. 12/05/12
Smart 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. 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 don't exist in the
source.
Caution: directory names should end with / . Otherwise, you get a
backup/work/ directory at the destination.
29
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. 12/05/12
mv and rm commands
mv <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 rm
rm 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 r
c o n te n ts .
31
32. 12/05/12
Create 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 (doesn't need empty directories).
mkdir p /dir1/dir2/dir3
32
33. 12/05/12
Displaying 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 n
fi 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 to
c 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. 12/05/12
The 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. 12/05/12
The grep command
grep <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 h
th e g i ve n p atte rn .
grep URGETN *.log
D 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 s
grep i URGENT *.log
S am e , b u t c as e i n s e n s i ti ve
grep 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 ts
s u b d i re c to ri e s
grep v INFO *.log
O 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 g
info.
35
36. 12/05/12
The sort command
sort <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 r
o 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 al
li 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. 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 d
c o m p le x i ty w h e n 2 fi le s h ave th e s am e
c o n te n t.
E x am p le :
/var/lo g /s e c u re →/home/var.log.secure
H 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 .
37
G 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. 12/05/12
Creating symbolic links
T 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_name
T o c re ate a li n k w i th to a fi le i n an o th e r
d i re c to ry, w i th th e s am e n am e :
ln s ../README.txt
T o c re ate m u lti p le li n k s at o n c e i n a g i ve n
d i re c to ry:
ln s file1 file2 file3 ... dir
T o re m o ve a li n k :
rm link_name
Of course, this doesn't remove the linked file!
38
39. 12/05/12
Hard links
T h e d e fau lt b e h avi o r fo r ln i s to c re ate h ard
lin ks
A h ard lin k to a fi le i s a re g u lar fi le w i th e x ac tly
th e s am e p h ys i c al c o n te n ts
W 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 e
d 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 o
i 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 o
m o re fi le s (h ard li n k s ) to th e m .
39
40. 12/05/12
Files names and inodes
M ak e s h ard an d s ym b o li c (s o ft) li n k s
Users
e 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. 12/05/12
File access rights
Use 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. 12/05/12
Access 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. 12/05/12
Access rights examples
rwrr
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 le
fo r o th e rs
rwr
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 le
fo 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
rx
F 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 r
fri 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 a
trap ...
43
44. 12/05/12
chmod: changing
permissions
chmod <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 y
e 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 d
o th e rs .
chmod uw: re m o ve w ri te p e rm i s s i o n s fro m
u s e r.
chmod ax: (a: all) re m o ve e x e c u te p e rm i s s i o n
44
fro m all.
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 ly
X: x, b u t o n ly fo r d i re c to ri e s an d fi le s
alre ad y e x e c u tab le
Ve ry u s e fu l to o p e n re c u rs i ve ac c e s s to
d 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 ts
to all fi le s .
45
46. 12/05/12
More chmod (2)
chmod a+t /tmp
t: (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
48. 12/05/12
Standard output
M o re ab o u t c o m m an d o u tp u t
All th e c o m m an d s o u tp u tti n g te x t o n
yo u r te rm i n al d o i t b y w ri ti n g to th e i r
stan 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 l
S tan d ard o u tp u t c an b e ap p e n d e d to
an e x i s ti n g fi le u s i n g th e >> s ym b o l
48
49. 12/05/12
Standard output
redirection examples
ls ~vuhung/* > ~vuhung/vuhunghomelist.txt
cat america.txt > the_earth.txt
cat america.txt >> the_world.txt
echo “README: No such file or directory” >
README
Useful way of creating a file without a text editor.
Nice Unix joke too in this case.
49
50. 12/05/12
Standard input
M o re ab o u t c o m m an d i n p u t
L 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 t
arg u m e n ts , c an tak e th e i r i n p u t fro m stan d ard
in p u t. sort takes its input from
the standard input: in this case,
sort
what you type in the terminal
windows
(ended by [Ctrl][D])
linux
[Ctrl][D]
linux
windows
sort < participants.txt
T 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 n
fi le .
50
51. 12/05/12
Pipes
U 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 ard
o 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 f
an o th e r o n e .
E x am p le s
cat *.log | grep i error | sort
grep ri error . | grep v “ignored” | sort u
> serious_errors.log
cat /home/*/homework.txt | grep grade | more
T 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 x
s h e lls !
51
52. 12/05/12
The tee command
tee [a] file
T h e tee c o m m an d c an b e u s e d to s e n d
s tan d ard o u tp u t to th e s c re e n an d to a fi le
s i m u ltan e o u s ly.
make | tee build.log
R u n s th e make c o m m an d an d s to re s i ts
o u tp u t to build.log.
make install | tee a build.log
R u n s th e make install c o m m an d an d
ap p e n d s i ts o u tp u t to build.log.
52
53. 12/05/12
Standard error
E 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 ram
i s w e ll w ri tte n ) to stan d ard e rro r i n s te ad o f
s 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_file
Note: 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 ard
e rro r to th e s am e fi le u s i n g &> :
cat f1 f2 nofile &> whole_file
53
54. 12/05/12
The yes command
U 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 y
d e fau lt).
E x am p le s
yes | rm r dir/
bank> yes no | credit_applicant
yes "" | make oldconfig
(equivalent to hitting [Enter] to accept all
54
default settings)
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/null
T 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 g
i n fo rm ati o n :
mplayer big_buck_bunny_1080p_surround.avi &>
/dev/null
/dev/zero
R e ad s fro m th i s fi le alw ays re tu rn 0 c h arac te rs
U 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=2048
S e e man null o r man zero fo r d e tai ls
55
56. 12/05/12
Special devices (2)
/dev/random
R 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 y
c ryp to g rap h i c p ro g ram s . U s e s i n te rru p ts fro m
s 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 s
g ath e re d .
/dev/urandom
F 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 rs
are 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 h
e 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 yte
s 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. 12/05/12
Special devices (3)
/dev/full
M 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 rly
h an d le s
th 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
59. 12/05/12
Full control on tasks
S i n c e th e b e g i n n i n g , U n i x s u p p o rts
tru 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 t
th 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 u
ru n .
Ab i li ty to c h o o s e w h i c h i n p u t yo u r
p ro g ram s tak e s , an d w h e re th e i r
o u tp u t g o e s .
59
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 s
I n s tan c e s o f a ru n n i n g p ro g ram s
S 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 at
th e s am e ti m e
D 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. 12/05/12
Running 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. 12/05/12
Background job control
jobs
R 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 e
s h e ll
[1] Running ~/bin/find_meaning_of_life without
god &
[2]+ Running make mistakes &
fg
fg %<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 d
m od e
M 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] Z
bg
kill %<n> 62
Aborts the nth job.
63. 12/05/12
Job control example
> jobs
[1] Running ~/bin/find_meaning_of_life without
god &
[2]+ Running make mistakes &
> fg
make mistakes
> [Ctrl] Z
[2]+ Stopped make mistakes
> bg
[2]+ make mistakes &
> kill %1
[1]+ Terminated ~/bin/find_meaning_of_life
withoutgod
63
64. 12/05/12
Listing all processes
... whatever shell, script or process they are started from
ps ux
Lists all the processes belonging to the current user
ps 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 m
ps aux | grep bart | grep bash
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
bart 3039 0.0 0.2 5916 1380 pts/2 S 14:35 0:00 /bin/bash
bart 3134 0.0 0.2 5388 1380 pts/3 S 14:36 0:00 /bin/bash
bart 3190 0.0 0.2 6368 1360 pts/4 S 14:37 0:00 /bin/bash
bart 3416 0.0 0.0 0 0 pts/2 RW 15:07 0:00 [bash]
PID: Process id
VSZ: Virtual process size (code + data + stack)
RSS: Process resident size: number of KB currently in RAM
TTY: Terminal
STAT: Status: R (Runnable), S (Sleep), W (paging), Z (Zombie)...
64
65. 12/05/12
Live process activity
top 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 d
b y c p u p e rc e n tag e
top 15:44:33 up 1:11, 5 users, load average: 0.98, 0.61, 0.59
Tasks: 81 total, 5 running, 76 sleeping, 0 stopped, 0 zombie
Cpu(s): 92.7% us, 5.3% sy, 0.0% ni, 0.0% id, 1.7% wa, 0.3% hi,
0.0% si
Mem: 515344k total, 512384k used, 2960k free, 20464k
buffers
Swap: 1044184k total, 0k used, 1044184k free, 277660k
cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3809 vuhung 25 0 6256 3932 1312 R 93.8 0.8 0:21.49 bunzip2
2769 root 16 0 157m 80m 90m R 2.7 16.0 5:21.01 X
h to p : I m p ro ve d to p
Yo u c an c h an g e th e s o rti n g o rd e r b y typ i n g
M: M e m o ry u s ag e , P: % C P U , T: T i m e .
65
Yo 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. 12/05/12
Live process activity
htop 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 d
b y c p u p e rc e n tag e
top 15:44:33 up 1:11, 5 users, load average: 0.98, 0.61, 0.59
Tasks: 81 total, 5 running, 76 sleeping, 0 stopped, 0 zombie
Cpu(s): 92.7% us, 5.3% sy, 0.0% ni, 0.0% id, 1.7% wa, 0.3% hi,
0.0% si
Mem: 515344k total, 512384k used, 2960k free, 20464k
buffers
Swap: 1044184k total, 0k used, 1044184k free, 277660k
cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3809 jdoe 25 0 6256 3932 1312 R 93.8 0.8 0:21.49 bunzip2
2769 root 16 0 157m 80m 90m R 2.7 16.0 5:21.01 X
3006 jdoe 15 0 30928 15m 27m S 0.3 3.0 0:22.40 kdeinit
3008 jdoe 16 0 5624 892 4468 S 0.3 0.2 0:06.59 autorun
3034 jdoe 15 0 26764 12m 24m S 0.3 2.5 0:12.68 kscd
3810 jdoe 16 0 2892 916 1620 R 0.3 0.2 0:00.06 top
Yo u c an c h an g e th e s o rti n g o rd e r b y typ i n g
M: M e m o ry u s ag e , P: % C P U , T: T i m e .
66
Yo 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. 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 y
th 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 3416
kill 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 e
s 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 l
w 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 r
to kill 1).
kill 9 1
K 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. 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 bash
xkill
Lets you kill a graphical application by clicking on it!
Very quick! Convenient when you don't know the
application command name.
68
69. 12/05/12
Recovering from stuck
graphics
I 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 r
typ 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 to
ac 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 lty
ap 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 c
s 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 can't identify the stuck program, you can also kill all your
processes: kill 9 1
You are then brought back to the login screen.
69
70. 12/05/12
Sequential commands
C 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 n
w 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 doesn't
exist”
Runs echo only if the first command fails
ls ~sd6 && cat ~sd6/* >
~sydney/recipes.txt
Only cats the directory contents if the ls command succeeds
(means read access).
70
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 ll
fro 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. 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 t
w 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 $USER
Back quotes (`) can be used to call a command within another
> cd /lib/modules/`uname r`; pwd
/lib/modules/2.6.91.6_FC2
Back quotes can be used within double quotes
> echo "You are using Linux `uname r`"
You are using Linux 2.6.91.6_FC2
72
74. 12/05/12
Environment variables
S 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 ts
o 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.
$set
L i s ts all d e fi n e d e n vi ro n m e n t vari ab le s an d
th e i r valu e .
74
75. 12/05/12
Shell 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. 12/05/12
Main 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. 12/05/12
PATH environment
variables
PATH
S 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/bin
LD_LIBRARY_PATH
S 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 s
s 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 r
fo r ld
/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib
MANPATH
S 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. 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 r
d 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 n
th 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 e
s 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 te
ti 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. 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 s
alias ls='ls la'
Useful to always run commands with default arguments.
alias rm='rm i'
U 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 risky'
Useful to replace very long and frequent commands.
alias cia='. /home/vuhung/env/cia.sh'
Useful to set an environment in a quick way
(. is a shell command to execute the content of a shell script).
79
80. 12/05/12
The which command
B 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 d
bash> which ls
alias ls='ls color=tty'
/bin/ls
tcsh> which ls
ls: aliased to ls color=tty
bash> which alias
/usr/bin/which: no alias in
(/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin
)
tcsh> which alias
alias: shell builtin command.
80
81. 12/05/12
~/.bashrc file
~/.bashrc, ~/.bash_profile,
/etc/bashrc
S h e ll s c ri p t re ad e ac h ti m e a bash
s h e ll i s s tarte d
Yo u c an u s e th i s fi le to d e fi n e
Yo 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. 12/05/12
Command editing
Yo u c an u s e th e le ft an d ri g h t arro w
k e ys to m o ve th e c u rs o r i n th e c u rre n t
c o m m an d .
Yo u c an u s e [Ctrl][a] to g o to th e
b 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 s
to s e le c t e arli e r c o m m an d s .
82
83. 12/05/12
Command history (1)
history
D i s p lays th e late s t c o m m an d s th at yo u ran an d
th 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 d
s 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
!1003
Yo u c an re c all th e late s t c o m m an d m atc h i n g a
s tarti n g s tri n g :
!cat
CtrlR
Search bash history backward 83
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 e
late s t c o m m an d :
^more^less
Yo u c an ru n an o th e r c o m m an d w i th
th e s am e arg u m e n ts :
more !*
84
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 s
n e d it
E 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 rs
vi (vi m )
n an o
86
87. 12/05/12
The gedit text editor
G n om e ge d it
B e s t te x t e d i to r fo r n on vi or emacs experts
F 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 g
S 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 d
fo rm ats . C an b e tai lo re d fo r yo u r o w n lo g
fi le s , to h i g h li g h t p arti c u lar e rro rs an d
w 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 s
I n s talle d b y d e fau lt b y m o s t
87
d i s tri b u ti o n s
89. 12/05/12
Emacs/Xemacs
E m ac s an d X e m ac s are p re tty
s i m i lar (u p to yo u r
p re fe re n c e )
E x tre m e ly p o w e rfu l te x t
e d i to r fe atu re s
G re at fo r p o w e r u s e rs
L e s s e rg o n o mic than nedit
N o n s tan d ard s h o rtc u ts
M 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 ave
to b e le arn t.
89
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 all
U n i x s ys te m s . C re ate d b e fo re
c 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 to
g 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 n
s ys te m ad m i n i s trati o n o r i n E m b e d d e d
S ys te m s , w h e n yo u ju s t h ave a te x t
c o n s o le .
90
91. 12/05/12
vim - vi improved
vi 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 s
I m p le m e n ts lo ts o f fe atu re s avai lab le i n
m 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 d
m 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 n
c 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 f
a s m all re s tri c ti o n i n fre e d o m to m ak e
c h an g e s )
91
92. 12/05/12
vi basic commands
Though vi is extremely
powerful, its main 30
commands are easy to learn
and are sufficient for 99% of
everyone's needs!
You can also take the quick
tutorial by running
vimtutor.
92
93. 12/05/12
GNU nano
An o th e r s m all te x t-o n ly, m o u s e fre e te x t
e 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 n
Pin 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 rs
th 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 ral
p 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
95. 12/05/12
The Unix and GNU/Linux
command line
Miscellaneous
Compressing and Archiving
95
96. 12/05/12
Measuring disc usage
C 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 n
h 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, du
re 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 y
th 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 s
i n th e g i ve n d i re c to ry.
96
97. 12/05/12
Measuring disk space
df 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 e
fi 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 h
R e tu rn s d i s k s p ac e i n fo rm ati o n fo r all
fi le s ys te m s avai lab le i n th e s ys te m . W h e n
e rro rs h ap p e n , u s e fu l to lo o k fo r fu ll
fi le s ys te m s . 97
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 e
g[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 p
M 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 0
to 2 0% ).
S e e th e 7 -z i p p ag e fo r d e tai ls .
98
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 le
tar: 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 ate
v: 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 d
o th e rw i s e ).
E x am p le :
tar cvf /backup/home.tar /home
bzip2 /backup/home.tar
99
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 r
i n te g ri ty c h e c k :
tar tvf <archive>
t: te s t
E 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 s
re lati ve to th e arc h i ve ro o t d i re c to ry.
100
101. 12/05/12
Extra 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 bzip2
z o p ti o n : [u n ]c o m p re s s e s o n th e fly w i th gzip
E 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. 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 PL
7 -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 r
2 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. 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. 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 n
C 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 input
Restore this backup:
7z x so dir.tar.7z | tar xf
standard output
104
107. 12/05/12
Unix printing
M 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 r
I 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 ally
p 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 ly
o 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 rs
o 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 t
o r te x t. P ri n te r d ri ve rs o n th e s e rve r tak e
c 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 g
jo b s .
107
108. 12/05/12
Printing commands
U s e fu l e n vi ro n m e n t vari ab le : PRINTER
S 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=lp
lpr [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 e
T 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 to
P 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 d
q 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 e
n u m b e ri n g , i n fo fram e ...
108
109. 12/05/12
Print job control
lpq [P<queue>]
Lists all the print jobs in the given or default queue.
lp is not ready
Rank Owner Job File(s) Total Size
1st asloane 84 nsa_windows_backdoors.ps 60416
bytes
2nd amoore 85 gw_bush_iraq_mistakes.ps 65024000
bytes
cancel <job#> [<queue>]
Removes the given job number from the default queue.
109
110. 12/05/12
Using 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. 12/05/12
The Unix and GNU / Linux
command line
Miscellaneous
Comparing Files and Directories
111
112. 12/05/12
Comparing files and
directories
diff file1 file2
R 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 r
n 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 th
th 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 e
g rap h i c al to o ls !
vi m d i ff
112
115. 12/05/12
kdiff
An 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 e
d i ffe re n c e s
115
116. 12/05/12
gvimdiff
An 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 s
Available in most
distributions with gvim
Apparently not using
diff.
No issue with files with
binary sections!
116
118. 12/05/12
The find command
B 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 e
d o u b le q u o te s to p re ve n t th e s h e ll fro m
e 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 ry
an 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. 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 find
locate keys
Lists 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 le
m ac h i n e
locate “/home/fridgevuhung/*
eer*”Lists all the *beer* files in the given directory (absolute
p 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 n
a 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 a
re g u lar b as i s .
updatedb 119
121. 12/05/12
Getting 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. 12/05/12
Changing users
Yo 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 t
k 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 s
o 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 w
u 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 122
root u s e r.
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 s
h ttp an d ftp s u p p o rt
C an re s u m e i n te rru p te d d o w n lo ad s
C 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 ad
li n k s
Ve 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 are
avai 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. 12/05/12
wget examples
wget c
http://microsoft.com/customers/dogs/winxp4do
gs.zip
Continues an interrupted download.
wget m
https://www.facebook.com/nguyenvuhung
Mirrors a site.
wget r np http://virror.hanoilug.org/
Recursively downloads an online book for offline access.
np: "noparent". Only follows links in the current directory.
124
125. 12/05/12
Misc commands (1)
sleep 60
W 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.txt
C o u n ts th e n u m b e r o f li n e s , w o rd s an d
c h arac te rs i n a fi le o r i n s tan d ard i n p u t.
125
126. 12/05/12
Misc commands (2)
bc ("basic calculator?")
bc is a handy but fullfeatured calculator. Even includes
a programming language! Use the l option to have
floating point support.
python as a calculator
date
R e tu rn s th e c u rre n t d ate . U s e fu l i n s c ri p ts
to re c o rd w h e n c o m m an d s s tarte d o r
c o m p le te d .
126
128. 12/05/12
File ownership
P arti c u larly u s e fu l i n (e m b e d d e d ) s ys te m
d e ve lo p m e n t
w 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/vuhung
M 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 p
at th e s am e ti m e .
128
129. 12/05/12
Shutting down
shutdown 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 0
Another way to shutdown
(init is used internally by shutdown).
init 6
Another way to reboot.
[Ctrl][Alt][Del]
Also works on GNU/Linux (at least on PCs!).
129
130. 12/05/12
Network setup (1)
/sbin/ifconfig a; echo $PATH
P ri n ts d e tai ls ab o u t all th e n e tw o rk i n te rfac e s
avai lab le o n yo u r s ys te m .
ifconfig eth0
L i s ts d e tai ls ab o u t th e eth0 i n te rfac e
ifconfig eth0 192.168.52.100
As s i g n s th e 192.168.52.100 I P ad d re s s
to eth0 (1 I P ad d re s s p e r i n te rfac e ).
ifconfig eth0 down
S 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. 12/05/12
Network setup (2)
route add default gw 192.168.52.3
S 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 e
lo c al n e tw o rk . T h e g ate w ay (h e re
192.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 m
to th e n e x t g ate w ay, e tc ., u n ti l th e fi n al
d e s ti n ati o n .
route
L i s ts th e e x i s ti n g ro u te s
route del default
route del <IP>
D e le te s th e g i ve n ro u te
U s e fu l to re d e fi n e a n e w ro u te .
131
132. 12/05/12
Network testing
ping freshmeat.net (external)
ping 192.168.52.16 (internal)
ping 192.168.51.1 (semiinternal)
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 t
ac 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
ms
64 bytes from 192.168.51.1: icmp_seq=1 ttl=150 time=3.16
ms
64 bytes from 192.168.51.1: icmp_seq=2 ttl=150 time=2.71
ms
64 bytes from 192.168.51.1: icmp_seq=3 ttl=150 time=2.67
ms
W 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 rk
i n te rfac e w o rk s fi n e . 132
133. 12/05/12
Network setup summary
O 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 r
d e vi c e ...)
I d e n ti fy yo u r n e tw o rk i n te rfac e :
ifconfig a
As 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 g
192.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. 12/05/12
Name resolution
Yo 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 s
c 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 as
kernel.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 r
m o re D N S s e rve rs i n yo u r /etc/resolv.conf
fi 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. 12/05/12
Creating filesystems
E x am p le s
mkfs.ext2 /dev/sda1
st
F o rm ats yo u r U S B k e y (/dev/sda1: 1 p arti ti o n raw
d ata) i n ext2 fo rm at.
mkfs.ext2 F disk.img
F o rm ats a d i s k i m ag e fi le i n e x t2 fo rm at
mkfs.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.img
F 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. 12/05/12
Mounting devices (1)
raw data
T 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. 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 to
c 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 e
th 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 t
p 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. 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 e
U 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 m
i 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 k
w 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. 12/05/12
Listing 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. 12/05/12
Unmounting devices
umount /mnt/usbdisk
C 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 n
d 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 e
all 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 d
p arti ti o n
M 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 g
d 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 ) to
vi 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 e
m o u n te d p arti ti o n .
140
141. 12/05/12
Beware 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. 12/05/12
Using 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 u
ac 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 r
p as s w o rd .
E x am p le : sudo mount /dev/hda4 /home
D 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 't
allo 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 re
c 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 th
root, 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
144. 12/05/12
How to find packages
U 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 e
rp 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. 12/05/12
Identifying packages
W 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
coreutils5.2.17
D e b i an , U b u n tu :
> dpkg S /bin/ls
fileutils: /bin/ls
145
146. 12/05/12
Information 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 <packagename>
D e b i an :
dpkg s <packagename>
146
148. 12/05/12
Command help
S 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 x
c 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 r
o p ti o n s )
help
( i s alw ays u s e d to i n tro d u c e th e
c 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 h
m 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 s
w h e n yo u i n p u t an i n vali d arg u m e n t.
148
149. 12/05/12
Manual pages
man <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 man
M o s t avai lab le m an u al p ag e s are ab o u t U n i x
c 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 m
c o n fi g u rati o n fi le s !
man stdio.h
man 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 s
s 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. 12/05/12
Info pages
I 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 fo
p ag e s . S o m e m an u al p ag e s e ve n te ll to re fe r to
i 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 p
I n fo p ag e s g e n e rate d fro m th e s am e te x i n fo
s 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. 12/05/12
Searching 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
152. 12/05/12
Searching the Internet for
resources (2)
L o o k i n g fo r d o c u m e n tati o n
L o o k fo r <tool> o r <tool> page to fi n d th e to o l o r
p ro je c t h o m e p ag e an d th e n fi n d th e late s t
d o c u m e n tati o n re s o u rc e s .
L o o k fo r <tool> documentation o r <tool> manual
i n yo u r favo ri te s e arc h e n g i n e .
L o o k i n g fo r g e n e ri c te c h n i c al
i n fo rm ati o n
W i k i P e d i a: h ttp ://w i k i p e d i a.o rg
L o ts o f u s e fu l d e fi n i ti o n s i n c o m p u te r s c i e n c e . A
re al e n c yc lo p e d i a! O p e n to an yo n e 's c o n tri b u ti o n s .
152
153. 12/05/12
Local Communities
Vi e tlu g
U b u n tu -vn
H an o i L u g
S ai g o n L u g
H ueLug
T h ai N g u ye n L U G
D an an g L u g ?
153
154. 12/05/12
Related documents
M o re th an 1 5 00 p ag e s u n d e r fre e li c e n s e s .
h ttp ://fre e -e le c tro n s .c o m /trai n i n g
L i n u x U S B d ri ve rs
I n tro d u c ti o n to U n i x an d G N U /L i n u x
R e al-ti m e i n e m b e d d e d L i n u x s ys te m s
E m b e d d e d L i n u x k e rn e l an d d ri ve r d e ve lo p m e n t
I n tro d u c ti o n to u C li n u x
F re e S o ftw are to o ls fo r e m b e d d e d L i n u x s ys te m s
L i n u x o n T I O M AP p ro c e s s o rs
Au d i o i n e m b e d d e d L i n u x s ys te m s
F re e S o ftw are d e ve lo p m e n t to o ls
M u lti m e d i a i n e m b e d d e d L i n u x s ys te m s
Java i n e m b e d d e d L i n u x s ys te m s
h ttp ://fre e -e le c tro n s .c o m /arti c le s I n tro d u c ti o n to G N U /L i n u x an d F re e S o ftw are
Ad van tag e s o f F re e S o ftw are i n e m b e d d e d is ysx an s e c o lo g y
L n u te m d
E m b e d d e d L i n u x o p ti m i z ati o n s W h at's n e w i n L i n u x 2 .6?
E m b e d d e d L i n u x fro m S c ratc h ... i n 40 m i n ! o w to p o rt L i n u x o n a n e w P D A
H
154
Notas del editor
No longer mentioned here [un]compress <file> Traditional Unix compression utility. Creates .Z files. Obsolete. Only kept for compatibility. Poor performance.