Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
C h a pter 1 . Accessi n g t h e C o m m a n d L i n e
-
Access i n g t h e Co m m a n d L i n e U s i n g t h e
-
D es kt o p
-
Objectives
Afte r com p l e t i n g t h i s sect i o n , students s h o u l d be a b l e to l o g i nto t h e L i n u x system u s i n g t h e -
-
The GNOME desktop environment
T h e desktop environment is t h e g ra p h ica l user i nterface on a L i n u x system. T h e defa u l t des ktop
e n v i ro n m e nt i n Red Hat Enterprise L i n u x 7 is provided by G N O M E 3. I t p rovides a n i ntegrated -
d e s ktop for users and a u n ified deve l o p m e nt p l a tform o n top of a g ra p h i c a l framework p rovided
by the X W i n d o w System.
-
-
The GN O M E desktop e nvi ronment
1/4
Pa rts of t h e G N O M E S h e l l
The v a r i o u s pa rts of t h e GN O M E S h e l l have s p e cific na mes a nd pu r poses. These pa rts inc l u d e t h e
fo l l ow i n g :
window list: The ba r that runs a lo n g t h e bottom of t h e screen. The w i ndow l i st p rovides a n easy
way to a ccess, m i ni m ize, a nd rest o re a l l w i nd ows in t h e c u rrent workspace. On t h e right corne r
is an i nd icator to tell t h e user w h i c h workspace t h ey are on a n d how m a ny a re ava i l a ble.
message tray: The messa g e t ra y p rovides a way to review notifications sent by a p p l icati ons or
system c o m p o ne nts to GN O M E. If a notification occ u rs, no r m a l ly the notification f i rst a p pears
briefly a s a s i n g l e l i ne at t h e bottom of the s c re e n , a nd a persistent i nd i cator a p pe a rs i n the
l ower right c o r n e r to i nform t h e user of how m a ny notifications have bee n recent l y received.
RH124- R H E L7 -e n-1-20140606 9
C h a pter 1 . Acces s i n g t h e C o m m a n d L i n e
Wo rks pa ces
Workspaces a re s e p a rate d es ktop screen s w h i c h have d ifferent a p p l icat i o n w i n d ows. T h e s e c a n
be u s e d to organize t h e w o r k i n g enviro n m ent by g ro u p i n g o p e n a p p l ic a t i o n w i ndows by t a s k .
For exa m p l e, w i n d ows b e i n g u s e d to perform a p a rt i c u l a r syste m m a i nt e n a n ce activity (such
a s setti n g u p a n e w r e m ote server) c a n b e g ro u p ed i n o n e workspa ce, while e m a i l and other
c o m m u n i ca t i o n a p pl i c a t i o n s can b e g ro u p ed in a n ot h e r workspace.
10 R H 1 24- R H E L7-en-1-20140606
-
Sta rt i n g a t e r m i n a l
-
-
An advantage of u s i n g the A c t i v i t i e s Ove rview is t h a t w i n d ows ca n be c l i c ked a n d d ra g g e d
betwe e n t h e c u rrent workspace a n d o n e o f t h e others by u s i n g t h e w o r k s p a c e s e l e c t o r o n t h e
r i g h t s i d e o f t h e screen a n d t h e w i n d ows overview i n t h e c e n t e r o f t h e screen.
-
Starting a terminal
-
To get a s h e l l pro m pt in G N O M E, start a g ra p h i c a l term i n a l a p p l ica t i o n s u c h as G N O ME Te r m i n a l.
T h e re a re severa l ways to do t h i s . H e re a re t h e t h re e most com m o n l y used methods:
-
• S e l e ct A p p l icat i o n s > Ut i l it i e s > Te r m i n a l.
-
W h e n a te r m i n a l w i ndow is o p e n e d , a s he l l p ro m pt d i s p l ays for t h e user t h a t started t h e g ra p h ic a l
term i n a l p ro g ra m . T h e s h e l l p ro m pt a n d t h e term i n a l w i n d ow ' s t it l e b a r w i l l i n d icate t h e c u rrent
user n a m e, host n a me, and wo r k i n g d i recto ry.
-
-
-
C h a pter 1 . Acces s i n g t h e C o m m a n d L i n e
References
GNOME Help
·yelp
-
-·
12 R H 1 24- R H E L7 - e n -1 -20140606 -
-
Guided exercise
-
Outco m e :
A basic orientation to t h e G N O M E 3 d esktop e n v i ro n m e nt.
-
Before you begin ...
Access the g ra p h i c a l l o g i n screen of d e s k t opX . example . com.
-
Important
- T h e re a re two v i rt u a l m a c h i n es ava i l a b l e for l a b exercises, a d e s ktop m a c h i n e
(g e n e rica l l y c a l l e d d e s k t o pX) a n d a s e rv e r (g e n e rica l l y ca l l ed s e rve rX).
D 1. L o g i n as s t u d e n t u s i n g t h e password s t u d e n t .
-
D 1 .2. C l i c k Sign I n o n ce the password has been typed i n .
- O n t h e e m pty desktop, press the Menu key o r rig ht-c l i c k with t h e mouse to open
t h e context m e n u .
- D 2 . 2 . S e l ect O p e n i n Term i n a l .
-
D 3. Log o u t .
- R H1 24- R H E L7 - en-1-20140606 13
-
-
C h a pter 1 . Access i n g t h e Co m m a n d L i n e
-
D 4.2. C l ic k Sign I n o nce t h e pa ssword h a s b e e n typed i n .
D 5. L o c k t h e scree n .
-
D 6. U n l o c k t h e scre e n . -
D 6.1 . P ress E n t e r to l i ft t h e l o c k s c r e e n c u rt a i n .
-
14 R H 1 24- R H E L 7 - e n -1-20140606
-
-
-
Exec ut i n g Co m m a n d s U s i n g t h e B a s h S h e l l
-
-
Objectives
After com p l et i n g t h i s section, students s h o u l d be a b l e to save t i m e r u n n i n g com m a n d s from a
s h e l l p ro m pt u s i n g B a s h s h o rtcuts.
-
-
Examples of simple commands
T h e d a t e c o m m a n d is u s e d to d i s p l ay t h e c u r re n t d a t e a n d t i m e. I t c a n a l so be u s e d by t h e
s u pe r u s e r t o set t h e system c l oc k . A n a rg u m e n t t h a t beg i n s w i t h a p l u s s i g n (+) specifies a
- format st r i n g for t h e date com m a n d .
08 : 13
[student@desktopX -]$ date +%x
-
04/05/2014
The pas swd com m a n d c h a n g e s a user's own pa ssword. The o r ig i n a l password for the account
-
m u st b e s pecified before a change w i l l be a l l owed. By defa u lt , pas swd i s config u red to req u i re a
strong pa ssword, c o n s i st i n g of l owe rcase l etters, u p p e rcase l etters, n u m bers, a n d sy m b o l s , a n d
-
i s n o t based o n a d i ct i o n a ry word. T h e s u p e r u s e r c a n u s e t h e pas swd c o m m a n d t o c h a n g e ot h e r
u s e rs' pa sswords.
[student@desktopX -]$
I
I file /etc/passwd
-
-
C h a pte r 1. Access i n g t h e C o m m a n d L i n e
lp : x : 4 : 7 : lp : /var/spool/lpd : /sbin/nologin
sync : x : 5 : 0 : sync : /sbin : /bin/sync
shutdown : x : 6 : 0 : shutdown : /sbin : /sbin/shutdown
halt : x : 7 : 0 : halt : /sbin : /sbin/halt
-
�-------"----�----'
[ student@desktopX -]$
-
39 /etc/passwd
wc -1 /etc/pas swd
[ student@desktopX -]$
843 /etc/group
wc -c /etc/group /etc/ho s t s -
227 /etc/hosts
1070 total -
-
Ta b com p l et i o n can be used to co m p l ete f i l e n a m e s w h e n ty p i n g t h e m as a rg u ments to
c o m m a n d s. W h e n Tab i s p ressed, i t w i l l com p l ete the f i l e n a m e a s m u c h a s i t can. P res s i n g Tab
a second t i m e c a u ses the s h e l l to l ist a l l of t h e f i l e s that a re m a t c h e d by the c u rrent patte r n .
-
16 R H 1 24- R H E L7-en-1-20140606 -
--
-
Co m m a n d h i story
-
[student@desktopX -]$
-
[student@desktopX -]$
ls /etc/pas<Tab>
passwd passwd -
ls /etc/passwd<Tab>
-
A rg u m e nts a n d o p t i o n s ca n be m a t c h e d w i t h t a b c o m p l e t i o n for m a n y co m m a n d s. T h e u s e r ad d
-
co m m a n d i s u s e d by t h e s u pe r u s e r, r oo t , to c reate a d d i t i o n a l users o n t h e syste m . I t h a s m a n y
o pt i o n s t h a t can b e u s e d t o control h o w t h a t c o m m a n d beh aves. Ta b c o m p l et i o n fo l l ow i n g a
pa rt i a l o p t i o n can be used to com p l ete t h e o p t i o n without a l ot of typ i n g .
. . . Output omitted . . .
23 clear
24 who
-
25 pwd
26 ls /etc
27 uptime
28 ls - 1
-
29 date
30 history
[student@desktopX -]$ ! ls
ls - 1
-
total e
drwxr-xr - x . 2 student student 6 Mar 29 21 : 16 Desktop
. . . Output omitted . . .
-
[student@desktopX -]$ ! 26
ls /etc
ab rt hosts pulse
adj time hosts . allow purple
-
- R H1 24- R H E L 7 - en -1 -20140606 17
-
-
C h a pte r 1 . Access i n g t h e C o m m a n d L i n e
-
S h o rtcut Desc r i p t i o n
-
Ct rl+a J u m p to the beg i n n i n g of the c o m m a n d l i ne.
Ct rl+e J u m p to the end of the com m a n d l i n e.
-
Ct rl+u C l e a r from t h e c u rs o r to t h e beg i n n i n g of the com m a n d l i ne.
Ct rl+k C l e a r from t h e c u rs o r to t h e e n d of t h e com m a n d l i n e .
-
Ct rl+Left A r row J u m p to t h e beg i n n i n g of t h e p rev ious word on t h e c o m m a n d l i ne.
Ct rl+Ri gh t Ar row J u m p to t h e beg i n n i n g of t h e next word on t h e com m a n d l i ne.
-
Ct rl+ r Searc h t h e h istory l i st o f co m m a n d s f o r a patte r n .
R References -
18 R H1 24- R H E L 7 - e n -1 -20140606 -
-
-
P ra ct i ce: B a s h Co m m a n d s a n d Keyboa rd
-
S h o rtcuts
-
Quiz
-
M a t c h t h e fo l l ow i n g B a s h s h o rtcuts to t h e i r d e s c r i p t i o n s i n t h e ta b l e.
I IOI I
-
l��I
-
Ct rl+k h i s t o ry
-
J u m p to t h e beg i n n i n g of t h e prev i o u s wo rd o n
-
t h e com m a n d l i n e.
- S e p a rate com m a n d s o n t h e s a m e l i n e.
-
C l e a r f ro m t h e c u rs o r to t h e e n d of t h e co m m a n d
l i ne.
-
Re-exe c u te a rec e n t co m m a n d by m a tc h i n g t h e
-
co m m a n d n a m e.
-
S h o rt c u t u s e d to com p l ete co m m a n d s , f i l e
n a m es , a n d o p t i o n s .
-
- R H1 24- R H E L 7-en-1-20140606 19
-
C h a pte r 1 . Acces s i n g t h e C o m m a n d L i n e
D es c r i pt i o n Shell command
D i s p l a y t h e l i st o f p rev i o u s c o m m a n d s .
C o p y t h e l a st a rg u m e n t of p rev i o u s c o m m a n d s .
Solution
-
Solution
-
Match t h e fo l l ow i n g B a s h s h o rtcuts to t h e i r d e s c r i pt i o n s i n t h e ta b l e.
D escr i p ti o n S h e l l com m a n d
- S e p a rate co m m a n d s o n t h e s a m e l i n e. .
I
- C l e a r f r o m t h e c u rs o r to t h e e n d of t h e co m m a n d Ct rl+k
l i ne.
-
-
S h o rt c u t u s e d to com p l ete c o m m a n d s, f i l e Tab
n a m es , a n d o p t i o n s.
-
J u m p to t h e b eg i n n i n g for t h e co m m a n d l i n e. Ct rl+a
-
-
D i s p l ay t h e l i st of p rev i o u s co m m a n d s . h is t o ry
- C o py t h e l a st a rg u m e n t of p rev i o u s co m m a n d s. Esc+ .
- R H1 24- R H E L7-en-1-20140606 21
-
-
C h a pte r 1 . Accessi n g t h e C o m m a n d L i n e
-
La b: Access i n g t h e Co m m a n d L i n e
-
Outcomes:
-
• Practice u s i n g s he l l com m a n d line e d i t i n g and h i story fu n c t i o n s to effi c i e n t l y execute
co m m a n d s with m i n o r c h a n g es.
4. D i s p l ay t h e c u rre nt t i m e a n d date. -
10. Repeat the p rev i o u s com m a n d , b u t u s e the - n 20 opt i o n to d i s p l ay the l a st 20 l i nes in the
f i l e. U s e co m m a n d l i n e e d i t i n g to accom p l i s h this w i t h a m i n i m a l a m o u nt of keyst rokes.
-
11. Execute the date c o m m a n d w i t h o u t any a rg u m e nts to d i s p l ay the c u r re n t date and t i me.
22 R H 1 24- R H E L 7 - e n -1 -20140606 -
-
-
Sol ution
-
Solution
-
O utcomes:
- • Practice using s h e l l c o m m a n d l i n e editing and h istory f u n c t i o n s to effic i e n t l y execute
co m m a n d s w i t h m i n o r c h a n g es.
[student@desktopX - ] $ passwd
Changing password for user student .
Changing password for student .
-
- - --------- -----�
-
4. D i s p l ay t h e current t i m e a n d date.
[student@desktopX - ] $ date
Thu Apr 3 10 : 13 : 04 PDT 2014
�----� ---�-- ----- - ·--- �--� ----�------ ----��-
[ student@desktopX
10 : 14 : 07 AM
- ] $ d a t e +%r
,_.
- R H124- R H E L7 -e n -1-20140606 23
-
-
C h a pter 1 . Accessi n g t h e C o m m a n d L i n e
-
.I
Use t h e file co m m a n d to dete r m i n e i t s f i l e type.
[student@desktopX - ] $ w c <Esc> .
[student@desktopX - ] $ wc / u s r/bin/clean - binary - files
-
The head com m a n d d i s p l ays the beg i n n i n g of the f i l e. Did you use t h e bash s h o rtcut a g a i n? -
#
# Script to clean binary files .
#
# JPackage Proj ect <http : //www . j package . org/>
-
#
# $Id : clean-binary-files, v 1 . 1 2006/09/19 19 : 39 : 37 fnasser Exp $
U s e t h e t ail com m a n d .
[ student@desktopX - ] $
[student@desktopX - ] $
tail <Esc> . -
. . Output omitted . . .
tail / u s r/bin/clean - binary - files
.
[student@desktopX - ] $
. . . Output omitted . . .
t ail - n 20 / u s r/bin/clean - binary - files
-
I [ student@desktopX - ] $ date
-
24 R H1 24- R H E L 7-en-1-20140606 -
-
-
i
-
12. U s e b as h h istory t o d i s p l ay j u st t h e t i m e.
[ student@desktopX ]$
date +%X
! 44
10 : 49 : 56 AM
- -
-
U s e e i t h e r exit o r t h e Ct r l +d k e y c o m b i n at i o n t o c l ose t h e s h e l l .
I [ student@desktopX ]$ - exit
-
-
-
C h a pter 1 . Access i n g t h e C o m m a n d L i n e
-
S u m m a ry
-
Exec u t i n g C o m m a n d s U s i n g t h e B a s h S h e l l -
26 R H1 24- R H E L 7-en-1-20140606 -
-
red h at ®
®
TRAINING
C H A PT E R 2
Overview
C h a pter 2. M a n a g i n g F i l es Fro m t h e C o m m a n d L i n e
-
T h e L i n u x F i l e Syste m H i e ra rc hy -
Objectives -
After com p l e t i n g t h i s sect i o n , st u d e n t s s h o u l d be a b l e to u n d e rsta n d f u n d a m e ntal f i l e system
l ayout, o rg a n i za t i o n , and the l ocat i o n of key f i l e types.
-
• -
I
-
• • • • • • • • • • •
bin
........ b o ot dev etc home root ru n sbin tmp usr va r
-
I I . . ..
•• . . . �
·.
....
·... . .
I , .. .... I
• • •
..
. tmp -
' ,......._ ,
Note
� -
28 R H 1 24- R H E L 7 - e n -1-20140606 -
-
-
T h e f i l e system h i e ra rc h y
-
Locat i o n Pu r p o se
-
/usr I n sta l l ed softwa re, s h a red l i b ra ries, i n c l u d e fi l es, a n d static rea d -o n l y pro g ra m
data. I m portant s u b d i rectories i n c l u d e :
- - /u s r /bin: User commands.
- / u s r/sbin: System administration commands.
- / u s r /local: L ocally customized software.
-
/etc Config u ration f i l e s s p e c i f i c to t h i s syste m .
/va r Va r i a b l e data s p e c i f i c to t h i s system that s h o u l d p e rs ist betwee n boots. F i l e s
- that d y n a m i ca l l y c h a n g e (e.g. databases, ca c h e d i rectories, log f i l es, p r i nter-
s p o o l e d doc u m e nts, and webs ite content) may be fo u n d u n d e r /va r.
/run R u nt i m e data f o r p rocesses sta rted s i n c e t h e last boot. T h i s i n c l udes process
ID files a n d lock f i l es, a m o n g ot h e r t h i ngs. The contents of this d i rectory a re
recreated o n reboot. ( T h i s d i rectory c o n s o l idates /va r / r u n a n d /va r /loc k
from o l d e r vers i o n s of Red H a t Enterprise L i n ux.)
-
-
Important
I n Red H a t Enterprise L i n u x 7, f o u r o l d e r d i rectories i n I n ow have i d e ntica l contents a s
- t h e i r cou nterparts l ocated i n / u s r :
- / b i n a n d / u s r /bin.
- /sbin and / u s r /s bin.
- - /lib a n d / u s r/lib.
- /lib64 and / u s r /lib64.
-
-
C h a pter 2. M a n a g i n g F i l e s Fro m t h e C o m m a n d L i n e
-
.- _
References
I
,!, Ri -
t!i__I
hie r (7 ) m a n page
30 R H1 24- R H E L7-en-1-20140606 -
-
-
P ra ct i ce: F i l e Syste m H i e ra rc hy
-
-
Quiz
DBEJ BGB B
-
I I B
/u s r/b i n /u s r/s b i n
D i rectory p u r p o s e L o ca t i o n
-
T h i s d i rectory c o n t a i n s d y n a m i c c o n f i g u ra t i o n d a ta ,
-
s u c h a s F T P a n d w e b s i tes.
-
Reg u l a r user c o m m a n d s a n d u t i l i t i e s a re l ocated h e re.
- R H124- R H E L7-en-1-20140606 31
-
-
C h a pter 2. M a n a g i n g F i l es From t h e C o m m a n d L i n e
-
C o n ta i n s d y n a m i c , n o n - pe rs i st e n t a p p l i c a t i o n r u n t i m e -
data.
-
32 R H 1 24- R H E L7 -e n -1 -20140606 -
-
-
Solution
-
Solution
-
-
U s e r h o m e d i recto ries a re l o cated u n d e r t h i s d i recto ry. /home
-
T h i s i s t h e root acco u nt ' s h o m e d i recto ry. /root
-
C o n t a i n s d y n a m i c , n o n - p e rs i stent a p p l icat i o n r u n t i m e /ru n
data.
-
- R H1 24- R H E L 7-en-1-20140606 33
-
C h a pter 2. M a n a g i n g F i les Fro m the C o m m a n d L i n e
Locat i n g F i l es by N a m e
O bject ives
After c o m p l e t i n g t h i s secti o n , stu d e n t s s h o u l d b e a b le t o correct l y u s e a b s o l ute path n a mes,
c h a n g e a work i n g d i rectory, a n d use c o m m a n d s to d ete r m i n e d i rectory l o c a t i o n s and contents.
A b so l u t e pat h s a n d re l a t i ve pat h s
I
I
home
home var
al e
ic bob eve log
I I
.�·
· log -- 1mesfages
...,
.
meslagel
l '"I
Figure 2.2: The common file browser view (left) is equivalent to the top-down view (right)
I m p o rta nt
A l t h o u g h a Space is a n accepta b l e c h a racter i n L i n u x file n a mes, a s p a c e is t h e
d e l i miter used b y t h e comma n d s h e l l f o r c o m m a n d syntax interp retat i o n . New
a d m i n i strators a re a d vi sed to avo i d using s pa ces i n file n a mes, since file n a mes that
i n c l u d e s p a ces frequently res u l t i n u n de s i re d command execution behavior.
Absol ute p a t h s
A n absolute path i s a fully qualified n a me, beg i n n i n g at the root (!) d i rectory a n d specifying each
s u b d i rectory t raversed to reach a n d u n i q u ely re p resent a s i n g l e f i l e. Every f i l e i n a file system has
a u n i q u e a bsol ute path n a me, recog n ized w i t h a simple r u l e: A path name w i t h a fo rward s l a s h
( / ) a s t h e fi rst c h a ra cter is a n a bs o l ute p a t h n a me. For exa m p le, t h e a b s o l ute p a t h n a m e f o r t h e
system m e s s a g e l og f i l e i s /var /log/messages. Absol ute p a t h n a m es c a n b e long to type, s o
files may a l so b e l ocated relatively.
34 R H1 24- R H E L7-en-1-20140606
-
N a v i g a t i n g paths
-
R e l at i v e pat h s
L i ke a n a bsol ute p a t h , a relative path identifies a u n iq u e f i l e , s pecify i n g o n l y t h e p a t h n e cessa ry
-
to rea c h the f i l e from the wo r k i n g d i rectory. Recog n i z i n g re l a t ive path n a m e s fo l l ows a s i m p l e
r u l e: A p a t h n a m e w i t h anything other than a forwa rd s l a s h ( / ) as a fi rst c h a racter is a relat ive
path n a m e. A user i n the /var d i rectory co u l d refe r to the message log f i l e re l a t i ve l y a s
-
log/messages.
Navigating paths
The pwd com m a n d d i s p l ays t h e fu l l path n a m e of t h e c u rrent l ocati o n , w h i c h h e l ps d eterm i n e
-
a p p rop riate syntax f o r rea c h i n g f i l e s u s i n g re l a t ive p a t h n a m es. T h e ls com m a n d l ists d i rectory
contents for t h e s peci fi ed d i rectory o r, if n o d i rectory i s g iv e n , for t h e c u rrent d i recto ry.
-
[student@desktopX -]$
rI /home/student pwd
[student@desktopX -]$ l
Desktop Documents Downloads Music Pictures Public Templates Videos
s
[student@desktopX -]$
-
/home/student
- [student@desktopX -]$
T h e s h e l l p rog ra m p ro m pt d i s p l ays, for brevity, o n l y t h e l a st component o f t h e c u r re n t d i rectory
-
path. For / home/ s t u d e n t /Video s, only Videos d i s p l ays. At any t i me, ret u r n to the u s e r ' s
h o m e d i rectory u s i n g c d without specify i n g a d e st i n a t i o n . T h e pro m pt d i s p l ays t h e tilde (-)
c h a racter when the u s e r ' s c u rrent d i rectory i s t h e i r h o m e d i rectory.
-
-
C h a pter 2. M a n a g i ng F i l es Fro m t h e C o m m a n d L i n e
-
[student@desktopX
-
[student@desktopX
-]$ touch Videos/blockbus t e r l . ogg
[student@desktopX
-]$ touch Videos/blockb u s t e r 2 . ogg
[ student@desktopX
-]$ touch Documents/t hesis_ch ap t e r l . odf
[student@desktopX
-
-]$ touch Docume n t s / t hesis_chapt e r 2 . odf
-]$
-·
T h e l s co m m a n d has m u lt i p l e o pt i o n s for d i s p l a y i n g att r i b utes o n f i l es. T h e m ost co m m o n a n d
usefu l a re - 1 ( l o n g l is t i n g format), - a (a l l f i l es, i n c l ud e s hidden f i l es), a n d - R (recursive, to i n c l u d e
t h e contents o f a l l s u b d i rectories). -
[student@desktopX - ] $ ls
total 15
-1
total 15
d rwx - - - - - - . 16 student student 4096 Feb 8 16 : 15
drwxr-xr - x . 6 root root 4096 Feb 8 16 : 13
-
[student@desktopX - ] $
-
T h e two specia l d i rectories a t t h e top o f t h e l is t i n g refe r t o t h e c u rrent d i rectory ( . ) a n d t h e
parent d i rectory ( ) . T h e s e s p e c i a l d i rectories exist i n every d i rectory o n t h e syst e m . T h e i r
• .
u s ef u l ness w i l l b e c o m e a p pa re n t w h e n f i l e m a n a g e m e n t c o m m a n d s a re practiced. -
36 R H 1 24- R H E L 7 - e n -1-20140606 -
-
-
Navigating paths
-
A Important
T
-
[student@desktopX -]$ ls - R
-
Desktop Documents Downloads Music Pictures Public Templates Videos
. /Desktop :
. /Documents :
thesis_chapter1 . odf thesis_chapter2 . odf
-
-
. /Downloads :
. /Music :
. /Pictures :
. /Public :
. /Templates :
-
. /Videos :
blockbuster1 . ogg blockbuster2 . ogg
[student@desktopX -]$
-
-
T h e cd co m ma n d h a s m a ny o p t i o n s . A few a re so usef u l as t o be wort h pract i c i n g e a r l y a n d u s i n g
ofte n . T h e co m m a n d cd - c h a n g e s d i rectory to t h e d i rectory w h e re t h e u s e r w a s previous to t h e
c u rre nt d i recto ry. Watch as t h i s u s e r ta kes a d va ntage o f t h i s b e h a v i o r to a l ternate between two
-
d i rectories, u s ef u l when process i n g a series of s i m i l ar tasks.
[student@desktopX Documents]$ cd -
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd -
-
- R H1 24- R H E L 7-en-1-20140606 37
-
-
C h a pter 2 . M a n a g i n g F i l e s Fro m t h e Co m m a n d L i n e
-
[student@desktopX Videos]$ cd
[student@desktopX Videos] $ pwd
.
/home/student/Videos
-
[student@desktopX Videos] $ c d
[student@desktopX -]$ pwd
/home/student
[student@desktopX -]$ cd
-
/home
[student@desktopX home]$ cd
-
/home/student
[student@desktopX -]$ -
R References -
info libc ' file n ame resolu t ion ' (GNU C Library Reference Manual)
•Section 11.2.2 F i l e n a m e reso l ut i o n -
-
U T F-8 and U n icode
http://www. u tf-8.com/
-
-
-
Quiz
Match t h e fo l l owi n g items to t h e i r cou nterparts i n t h e t a b l e.
-
Act i o n to a c co m p l i s h Co m m a n d
-
Dete r m i n e t h e a bs o l ute p a t h n a m e of t h e c u r r e n t locat i o n .
-
Ret u rn to t h e m ost p rev i o u s wo r k i n g d i recto ry.
L i st t h e c u r re n t l oca t i o n ( l o n g fo r m a t ) w i t h h i d d e n f i l e s .
-
M ove u p to t h e pa re nt of t h e cu r re n t l oc a t i o n .
-
- R H124- R H E L 7 - en -1 -20140606 39
-
-
C h a pter 2. M a n a g i n g F i l es Fro m t h e Co m m a n d L i n e
Solution
-
-
Dete r m i n e t h e a bs o l ute p a t h n a m e of t h e c u r re n t l oca t i o n . pwd
-
Ret u r n to t h e m ost p rev i o u s wo r k i n g d i recto ry. cd -
40 R H 1 24- R H E L 7 - e n -1-20140606 -
-
M a n a g i n g F i l e s U s i n g C o m m a n d - L i n e Too l s
M a n a g i n g F i l es U s i n g C o m m a n d - L i n e Too l s
O bj ect ives
After c o m p leting t h i s sect i o n , students s h o u l d be a b l e to c reate, co py, l i n k , move, a n d remove
f i l es a n d s u b d i rectories in various d i rectories.
Co m m a n d - l i n e fi l e m a n a g e m e n t
F i l e m a na g e m e n t i nvolves c reat i n g , d e l e t i n g , copying, a n d moving f i l es. Add iti o n a l l y, d i rectories
c a n b e c reated, d e l eted, copied, and m oved to help o rg a n ize f i l es l o g i c a l ly. When wo r k i n g at the
co m m a n d line, file m a n a g e m e n t req u i res a wa re n ess of t h e c u rrent w o r k i n g d i rectory to choose
e i t h e r a bs o l ute o r re lat ive path syntax as most efficient for t h e i m m e d iate task.
C reate d i rectories
The mkdir c o m m a n d c reates o n e o r m o re d i rectories o r s u b d i recto ries, genera t i n g e r ro rs if
t h e file name a l ready exists o r w h e n atte m pt i n g to cre ate a d i rectory i n a parent d i rectory
t h at d o e s n ' t exist . The -p parent o p t i o n c reates m i s s i n g pa rent d i rectories for the re q u ested
desti n at i o n . Be cautious when u s i n g mkdir - p, s i n ce a c c i d e n t a l s p e l l i ng m ista kes c reate
u n i ntended d i rectories without g e n e ra t i n g error messages.
R H 1 24 - R H E L 7-en-1-20140606 41
-
C h a pter 2 . M a n a g i ng F i l e s Fro m t h e C o m m a n d L i n e
Documents/Proj ectY :
Documents/Thesis :
-
Documents/Thesis/Chapter2 :
Documents/Thesis/Chapter3 :
-·
Videos :
blockbuster1 . ogg blockbuster2 . ogg Watched -
Videos/Watched :
[ student@desktopX -]$
-
Copy files -
[student@desktopX Videos]$ ls
total 0
- rw- rw- r - - . 1 student student 0 Feb 8 16 : 23 blockbusterl . ogg
-1
i
d i rectories, with contents, req u i res the - r recursive option.
I
[student@desktopX Documents]$ cp - r Thesis Proj ectX
[ student@desktopX Documents]$ c p t he sis_chap t e r 2 . odf
-
[ student@desktopX Documents]$ ls - R
Thesis/Chap t e r 2 /
-
-
Com m a n d - l i n e f i l e m a n a g e m e n t
. /Proj ectX :
-
Thesis thesis_chapter1 . odf thesis_chapter2 . odf
. /Proj ectX/Thesis :
. /Proj ectY :
-
. /Thesis :
-
Chapter1 Chapter2 Chapter3
. /Thesis/Chapter1 :
. /Thesis/Chapter2 :
thesis_chapter2 . odf
-
. /Thesis/Chapter3 :
[student@desktopX Documents]$
-
-
M ove fi l e s
The mv command re n a m e s f i l e s i n t h e same d i rectory, o r re l ocates f i l e s to a new d i recto ry. F i l e
contents re m a i n u n c h a n g ed. F i l es m oved to a d iffe re nt f i l e system req u i re c rea t i n g a n e w f i l e by
- copy i n g the s o u rce f i l e, t h e n d e l e t i n g the s o u rce f i l e. A l t h o u g h norma l l y t ra n s pa re n t to the user,
l a rge f i l e s may t a ke n o ti cea b l y l o n g e r to m ove.
[student@desktopX Videos]$ cd
[student@desktopX Documents]$
- . . /Docume n t s
total 0
ls - 1
[student@desktopX Documents]$
mv thesis_c h ap t e r 2 . odf t h esis_chapter2_reviewed . odf
[student@desktopX Documents]$
mv t hesis_c hap t e r 1 . odf Thesis/Chap t e r 1
..
ls - lR
total 16
-
total 0
. /Proj ectY :
-
total 0
. /Thesis :
total 12
-
-
. /Thesis/Chapter1 :
-
-
C h a pter 2. M a n a g i n g F i l es Fro m t h e C o m m a n d L i n e
total 0
-
total 0
[student@desktopX Documents]$ -
R e m ove fi l es a n d d i re c t o r i e s
Defa u l t syntax for rm d e l etes fi les, but not d i rectories. D e l e t i n g a d i recto ry, and pote n t i a l l y m a n y -
s u b d i rectories a n d fi l es b e l ow it, req u i res the - r recursive o pt i o n . There is n o com m a n d - l i n e
u n d e l ete fea t u re, n o r a t ra s h b i n from w h i c h to restore.
/home/student/Documents
[ student@desktopX Documents]$ r m thesis_chap t e r 2_reviewed . odf
[ student@desktopX Documents]$ rm Thesis/Chap t e r l
rm : cannot remove ' Thesis/Chapter1 ' : Is a directory
-
/home/student/Documents
[student@desktopX Documents]$ rmdir P roj ectY
[ student@desktopX Documents]$ rmdir Proj ectX
-
rmdir : failed to remove ' Proj ectX ' : Directory not empty
[student@desktopX Documents]$ rm - r P roj ectX
[student@desktopX Documents]$ ls - lR -
total e
[student@desktopX Documents]$ -
44 R H 1 24- R H E L 7 - e n -1 -20140606 -
-
-
References
-
-
-
C h a pter 2. M a n a g i ng F i l e s Fro m t h e C o m m a n d L i n e
-
Guide d exercise -
Outcomes:
S t u d e nts w i l l practice c reat i n g , rea rra n g i n g , and d e l et i n g f i l es. -
to use the g u ided s o l u t i o n to see and practice how the t a s k is acco m p l i s he d . U s e the s h e l l
t a b c o m p l et i o n to l ocate a n d c o m p l ete p a t h n a mes m o re e a s i l y.
-
C reate s i x f i l es with n a m e s of t h e form songX . mp3.
-
I n e a c h set, re p l a ce X with t h e n u m be rs 1 t h ro u g h 6.
[student@serverx - ] $ t o u c h song1 . mp3 song2 . mp3 song3 . mp3 song4 . mp3 song5 . mp3
[ student@serverx
-
song6 . mp3
- ] $ t o u c h snapl . j pg snap2 . j pg snap3 . j pg s n ap4 . j pg snap5 . j pg
[ student@serverX
snap6 . j pg
- ] $ t o u c h filml . avi film2 . avi film3 . avi film4 . avi film5 . avi
[student@serverx
-
film6 . avi
- ] $ ls -1
t h e d e st i n a t i o n f o r e a c h f i l e m a n a g e m e n t task.
[student@serverX - ] $ m v song1 . mp3 song2 . mp3 song3 . mp3 song4 . mp3 song5 . mp3 -
[student@serverX - ] $ mv
song6 . mp3 Music
snapl . j pg snap2 . j pg snap3 . j pg snap4 . j pg snap5 . j pg
[ student@serverx - ] $ mv
snap6 . j pg Pic t u res -
filml . avi film2 . avi film3 . avi film4 . avi film5 . avi
[student@serverX - ] $ ls
film6 . avi Videos
-1 M u s ic Pic t u res Videos
-
-
-
G u i d e d exercise
-
D 4. Yo u w i l l co l l ect some of the new fi l es i nto t h e p roj ect d i rectories for fa m i l y and friends.
U s e a s m a n y c o m m a n d s as needed. Yo u d o not have to use o n l y o n e com m a n d as i n t h e
exa m p l e. For e a c h project, fi rst c h a n g e to t h e p roject d i rectory, t h e n c o p y t h e s o u rce f i l es
into t h i s d i recto ry. Yo u a re m a k i n g copies, s i n c e you wi l l keep t h e ori g i n a l s after g i v i n g
t h e s e proj ects to fa m i l y a n d friends.
-
[student@serverX friends]$ ls
snap1 . j pg -/Pic t u res/snap2 . j pg -/Videos/film1 . avi -/Videos/film2 . avi .
[student@serverX family]$ ls
snap3 . j pg -/Pic t u res/s nap4 . j pg -/Videos/film3 . avi -/Videos/film4 . avi
- -1
-
D 6. Yo u r p rojects a re n o w d o n e. Time to c l e a n u p t h e p rojects.
[student@serverX work]$ c d
[student@serverX -]$ rmdir family f riends
rmdi r : failed to remove ' family ' : Directory not empty
-
-
rmdir : failed to remove ' friends ' : Directory not empty
U s i n g the r md i r co m m a n d s h o u l d fa i l s i n ce both d i rectories a re n o n - e m pty.
-
D 7. U s e a n o t h e r co m m a n d that w i l l s u cceed i n d e l e t i n g both t h e fa m i l y a n d f r i e n d s fo l d e rs.
[student@serverX -]$
[student@serverX -]$
- rm - r family f riends
ls - 1
-
-
C h a pter 2. M a n a g i n g F i l e s Fro m t h e C o m m a n d L i n e
[student@serverx -]$ cd
[student@serverX work]$
-
wo rk
rm song5 . mp3 song6 . mp3 snap5 . j pg snap6 . j pg film5 . avi
[student@serverx work]$
film6 . avi
-
ls - 1
[student@serverx work]$ cd
[student@serverx -]$ rmdir
[student@serverx -]$ ls
-
wo rk
-1
48 R H 1 24- R H E L7-en-1-20140606 -
-
-
M a tc h i n g F i l e N a m e s U s i n g Pat h N a m e E x p a n s i o n
-
M atc h i n g Fi l e N a m es U s i n g Pat h N a m e
-
E xp a n s i o n
-
Objectives
- After c o m p l et i n g t h i s sect i o n , s t u d e n t s s h o u l d be a b l e to u s e m eta-cha racte rs a n d e x pa n s i o n
tec h n i q ues to i m p rove f i l e m a n a g e m e n t p rocess i n g eff i c i e n cy.
-
File globbing: path name ex pansion
T h e B a s h s h e l l has a path n a m e - m a t c h i n g capa b i l ity h isto rica l l y ca l l ed globbing, a b b reviated from
-
t h e "g l o b a l com m a n d " file path e x p a n s i o n p ro g ra m of early U N I X . T h e B a s h g l o b b i n g feat u re,
co m m o n l y c a l led pattern matching o r " w i l d ca rd s " , ma kes m a n a g i n g l a rg e n u m be rs of files e a s i e r.
U s i n g meta-characters that "expa n d " to match f i l e a n d path n a mes b e i n g soug ht, co m m a n d s
-
p e rfo r m o n a focused s e t o f fi l es at o n ce.
Patt e r n m a t c h i n g
-
G l o b b i n g is a shell c o m m a n d - p a rs i n g opera t i o n t h a t expa n d s a w i l d ca rd pattern i nto a l i st of
matc h i n g path n a m es. C o m m a n d- l i n e meta-cha racters a re re p l aced by t h e match l ist p r i o r to
com m a n d execut i o n . Patte rns, e s p e c i a l l y s q u a re- b rac keted c h a racter c l asses, t h at d o not ret u r n
-
matches d i s p l ay t h e orig i n a l patte r n requ est a s l itera l text. T h e fo l l ow i n g a re co m m o n m eta
c h a racters and pattern c l asses.
-
Patte rn M at c h e s
*
Any st r i n g of 0 or m o re c h a racte rs.
? A n y s i n g l e c h a racter.
-
-
-
C h a pter 2. M a n a g i n g F i l e s Fro m t h e C o m m a n d L i n e
-
A s a m p l e s e t o f f i l es is u s e f u l to d e m o n st rate expa n s i o n .
[student@desktopX glob] $ ls
baker cast dog easy
able alfa baker bravo cast charlie delta dog easy echo
[student@desktopX glob] $
-
[student@desktopX glob] $ ls a *
able alfa
[student@desktopX glob] $ ls * a *
-
[student@desktopX glob] $
-
T i l d e ex p a n s i o n
T h e t i l d e c h a racter (-), w h e n fo l l owed by a s l a s h d e l i m iter, matches t h e c u rrent u s e r ' s h o m e
d i recto ry. W h e n fo l l owed by a stri n g o f c h a racters u p to a s l a s h , it w i l l b e i n t e r p reted as a -
able alfa baker bravo cast charlie delta dog easy echo
[student@desktopX glob] $ e c h o -/glob
/home/student/glob
-
[student@desktopX glob]$
-
B ra c e ex p a n s i o n
B race ex p a n s i o n is u s e d to g e n e rate d i s c retio n a ry strings of c h a racters. B races conta i n a c o m m a
-
s e p a rated l ist of s t r i n g s , o r a seq u e n ce ex p ress i o n . The res u lt i n c l udes t h e t e x t preced i n g o r
fo l l owing t h e brace d e f i n i t i o n . Brace expa n s i o n s may be neste d , o n e i n s i d e a n ot h e r.
C o m m a n d s u bst i t u t i o n
-
Co m m a n d s u bst i t u t i o n a l l ows t h e o u t p u t of a co m m a n d to re p l ace t h e command itse l f. Co m m a n d
s u bst itution occ u rs w h e n a co m m a n d i s e n c losed w i t h a beg i n n i n g d o l l a r s i g n a n d p a re n t h es i s ,
$ ( command ) , o r with b a c k t i c k s , · command ' . T h e form with b a c kt i c ks i s o l d e r, a n d h a s t w o
--
50 R H 1 24-R H E L 7-en-1-20140606 -
-
-
F i l e g l o b b i n g : path n a m e expa n s i o n
-
- [student@desktopX glob]$
Prote c t i n g arg u m e n t s fro m expa n s i o n
-
M a n y c h a racters h a ve s pecia l m ea n i n g i n t h e B a s h s h e l l . To i g n ore m eta-c h a racter s p e c i a l
mea n i n g s , quoting a n d escaping a re u s e d to p rotect t h e m f r o m s h e l l e x p a n s i o n . T h e backs l as h
(\) is a n e s c a p e c h a racter i n Bash, p rotect i n g t h e s i n g l e fo l l ow i n g c h a racter fro m s p e c i a l
-
interpreta t i o n . To p rotect l o n g e r c h a racter st r i n g s, s i n g l e ( ' ) or d o u b l e q u otes ( " ) a re used to
e n c l ose st r i n g s.
-
Use d o u b l e q u ot a t i o n m a rks to s u p p ress g l o b b i n g a n d s h e l l expa n s i o n , but sti l l a l l o w com m a n d
a n d va ria b l e s u bs t i t u t i o n . Va r i a b l e s u bst i t u t i o n i s concept u a l l y identica l to com m a n d
s u bstitut i o n , b u t may u s e opt i o n a l b race syntax.
desktopX
[student@desktopX glob]$ echo " * * * * * h o s t n ame is $ { h o s t } * * * * * "
hostname is desktopX
-
-
[student@desktopX glob]$
- U s e s i n g l e q u ot a t i o n m a rks t o i nterpret a// text l itera l l y. Obse rve t h e d i fferen ce, o n both screen
a n d keyboard, betwee n the s i n g l e q u ote ( ' ) and the c o m m a n d substitution ba c k t i c k ( ' ) . Besides
s u ppress i n g g l o b b i n g and s h e l l expa n s i o n , q u otat i o n s d i rect the s h e l l to a d d itio n a l l y s u p press
- com m a n d and va r i a b l e s u bst i t u t i o n . The q u est i o n m a r k is a m eta-c h a racter that a l so needed
p rotect i o n from e x pa n s io n .
-
[student@desktopX glob]$
-
R References
bash(1), cd(1), glob(7), isalpha(3), ls(1), pat h_resolu t ion(7) , a n d pwd(1) m a n
- pages
-
-
C h a pter 2. M a n a g i n g F i l e s Fro m t h e C o m m a n d L i n e
Quiz -
G�881 IG
-
* [ [:d i g i t : ] ] * [ [: u p p e r: ] ] *
O n l y f i l e n a m e s b e g i n n i n g w i t h " b"
O n l y f i l e n a m e s c o n ta i n i n g a " b "
-
O n l y f i l e n a m e s a t l east 3 c h a ra c t e rs i n l e n g t h -
O n l y f i l e n a m e s t h a t conta i n a n u m b e r -
-
O n l y f i l e n a mes t h a t b e g i n w i t h a n u p pe r-case l ette r
-
Sol ution
-
Solution
-
-
O n l y f i l e n a m es at l east 3 c h a racters i n l e n g t h ???*
O n l y f i l e n a m es t h a t conta i n a n u m be r * [ [:digit:] ] *
-
O n l y f i l e n a m es t h a t beg i n w i t h a n u p p e r-case l ette r [ [: u pper: ] ] *