Accessing The Command Line: Training
Accessing The Command Line: Training
®
TRAINING
C H A PT E R 1
Overview
• L a u n c h a p p l i ca t i o n s i n a G N O M E desktop e n v i ro n me nt.
Lab • Acces s i n g t h e C o m m a nd L i ne
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
-
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 Loca l
-
Conso l e
-
Objectives
After com p l et 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 a L i n u x system on a loca l text -
r-
l [ student@desktopX -]$ -
[ root@desktopX -]#
� - �����
I
i
I
-
L>=-SJ
-�>,
Note -
Virtual consoles -
-
-
Shel l basics
-
-
Important
I n t h e p re-config u re d v i rt u a l i m a g es d e l ivered by Red H a t , l o g i n p ro m pts have b e e n
- d i sa b l ed i n t h e v i rt u a l co n s o l es .
-
Note
I n Red H a t Enterprise L i n u x 5 a n d ea r l i e r, t h e f i rst six v i r t u a l c o n s o l e s a l ways p rovided
-
text login prom pts. When the g ra p h i ca l e n v i ro n m e nt was l a u n c h e d , it ran o n v i rt u a l
c o n s o l e seven (accessed t h ro u g h Ct r l +Al t + F 7 ) .
Shell basics
-
C o m m a n d s entered at t h e s h e l l p ro m pt have t h ree basic pa rts:
• Command to run
-
• Options to adjust the b e h a v i o r of t h e com m a n d
- R H 1 24- R H E L 7-en-1-20140606 3
-
-
C h a pte r 1 . Access i n g t h e C o m m a n d L i n e
-
r·�
[student@desktopX -]$ date
date [OPTION] . . . [+FORMAT]
! - - h elp
I -
Note
T h e man page for a com m a n d h a s a SY N O P S I S sect i o n t h a t p rovi d e s i nfor m a t i o n a b o u t -
R References
in t r o(l), bash(l ) , console(4), p t s (4), a n d man - pages(7) m a n pages -
Note: Some details of the console(4) man page, involving init(B) and inittab(S), are
outdated. -
-
4 R H 1 24- R H E L7-en-1-20140606
-
-
-
Quiz
- I A rg u m e nt jI 18 Command Phys i ca l c o n s o l e
-
j P ro m pt
!B I I
Shell Te r m i n a l Vi r t u a l c o n s o l e
Description Term
-
I
-
T h e n a m e o f a p ro g ra m to r u n .
- T h e pa rt o f t h e c o m m a n d l i n e t h a t a dj u sts t h e
b e h a v i o r o f a co m m a n d .
-
T h e p a rt o f t h e co m m a n d l i n e t h a t s pe c i f i e s t h e
- ta rget t h a t t h e co m m a n d s h o u l d o p e rate o n .
-
T h e h a rd wa re d i s p l a y a n d keyboa rd u s e d t o
i nt e ract w i t h a syst e m .
-
O n e of m u lt i p l e l o g i ca l co n s o l es t h a t c a n e a c h
- s u p po rt a n i n d e pe n d e n t l og i n sess i o n .
- R H 1 24- R H E L 7-en-1-20140606 5
-
-
C h a pte r 1 . Access i n g t h e C o m m a n d L i n e
-
Description Term -
6 RH124- R H E L 7 - e n -1-20140606 -
-
-
Solution
-
Solution
-
Description Term
T h e n a m e of a p rog ra m to r u n . Co m m a n d
-
-
T h e pa rt of t h e c o m m a n d l i n e that s p e c i f i es t h e A rg u m e nt
ta rg et t h a t t h e c o m m a n d s h o u l d o p e rate o n .
-
T h e h a rd wa re d i s p l ay a n d keybo a rd u s ed to Phys i ca l c o n s o l e
-
i nt e ra ct w i t h a syst e m .
·-
O n e of m u l t i p l e l o g i ca l c o n s o l es t h a t ca n e a c h V i rt u a l co n so l e
s u p p o rt a n i n d e p e n d e n t l og i n sess i o n .
-
-
-
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: ] ] *
-
-
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
-
L a b : M a n a g i n g F i l es wit h S h e l l Expa n s i o n
-
Outcomes:
Fa m i l ia rity and p ractice with many fo r m s of w i l d c a rd s for l o ca t i n g and u s i n g f i l es. -
3. To o rg a n ize the TV e p isod es, c reate two s u b d i rectories n a m e d seasonl and season2 u n d e r
t h e exist i n g Videos d i recto ry. U s e o n e com m a n d .
-
-
-
Perfo r m a n c e check l i st
-
11. W i t h o n e co m m a n d , change t h e work i n g d i rectory to vacation, then l ist its f i l es. Episode
2 i s a l so needed. Ret u r n to t h e season2 d i rectory using the previous working directory
-
s h o rtcut. T h i s w i l l s u cceed if t h e l a st d i rectory c h a n g e was acco m p l i shed w i t h o n e com m a n d .
C o p y t h e e p i s o d e 2 f i l e i nto vac ation. Ret u r n to vacat ion u s i n g t h e s h o rtcut a g a i n .
14. The p l o t c h a n g es were not s u ccessfu l . D e l ete t h e plot_c hange d i recto ry. F i rst, d e l ete
a l l of the f i l e s in the plot_c hange d i recto ry. C h a n g e d i rectory up one l ev e l because t h e
- d i rectory ca n n ot be d e l eted w h i l e it i s t h e w o r k i n g d i recto ry. Try to d e l ete t h e d i rectory u s i n g
t h e r m com m a n d without t h e recursive o pt i o n . T h i s att e m pt s h o u l d fa i l . Now u s e t h e rmd i r
co m m a n d , w h i c h w i l l succeed.
-
15. When t h e vaca t i o n is over, t h e vac ation d i rectory is n o longer needed. D e l ete it using the
rm co m m a n d w i t h t h e recursive option.
-
W h e n f i n i s h e d , ret u r n to t h e h o m e d i recto ry.
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
Solution
-
Outcomes:
Fa m i l ia rity and p ractice w i t h many forms of w i l d ca rd s for l ocati n g a n d using fi l es. -
t h e l a b i n t h e h o m e d i rectory.
! [student@serverX
[student@serverX - �'"$
��--
- ] $ t ouch myst e ry_chap t e r { 1 . . 8} . odf
I
ls - 1
---�----
-
3. To o rg a n ize t h e TV e p i sodes, c reate two s u b d i rectories n a m e d season l a n d season2 u n d e r
t h e exist i n g Videos d i rectory. Use o n e co m m a n d .
[student@serverx
[student@serverx
- ] $ mkdir Videos/seaso n { 1 . . 2 }
- ] $ ls - lR
-
4. Move the a p p ro p riate T V episodes i nto t h e s e a s o n s u b d i rectories. Use o n l y two c o m m a n d s ,
specify i n g d e st i n a t i o n s u s i n g re l a t ive syntax.
lI.�[student@serverX
tudent@serverX - ] $ mv tv_season1* Videos/season1
-
[student@serverx
- ] $ mv tv_seaso n 2 * Videos/season2
- ] $ ls - lR
-
..
[student@serverx
I [student@serverX
-
- ] $ mkdir - p Docume n t s /my_best selle r/chapt e r s
- ] $ ls - lR
-
56 RH124- R H E L 7-en-1-20140606
-
-
Sol ution
-
-·
ll � student@serverX -]$ � �
-
parent option is n ot needed s i n ce t h e my_b e s t selle r p a rent d i rectory a l re a d y exists.
[student@serverX -]$
mkd r Docume n t s /my_bes t s lle r / { edit o r , plot_change , vacation}
1 ls - lR
l
[student@serverX chapters]$
[student@serverX chapters] $
mv -/myste ry_chapte r * .
I ls - 1
L �������- �����--'
8. T h e f i rst two c h a pters a re sent to t h e editor for review. To re m e m b e r to not mod ify t h ese
-
c h a pte rs d u ri n g t h e revi ew, move those two c h a pters only to t h e editor d i recto ry. U s e
re l a t ive s y n t a x sta r ti n g from t h e chapt e r s s u b d i recto ry.
; [student@serverX chapters]$
II [student@serverx chapters]$
-
mv mys t e ry_chap t e r l . odf mys t e ry_chapte r2 . odf . . /ed i t o r
[student@serverx chapters]$
ls - 1
ls - 1 . . /editor
- L - �����
I [student@serverX chapters]$
vacation. Use one com m a n d with out w i l d ca rd c h a racte rs.
�� tudent@serverx chapters] $
ls - 1
I
..
I
-·
-
_ ls - 1 . . /vacation
l
i [ student@serverx chapters]$ cd -/Videos/season2
[stud � nt@serverX season2]$ cp tv_season2_episode1 . og g
vacation
-/Document s/my_best selle r /
-
.
[student@serverX vacation] $ c d -
-1
-
[student@serverX vacation] $ ls - 1
k- - ��������- -����- -���
- R H 1 24- R H E L7-en-1-20140606 57
-
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
-i
d i recto ry, t h e n use o n e co m m a n d from t h e re.
[student@serverx
[ student@serverX
[student@serverx
vacation] $ cd . .
my_bestseller]$ cp
my_bestseller]$ ls
I
I
chap t e r s/my s t e ry_c hapt e r [ 56 ] . odf plot_change
[student@serverx my_bestseller]$ ls
- 1 chapters
- 1 plot_change
L_ ���������������������--�������������
mys t e ry_chapter5_$ ( da t e +
[student@serverx plot_change] $ ls
mys t e ry_chapter5_$USER . odf
-1
[student@serverX plot_change] $ cd . .
[student@serverx my_bestseller]$ rm plot_c hange
rm : cannot remove ' plot_change ' : Is a directory
[student@serverx my_bestseller]$ rmdir plot_change
[student@serverX my_bestseller]$ ls -1
-
W h e n f i n i s h e d , ret u r n to t h e h o m e d i rectory.
[student@serverX my_bestseller]$
[student@serverx my_bestseller]$
rm - r vacation
[student@serverX my_bestseller]$
ls - 1
cd
-
58 R H 1 24- R H E L 7-en-1-20140606
-
Solution
-
S u m m a ry
-
T h e L i n u x F i l e Syste m H i e ra rc h y
I d e ntify t h e p u rpose for top- l ev e l d i rectories i n t h e L i n u x h i e ra rc hy.
-
Locat i n g F i l e s by N a m e
I nterpret a n d a p pro priate l y u s e f u l l a n d partia l p a t h f i l e n a m e synta x .
M a n a g i n g F i l es U s i n g Co m m a n d - L i n e Too l s
Work from t h e com m a n d l i n e t o c reate, m ove, a n d d e l ete f i l es a n d d i rectories.
-
M a t c h i n g F i l e N a mes U s i n g Pat h N a m e E x p a n s i o n
Lear n how to s pecify m u lt i p l e fi l es u s i n g many w i l d ca rd tec h n i q ues.
--
- R H124- R H E L 7-en-1-20140606 59
-
-
--
1
--
--
-
I
--
-
red h at ®
®
TRAINING
C H A PT E R 3
Overview
Objectives
After com 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 l ocate d o c u m e ntat i o n a n d research
a n swers a b o u t commands.
Note
-
M a n u a l section 9 is a recent a d d it i o n to L i n u x . N o t a l l m a n sect i o n l is t i n g s refere n c e it.
To d i st i n g u i s h i d e n t i c a l topic names i n d iffe rent sect i o ns, man page refe re nces i n c l u d e t h e
section n u m be r i n pa re n t h eses after t h e topic. For exa m p l e, passwd(l ) describes t h e c o m m a n d
to c h a n g e pa sswords, w h i l e passwd ( 5 ) ex p l a i n s t h e / e t c / p a s swd fi l e format f o r sto r i n g l oca l
-
u s e r accou nts.
-
-
Co m m a n d Res u l t
-
Spac e b ar S c ro l l fo rwa rd (d own) o n e s c r e e n
Page Down S c ro l l forwa rd (d own) one screen
Page Up S c ro l l b a c kwa rd (u p) o n e screen
-
Important
- When p e rfo r m i n g searc h es, s t ri ng a l l ows regular expression syntax. W h i l e s i m p l e text
(s u c h a s p a s s wd ) works a s expected, reg u l a r express i o n s use m eta-c h a ra cters (s u c h
as $, * , . , a n d ") for more s o p h i st icated pattern matc h i n g . T h e refore, sea rc h i n g with
- stri n g s which i n c l u d e program ex pressi o n m eta-ch a racters, such a s make $$$, m i g h t
y i e l d u n expected res u lts.
- R H124- R H E L 7-en-1-20140606 63
-
-
Note -
Important
T h e man com m a n d - K option performs a f u l l -text page searc h , not j u st t i t l es a n d -
R References -
man(1 ), mandb(8), man - pages(7), less(1), i n t ro(1), int ro(2), i n t ro(5), i n t ro(7),
int ro(8) man pages
-
64 R H1 24- R H E L 7 - e n -1-20140606 -
-
-
Practice : U s i n g t h e m a n Co m m a n d
-
P ra ct i ce: U s i n g t h e m a n Co m m a n d
-
-
Guided exercise
I n t h i s l a b, you w i l l p ractice f i n d i n g re l eva nt i nfo r m a t i o n by u s i n g man options a n d a rg u ments.
O utcomes
Fa m i l i a rity w i t h t h e man L i n u x m a n u a l system and practice f i n d i n g usef u l i n fo r m a t i o n by
-
searc h i n g and b rows i n g .
[student@serverX ] $
-
- -- -- -·--------
I
-
- ma n i g e d i t
- --
_ --------
_ ____� - ----�
D 3. Research the gedit o pt i o n used to b eg i n a n e d i t i n g sess i o n with the c u rsor at the end of
the f i l e.
-
gedit + fil ename
[student@serverX -]$
-
����- - - · ---··---------------- ---
man 1 su
._
, __
_ ----- - ··- - - · - --- -------
-
s u a s s u m es a use rname of root.
[student@serverX -]$
-
-
pas swd - 1 username
-
-
• Protect yo u r password .
t i m e?
-
t u ne2fs(8) fou n d w i t h man - k ext4
-
Rea d i n g Documentation U s i n g pinfo C o m m a n d
Rea d i n g D o c u m e nt a t i o n U s i n g p i nfo C o m m a n d
O bj ect ives
After c o m p l e t i n g t h i s section, students s h o u l d be a b l e t o research a n swers u s i n g G N U I nfo
d o c u m e nt a t i o n .
I nt ro d u c i n g G N U i nfo
M a n p a g es have a formal f o r m a t u s e f u l a s a c o m m a n d referen ce, b u t less usef u l a s
g e n e r a l d o c u mentation. For s u c h d o c u m e nts, t h e GNU Project deve l o ped a d ifferent o n l i n e
d o c u m e ntation system, k n ow n a s GN U i nfo. I nfo d o c u ments a re a n i m po rt a nt resou rce o n a R e d
Hat Enterp rise Lin u x system b e c a u s e m a ny f u n d a mental com pone nts a n d u t i l ities, s u c h as t h e
coreutils p a c ka g e a n d glibc sta n d a rd l i b ra ries, a re e i t h e r deve l oped by t h e GNU P roject o r u t i l ize
the i nfo d o c u ment system.
File: dir Node: Top This is the top of the INFO tree
* Menu:
Archiving
• Cpio : ( cpio) . C o py - i n - c o p y - o u t a rc h i v er t o t a p e o r d i s k .
• Ta r : ( ta r ) . Making tape ( o r d i s k ) a rchive s .
Basics
• Common o p t i on s : { 'oreuti.ls ) Common options .
Common o p t i o n s .
• C o reutil s : (coreutils ) . C o re GNU (file , text , shel l ) utilities .
• D a t e input f o rmats : ( c o reutil s ) Date input formats .
• F ile p e rmi s s i o n s : ( c o reutils ) File permissions .
Access mode s .
• Finding file s : ( f ind ) . Operating o n f i l e s m a t c h i n g c e rt a i n c ri t e r i a .
* ed: ( ed ) . The GNU Line Edito r .
iewing line 25/2llfl2 , 1%
R H 1 24· R H EL 7 ·en-1-20140606 67
-
N av 1 g a t 1 on pinto man
D i s p l ay t h e d i rectory of topics d -
-
S c ro l l forward (down) o n e h a l f-scre e n d
-
D i s p l ay t h e p a r e n t n o d e o f a topic u -
Q u i t t h e p ro g ra m q q
R References
pinfo info (Info: An Introduction)
-
• A l l Sect i o n s
The G N U Project
-
http://www.g n u .o rg/g n u/t h e g n u p roject . h t m l
-
-
Practice: U s i n g t h e p i nto Co m m a n d
-
P ra c t i ce: U s i n g t h e p i nto Co m m a n d
-
- Guide d exercise
- ii [student@serverx - ] $ pinfo
I
Press u a g a i n . O b s e rve that when positioned at the top of a topic node, moving u p
- ret u r n s t o t h e d i rectory o f top ics. A lternate l y, p ress i n g d from a ny l eve l o r t o p i c w i l l m ove
d i rect l y to t h e d i rectory of topics.
- R H 1 24- R H E L7-en-1-20140606 69
-
-
D 1 0. Exit pinto.
Press q to q u i t pinto. -
D 11. I nvoke pinfo a g a i n , specify i n g nano a s the desti n a t i o n topic from t h e com m a n d l i n e.
[student@serverx -]$
-
!
j pinfo nano
!
U s e a r row keys to h ig h l ig h t a topic, PageUp and PageDown to b rowse the text, t h e n press -
u to m ove u p one leve l . Press q to q u it pinfo when you a re fi n i s hed.
70 R H 1 24- R H E L7 - e n -1 -20140606 -
-
-
-
Objectives
After com p l et i n g t h i s section, st u d e n t s s h o u l d be a b l e to researc h information u s i n g Red H a t
Packa g e M a n a g e r d o c u m e ntation.
I [student@desktopX -]$
p o i n t i n g a b rows e r of c h o i ce to f i l e : / / / u s r / s h a r e / d o c a n d uti l i z i n g a m o u se.
- - -- --- - --�---�----�--�
;
firefox file : // / u s r / s ha re/doc I
! ------��--- · __J
-
Some packages come with exte nsive exa m p l es, confi g u ration f i l e te m p l ates, scripts, tutori a l s ,
o r user g u ides. B rowse / u s r / s h a re/doc/vs f t pd - * as a n exa m p l e. S o m e d o c u m e ntation i s
-
s pa rse; t h e zip u t i l ity i n c l u d es t h e com p ress i o n a l g o r i t h m used a n d l itt l e e l se. O t h e r packages
i n c l udes l a rg e user m a n u a l s o r deve l o p e r g u ides, o r e l ectro n i c copies of related, p u b l i s h ed boo ks.
-
Note
Deve l op e rs may c hoose to b u n d l e exte n s ive doc u m e ntation i n a s e p a rate R P M . The
- gnuplot p rog ra m h a s t h e extra gnuplot-doc package, which m ust b e i n sta l l e d s e p a rate l y.
O t h e r s i m i l a r packages to b rowse i n c l u d e bash-doc a n d samba-doc. Often , extra
packages a re fo u n d i n Red H a t E n t e r p r i s e L i n u x ' s Optional softwa re c h a n n e l .
-
Note
-
T h e k e r n e l itse l f h a s a s i g n if i c a n t d o c u m entation package. T h e kernel-doc package is a
trea s u re of kern e l , d r ive r, t u n i n g , a n d a d v a n ced config u ration i n f o r m a t i o n . E x p e rienced
-
syst e m a d m i n istrators reg u l a r l y research kernel-doc f i l es.
References -
hie r (7 ) m a n page
• D i s c u sses t h e h i e ra rchy of Linux d i rectories, i n c l u d i n g /usr / s h a re/doc.
-
-
72 R H1 24- R H E L 7-en-1-20140606
-
-
-
Guide d exercise
I n t h i s l a b, you wi l l research t h e docu mentat i o n u n d e r / u s r / s hare/doc to a n sw e r q u est ions.
-
U s e yo u r cho i ce of l e s s , ged i t , o r a b rowse r to view t h e d o c u mentation f i l e contents.
O u tcomes
M o re fa m i l i a rity, t h ro u g h practice, with t h e types of i n fo r m a t i o n that deve l o pers i n c l u d e with
-
t h e i r softwa re packages.
-
i [student@serverx doc]$ less yum - 3 * / README
I
[student@serverx doc]$
[student@serverx doc]$
-
yum list * - doc *
[student@serverx -]$
cd