KEMBAR78
Accessing The Command Line: Training | PDF | Graphical User Interfaces | Areas Of Computer Science
0% found this document useful (0 votes)
77 views73 pages

Accessing The Command Line: Training

linux

Uploaded by

Robin Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views73 pages

Accessing The Command Line: Training

linux

Uploaded by

Robin Li
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

red hat ®

®
TRAINING

C H A PT E R 1

ACCESSING THE COMMAND


LINE

Overview

Goal To log i n t o a Lin u x system a n d r u n s i m p l e c o m m a nds u s i n g


the shell.

Objectives • Use B a s h s h e l l syntax to enter c o m m a nds at a L i n ux


console.

• 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.

• Use Bash features to run c o m m a nds from a s h e l l p ro m pt


u s i n g fewer keystrokes.

Sections • Acces s i n g t h e C o m m a nd Line U s i n g t h e Loc a l Console


(a nd P ra c t i ce)

• Acces s i n g the C o m m a nd Line U s i n g the Desktop (a nd


Practice)

• Exec u t i n g C o m m a nds U s i n g t h e Bash S h e l l (and Pract ice)

Lab • Acces s i n g t h e C o m m a nd L i ne

R H 1 24- R H E L7-en-1 -20140606


-

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 -

The bash shell


console a n d run s i m p l e co m m a n d s u s i n g the s h e l l .

A command line i s a text-based i nt e rface which c a n b e used to i n put i n st r u c t i o n s to a com puter


syste m. The L i n u x co m m a n d line i s p rovided by a p ro g ra m c a l l e d t h e shell. Over t h e long h i story
-
of U N IX - l i ke syste ms, m a ny s h e l l s have been deve l o pe d . The defa u lt s h e l l for u s e rs in Red H a t
E n t e r p r i s e L i n u x is t h e G N U Bourn e-Ag a i n S h e l l (bash). B a s h is a n i m p roved vers i o n of o n e o f
t h e m ost s u ccess f u l s h e l l s used o n U N I X- l i ke syste m s , t h e B o u r n e S h e l l ( s h ) .
-

W h e n a s h e l l i s used i nteractive l y, it d i s p l ays a stri n g w h e n it i s wa iting for a c o m m a n d from t h e


user. This is ca l l ed t h e shell prompt. W h e n a reg u l a r u s e r s t a r t s a s h e l l , t h e defa u lt p ro m pt e n d s
-
with a $ c h a racter.

r-
l [ student@desktopX -]$ -

T h e $ i s re p l a ced b y a # i f t h e sh e l l is r u n n i n g a s t h e s u pe r u s e r, root. T h i s m a kes it m o re obvious


t h a t it is a s u pe r u s e r s h e l l , which h e l ps to avo i d a c c i d e nts and m i sta kes i n t h e priv i l eg e d a ccou nt. -

[ root@desktopX -]#
� - �����
I
i
I
-

U s i n g bash to execute co m m a n d s c a n be powerf u l . T h e bash s h e l l prov ides a s c r i pt i n g l a n g u a g e


t h a t can s u p port a ut o m a t i o n o f tas ks. The s h e l l h a s a d d i t i o n a l ca pa b i l ities t h a t c a n s i m p l ify o r
-
m a ke poss i b l e o p e rat i o n s t h a t a re h a rd to acco m p l i s h eff i c i e n t l y w i t h g ra p h i c a l too l s .

L>=-SJ
-�>,
Note -

The bash s h e l l is s i m i l a r i n concept to t h e com m a n d l i n e interpreter fou n d i n recent


vers i o n s of M ic rosoft W i n d ows cmd . exe, a l t h o u g h bash h a s a m o re s o p h isticated
-
scri pti n g l a n g u a g e. It i s a l so s i m i l a r to W i n d ows Powe r S h e l l i n W i n d ows 7 and W i n dows
Server 2008 R2. Mac O S X a d m i n ist rators who u s e the M a c i ntos h ' s Te r m i n a l u t i l ity
may b e p l eased to note that bash is the d e fa u l t s h e l l in Mac OS X.
-

Virtual consoles -

Users access t h e bash s h e l l t h ro u g h a terminal. A t e r m i n a l provides a key boa rd for u s e r i n put


a n d a d i s p l a y for output. O n text-based i n sta l l a t i o n s , this c a n b e t h e L i n u x m a c h i n e ' s physical
-
console, t h e h a rdware key b o a rd a n d d i s p l ay. Te r m i n a l access c a n a l so be config u red t h ro u g h
seria l ports.

Another way to a ccess a s h e l l is from a virtual console. A L i n u x m a c h i n e ' s phys i c a l conso l e


s u p po rts m u l t i p l e v i rt u a l consoles w h i c h act l i ke s e p a rate term i n a l s. Each v i rt u a l co n s o l e
s u p po rts a n i n d e p e n d e n t l o g i n sess i o n .
-

2 RH124- R H E L7-en-1 -20140606 -

-
-

Shel l basics
-

I f t h e g ra p h i c a l en vi ro n m e n t i s ava i l a b l e, it w i l l r u n on t h e first v i rt u a l conso l e i n Red H a t


E n te r p r i s e L i n u x 7. Five a d d i t i o n a l t e x t l o g i n p ro m pts a re a va i l a b l e o n conso l es t w o t h ro u g h s i x
-
(or o n e t h ro u g h f i v e if t h e g ra p h ica l e n v i ro n m e nt i s t u r n e d off). With a g ra p h ica l e n v i ro n m e n t
r u n n i n g , access a text l o g i n pro m pt o n a v i r t u a l conso l e by h o l d i ng Ct r l +Alt a n d p ress i n g a
f u n c t i o n key (F2 t h ro u g h F6). Press C t r l +Al t + F l to ret u r n to t h e fi rst virtua l conso l e a n d t h e
-
g ra p h i c a l d e s ktop.

-
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

• Arguments, w h i c h a re typica l l y t a rg ets o f t h e co m m a n d


-

T h e command is t h e n a m e o f t h e p ro g ra m t o r u n . I t may b e fo l l owed by o n e or m o re options,


w h i c h a dj u st t h e behavior of t h e c o m m a n d or w h a t it w i l l d o. O pt i o n s n o rm a l l y sta rt w i t h o n e o r
- t w o d a s hes ( - a o r - - all, for exa m p l e) to d i st i n g u i s h t h e m from a rg u ments. Com m a n d s may a l so
b e fo l l owed by one or m o re arguments, w h i c h often i n d icate a t a rg et 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 .
-

Fo r exa m p l e, t h e com m a n d l i n e u s e r mod - L mo r gan h a s a com m a n d ( use rmod), a n option


( - L), and a n a rg u m e n t (mo r g an ) . T h e effect of t h i s com m a n d i s to l o c k t h e passwo rd o n user
-
m o rg a n ' s a ccount.

To u s e a com m a n d effect i ve l y, a user needs to k n ow w h a t options and a rg u ments it ta kes a n d


-
i n w h a t o rd e r i t expects t h e m (t h e syntax o f t h e com m a n d). M ost c o m m a n d s have a - - help
option. This causes t h e com m a n d to print a description of what it does, a " u sage state m e n t " t h a t
d e s c r i b e s t h e co m m a n d ' s sy n t a x , a n d a l i st o f t h e opt i o n s it a ccepts a n d w h at t hey d o .
-

U s a g e statements m a y seem c o m p l icated a n d d i ff i c u l t to rea d . T h e y b e c o m e m u c h s i m p l e r t o


u n d e rsta n d once a user beco m e s fa m i l i a r w i t h a few b a s i c convent i o n s :
-
• S q u a re b ra c kets, [ ] , s u rro u n d o pt i o n a l ite m s .

• Anyt h i n g fol lowed by ... represents a n a r b it ra ry- l e n g t h l i st o f items o f t h a t type.


-

• M u lt i p l e items sepa rated by p i pes, I. m e a n s o n l y one of t h e m ca n b e specified.

- 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
-

• Text i n a n g l e b ra c kets, < > , represe n t s va r i a b l e data. Fo r exa m p l e, <filename> m e a n s " i n s e rt


t h e f i l e n a m e you w i s h to u s e h e re". S o m et i m es these va ria b l es a re s i m p l y written i n c a p ita l -
l etters (e.g., F I L E N A M E).

C o n s i d e r t h e f i rst usage state m e n t for t h e da t e co m m a n d :


-

r·�
[student@desktopX -]$ date
date [OPTION] . . . [+FORMAT]
! - - h elp

I -

T h i s i n d icates t h a t d at e c a n take a n o pt i o n a l l i st of o p t i o n s ( [OPTION] . . . ), fo l l owed by a n


o pt i o n a l fo r m a t stri n g , prefixed w i t h a p l u s c h a racter, +, t h a t defines how t h e c u rrent date s h o u l d -

b e d i s p l ayed ( [+FORMAT]). S i n ce b o t h o f these a re option a l , d a t e wi l l w o r k even if it i s n o t g i v e n


o p t i o n s o r a rg u me nts (it w i l l p r i n t t h e c u rrent date a n d t i m e u s i n g i t s defa u lt form at).
-

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 -

t h e co m ma n d ' s s y n t a x . T h e man - pages(7) m a n pa g e d e s c r i b e s how to interpret a l l


t h e s q u a re b rac kets, vertica l ba rs, a n d s o forth t h at u s e rs see i n SY N O PS I S o r a u s a g e
message.

W h e n a u s e r is f i n ished u s i n g t h e s h e l l a n d wa nts to q u it. t h e re a re a c o u p l e of ways to e n d t h e -

s e s s i o n . T h e e x i t com m a n d ter mi nates t h e c u rrent s h e l l s e s s i o n . A n o t h e r w a y t o f i n i s h a sess i o n


is by typ i n g C t r l+D.
-

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

-
-

Pract ice: Loca l Conso l e Access Te r m s


-

P ra ct i ce: Loca l Conso l e Access Te r m s


-

-
Quiz

M atch t h e fo l l ow i n g item s to t h e i r cou nterparts i n t h e t a b l e.


-

- 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 i n t e r p rete r t h at exe c u tes co m m a n d s typed


-
a s st r i n g s.

T h e v i s u a l c u e t h a t i n d i cates a n i nt e ract ive s h e l l


i s wa i t i n g fo r t h e u s e r to type a co m m a n d .
-

-
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 -

A n inte rfa ce t h a t p rovid es a dis p l a y f o r o u t p u t -

a n d a key boa rd fo r in p u t to a s h e l l sessio n.


-

6 RH124- R H E L 7 - e n -1-20140606 -

-
-

Solution
-

Solution
-

M atch t h e fo l l owi n g items to t h e i r cou nterparts i n t h e ta b l e.

Description Term

T h e i nt e r p rete r t h a t executes com m a n ds ty p e d Shell


a s st r i n g s .
-

- T h e v i s u a l c u e t h a t i n d i cates a n i nt e ra ct ive s h e l l Prom pt


is wa i t i n g for t h e u s e r to t y p e a co m m a nd .

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 p a rt of t h e c o m m a n d l i n e that a dj u sts t h e Option


- b e h a v i o r of a 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 .
-

A n i nte rfa ce t h a t p rov i d es a d i s p l a y for o u t p u t Te r m i n a l


-
a n d a key boa rd for i n p u t to a s h e l l sess i o n .

- R H 1 24- R H E L 7-e n-1-20140606 7

-
-

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 -

G N O M E 3 d es ktop enviro n m e n t to r u n c o m m a n d s f r o m a s h e l l p ro m pt i n a term i n a l p rog ra m .

-
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.
-

T h e GN O M E S h e l l provides the core user i nterface f u n ct i o n s for the G N O M E d esktop


e n v i ro n m e nt. The gnome - s hell a p p l ication i s h i g h l y custo m i z a b l e. By defa u lt, R H E L 7 u s e rs
u s e t h e " G N O M E C l assic" t h e m e for g nome - s hell, w h i c h i s s i m i l a r to t h e G N O M E 2 desktop -

e n v i ro n m e nt. A n o t h e r ava i l a b l e o p t i o n i s t h e " m o d e r n " G N O M E 3 theme used by t h e u pstrea m


G N O M E p roj ect. E i t h e r t h e m e c a n b e s e l ected p e rs i ste n t l y at l o g i n by s e l e c t i n g the g e a r i c o n next
to the S i g n I n button when e n t e r i n g t h e user's password. -

T h e f i rst time a new u s e r l o g s in, an i n i t i a l set u p program runs to h e l p them config u re b a s i c


acco u nt sett i n gs. The GN O M E He l p a p p l ication i s t h e n sta rted o n t h e Gett i n g Started w i t h -

GN O M E screen. This screen i n c l u d e s v i d eos and documentation to help o r i e n t n ew u s e rs to the


G N O M E 3 e n v i ro n ment. GN O M E He l p c a n be q u i c k l y sta rted by t y p i n g F1 i n g nome - shell, by
-
selecting A p p l i c a t i o n s > D o c u m e n t a t i o n > He l p , o r by r u n n i n g t h e yelp com m a n d .

8 R H 1 24- R H E L 7-en-1 -20140606 -

-
The GN O M E desktop e nvi ronment

._ Applications Ptaces en2 �») 2 Fri 08:43 Q Student User

1/4

Figure 1.1: An empty GNOME 3 desktop

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 :

top bar: The bar t h a t r u n s a l o ng t h e top of t h e s c r e e n . The top ba r p rovides t h e A p p l icat i o n s


and Places menus, a nd cont ro l s for vo l u m e, netwo rking , c a l e nd a r a ccess, a nd selecting
between keybo a rd input methods (if t h e re are m ore t h a n o n e config u re d ) . U nd e r t h e m e n u for
t h e u s er's nam e are opti ons to a d just a c c o u nt sett i ngs, l o c k t h e screen, switch u sers, log out of
t h e syst e m , o r s h u t it d own.

Applications menu: T h i s menu on t h e t o p ba r p rovides a way to start a p p l ications, categorized


into s u bm enus. T h e Activities Overview c a n a lso be started fro m t h i s m e n u .

Places menu: T h i s menu to t h e r i g ht o f t h e A p p l icat i o n s menu p rov i d e s q u i c k a ccess t h r o u g h


a g ra p h i c a l f i l e m a n a g e r t o i m po rtant m enus i n t h e use r's home d i re cto ry, to I , a nd to exports
and file s h a res o n the n etwork.

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

The m e s s a g e t ray c a n b e opened to review t h ese notifications by c l i c k i n g t h e i n d icator o r


typing Supe r+M. T h e m e s s a g e t ray c a n be c l osed by typ i n g either Esc o r Supe r+M a g a i n .

Activities Overview: T h i s i s a s p e c i a l m o d e t h a t h e l p s a user organ ize w i n d ows a n d start


a p p l ications. The Activit ies Overview c a n b e started by selecting A p p l i ca t i o n s> Activities
Overview. A faster way is to press the S u p e r key (somet i m es ca l led the Windows key), fou n d
n e a r t h e lowe r l eft c o r n e r of a n I B M PC 1 04/1 0 5- key keyboard. The t h ree m a i n areas o f t h e
A c t i v i t i e s Overview are t h e d a s h o n t h e l eft s i d e of t h e s c r e e n , t h e w i n dows overview i n t h e
center o f t h e s c reen , a n d t h e workspace selector o n t h e right side of t h e screen. T h e A ctivities
Overview c a n b e exited by p ressing t h e Esc key.

dash: T h is is a confi g u ra b l e l i st of icons of t h e u s e r ' s favorite a p p l i ca t i o n s, a p p l ications which


a re c u r rently r u n n i n g , a n d a g r i d button w h i c h c a n b e used to select a r b it ra ry a p p l i cations.
A p p l ications c a n b e started by clicking o n o n e of the icons o r by u s i n g t h e g r i d button to find a
less c o m m o n l y used a p p l ication. T h e d a s h is a l s o sometimes cal l ed t h e dock.

Figure 1 .2: The GNOME 3 Activities Overview

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
-

T h e re a re t h ree m e t h o d s for switc h i n g between workspa ces. O n e method is t o c l i c k t h e i n d icator


i n t h e r i g ht corner of t h e window l ist a nd s e l ect t h e d e s i re d works p a ce. A n o t h e r, p e r h a p s t h e
-
fastest, is t o type C t r l+Alt + U pA r r ow or Ct r l+Al t + DownAr row to switch betwe e n works paces
s e q u e n t i a l l y. A t h i rd i s to switch to the Act i v i t i e s Overview and c l i c k the desired workspace.

-
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.

• O n a n e m pty des ktop, r i g ht-c l i c k , or press t h e Menu key, a n d s e l ect O p e n in Te r m i n a l from t h e


- context m e n u t h a t a p pears.

• Fro m t h e A c t i v i t i e s O v e r v i e w , s e l ect Te r m i n a l from the d a s h (e it h e r from t h e favorites a rea o r


- b y f i n d i n g it w i t h e i t h e r t h e g r i d b u tton ( i n s i d e U t i l it i e s g ro u p i n g ) o r t h e search f i e l d a t t h e t o p
o f t h e w i n d ows overvi ew).

-
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.
-

Locking the screen or logging out


-
Loc k i n g t h e scree n , or l o g g i n g out e n t i re l y, can be d o n e from t h e m e n u for t h e u s e r ' s n a m e o n
t h e fa r r i g h t s i d e o f t h e t o p b a r.

To l o c k t h e scree n , s e l ect (Us e r)> Lock or type Ct r l+Al t + L. The screen w i l l l o c k if t h e g ra p h i c a l


-
s ess i o n i s i d l e for a few m i n utes.

A lock s c r e e n c u rta i n w i l l a p p e a r that shows the syst e m t i m e and the n a m e of the l o g g e d - i n


-
user. To u n l o c k t h e s c r e e n , press E n t e r or Space to ra ise t h e l oc k s c r e e n c u rta i n , t h e n enter t h e
u s e r ' s password o n t h e l o c k s c reen.
-
To log out and end the c u rrent g ra p h i c a l l o g i n sess ion, s e l ect (Us e r)> Log Out from the top b a r. A
d i a l o g w i n d ow w i l l a p pe a r, g i v i n g t h e o pt i o n to C a n c e l t h e l o g out w i t h i n 60 seco n d s , o r co nfi r m
-
the Log Out action.

Powering off or re booting the system


-
To s h ut down t h e syst e m , s e l ect (Us e r)> Power Off from t h e top b a r or type Ct r l+Al t +Del. I n
t h e d i a l o g t h a t a p pea rs, t h e u s e r c a n c h oose t o Power Off, Restart t h e m a c h i n e, o r C a n c e l t h e
o p e rat i o n . I f t h e u s e r d o e s n o t m a ke a choice i n t h i s d i a l o g , t h e syste m w i l l automatica l l y s h ut
-
down after 60 seco n d s .

- R H124-R H E L 7-en-1 -20140606 11

-
-

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
-

G N O M E H e l p: Getting Started with GNOME


· yelp help:gnome - help/ge t t ing - s t a r t ed
-

12 R H 1 24- R H E L7 - e n -1 -20140606 -
-

Practice: The G N O M E 3 Desktop Enviro n m e nt

P ra ct i ce: T h e G N O M E 3 Des ktop E nvi ro n ment


-

Guided exercise
-

I n t h i s l a b , you w i l l l o g i n t h ro u g h t h e g ra p h i c a l d i s p l ay m a na g e r as a reg u l a r u s e r to become


fa m i l i a r w i t h t h e G N O M E C l assic d e s ktop e n v i ro n m e n t provided by G N O M E 3.
-

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).

Ta ke c a re to keep stra i g h t w h i c h v i rt u a l m a c h i n e a n exerc i se wants you to u se.


-

Do each of the fo l l o w i n g tasks on the d e s k t o pX m a c h i n e. M a r k each t a s k a s it is co m p l eted.


-

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 .1 . A t t h e G N O M E l o g i n scree n , c l i c k t h e s t ude nt u s e r account. Enter s t udent


when p ro m pted for t h e password.

-
D 1 .2. C l i c k Sign I n o n ce the password has been typed i n .

D 2. C h a n g e t h e password f o r s t u d e n t f r o m passwo r d to 55Tu r n K3y.


-
0 2.1 . T h e s i m p l est a p proac h is t o o p e n G N O M E Ter m i n a l a n d u s e t h e pas swd
com m a n d at t h e s h e l l p ro m pt.

- 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 2 . 3 . I n t h e ter mi n a l w i n d o w t h a t a p pea rs, type pas swd a t t h e s h e l l prom pt. Fo l l ow


- the i n structions p rovided by the progra m to c h a n g e the s t ud e n t password from
s t u de n t to 55Tu r n K3y.

-
D 3. Log o u t .

D 3.1 . S e l ect t h e student > Log O u t m e n u item.


-
0 3.2. C l i c k t h e Log Out button i n the confirmation w i n d ow that a p pears.

D 4. Log back in a s s t u d e n t with t h e new p a ssword of 55Tu r n K3y.


-

D 4.1 . A t t h e G N O M E l og i n scree n , c l i c k t h e s t u d e n t user a c c o u n t . E n t e r 55Tu r n K3y


w h e n p ro m pted for the password.
-

- 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 5.1 . S e l ect t h e student > Lock m e n u item.

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 .
-

D 6.2. I n the Password fie l d , en ter 55Tu r n K3y a s t h e password.

D 6.3. C l ic k the Un l o c k button. T h e G N O M E d esktop s h o u l d rea p p e a r. -

D 7. Dete r m i n e how to s h u t down d e s k t opX from t h e g ra p h ica l i nte rface, b u t C a n ce l the


o p e rat i o n w i t h o u t s h utt i n g down t h e syst e m .
-

D 7.1 . S e lect t h e s t u d e n t > Powe r Off m e n u item.

D 7.2 . C l i c k t h e Cancel button i n t h e confi r m a t i o n screen that a p p e a rs. -

14 R H 1 24- R H E L 7 - e n -1-20140606
-

-
-

Executing Commands Using the Bash Shell


-

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.
-

Basic command syntax


The GN U B o u rne-A g a i n S h e l l (bash) is a p rog ra m t h a t i nt e r p rets co m m a n d s typed in by t h e
-
user. E a c h st r i n g typed i nto t h e s h e l l can h a v e u p to t h re e pa rts: t h e c o m m a n d , o pt i o n s (that
beg i n w i t h a - o r --), and a rg u m e nts. Each word typed i nto t h e s h e l l i s separated from each
ot h e r w i t h s paces. C o m m a n d s a re the n a m es of p ro g ra m s t h a t a re i n sta l l e d on t h e syst e m . Each
-
c o m m a n d h a s its own options and a rg u m e nts.

T h e E n t e r key is p ressed when a user is rea dy to execute a c o m m a n d . Each co m m a n d is typed


-
on a s e p a rate l i ne and the o u t p u t from each com m a n d d i s p l ays before the s he l l d i s p l ays a
p ro m pt. If a user wants to type m o re t h a n o n e com m a n d on a s i n g l e l i ne, a semico l o n , ; , can
b e used a s a com m a n d s epa rator. A semico l o n i s a member of a c l ass of c h a racters c a l l e d
-
metacharacters t h a t h a s spec i a l m e a n i ngs for ba sh .

-
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 .

[student@desktopX -]$ date


Sat Apr 5 08 : 13 : 50 PDT 2014
[ student@desktopX -]$ date +%R
-

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 -]$ pas swd


Changing password for user student .
Changing password for student .
-

(current ) UNIX password : old_password


New password : new_password
Retype new password : new_password
-

passwd : all authentication tokens updated successfully .


-

L i n u x does not req u i re f i l e n a m e exte n s i o n s to c l assify f i l es by type. T h e file com m a n d s ca n s


t h e b eg i n n i n g o f a f i l e ' s contents a n d d i s p l ays w h a t type it i s . T h e f i l e s t o be c l assified a re passed
- a s a rg u m en ts to t h e co m m a n d .

[student@desktopX -]$
I
I file /etc/passwd

- R H 1 24- R H E L 7-en-1 -20140606 15

-
-

C h a pte r 1. Access i n g t h e C o m m a n d L i n e

/etc/passwd : ASCII text


-

[ student@desktopX -]$ file /bin/passwd


/bin/passwd : setuid ELF 64- bit LSB shared obj ect , x86- 64, version 1
(SYSV) , dynamically linked ( uses shared libs ) , for GNU/Linux 2 . 6 . 32,
BuildID [ sha1] =0x91a7160a019b7f5f754264d920e257522c5bce67, stripped
-

[ student@desktopX -]$ file /home


/home : directory -

T h e head a n d t ail co m m a n d s d i s p l ay t h e beg i n n i n g a n d e n d of a f i l e res pective l y. By defa u lt ,


-
t h ese com m a n d s d i s p l a y 1 0 l i n es, b u t t h ey bot h have a - n o p t i o n t h a t a l l ows a d iffe rent n u m b e r
o f l i nes to be specified. T h e f i l e to d i s p l a y i s passed a s a n a rg u m e nt t o t h ese com m a nds.

[student@desktopX -]$ head /etc/passwd


-

root : x : 0 : 0 : root : /root : /bin/bash


,�--

bin : x : l : l : bin : /bin : /sbin/nologin


daemon : x : 2 : 2 : daemon : /sbin : /sbin/nologin
adm : x : 3 : 4 : adm : /var/adm : /sbin/nologin
-

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
-

mail : x : 8 : 12 : mail : /var/spool/mail : /sbin/nologin


operator : x : 11 : 0 : operator : /root : /sbin/nologin
[ student@desktopX -]$ t ail -n 3 /et c/passwd
-

gdm : x : 42 : 42 : : /var/lib/gdm : /sbin/nologin


gnome-initial- setup : x : 993 : 991 : : /run/gnome-initial-setup/ : /sbin/nologin
tcpdump : x : 72 : 72 : : / : /sbin/nologin -

�-------"----�----'

T h e we c o m m a n d counts l i nes, words, a n d c h a racters i n a f i l e. I t c a n ta ke a - 1, - w, or - c o p t i o n -


to d i s p l a y o n l y t h e l i n es, w o r d s , o r c h a ra cte rs, res pective l y.

�-�student@desk :�pX -]$


39 70 2005 /etc/passwd
wc /etc/passwd

[ 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 -

Tab com pletion -

Tab completion a l l ows a u s e r to q u i c k l y c o m p l ete co m m a n d s or fi l e na mes once t h ey have typed


e n o u g h at the prompt to make it u n iq u e. I f t h e c h a racters typed a re not u n i q ue, press i n g the Tab
key twice d i s p lays a l l co m m a n d s that beg i n with t h e c h a racters a l ready typed. -

[student@desktopX -]$ pas<Tab><Tab>


passwd paste pasuspender
[student@desktopX -]$ pass<Tab>
-

[ student@desktopX -]$ pas swd


Changing password for user student .
Changing password for student .
( current ) UNIX password :
-

-
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
-

Ty pe a d d it i o n a l c h a ra cters u n t i l t h e n a m e i s u n i q ue, t h e n use t a b c o m p l et i o n to f i n i s h off the


com m a n d l i n e.

[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 .

[ root@desktopX ]# useradd - - <Tab><Tab>


-

- - base-dir - -groups - - no-log-init - - shell


- -comment - -help - - non- unique - - skel
-

- -create- home - - home-dir - - no- user-group - - system


-

- -defaults - -inactive - -password - - uid


- -expiredate - - key - - root - - user -group
- -gid - -no-create-home - - selinux- user
[ root@desktopX -]# use radd - -
-

Com mand history


-
The h i s t o r y com m a n d d i s p l ays a l i st of p revio u s l y executed com m a n d s p refixed w i t h a
com m a n d n u m be r.

The exc l a m a t i o n p o i n t c h a racter, ! , is a meta c h a racter t h a t is used to expa n d prev i o u s


-
com m a n d s w i t h o u t h a v i n g to rety pe t h e m . ! number expa n d s to t h e c o m m a n d matc h i n g the
n u m be r s p e c ified. ! s t ring expa n d s to t h e most recent com m a n d t h a t beg i n s w i t h t h e st r i n g
specified.

[ student@desktopX -]$ his t o ry


-

. . . 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
-

aliases hosts . deny qemu-ga


- . . . Output omitted . . .
The a r row keys c a n be used to n a v i g ate t h ro u g h prev i o u s c o m m a n d l i n e s i n t h e s h e l l ' s h isto ry. U p
-
Ar row e d i t s t h e p rev i o u s com m a n d i n t h e h i story l i st. Down A r r ow e d i t s t h e n ext c o m m a n d i n
t h e h istory l ist. U s e t h i s key w h e n t h e U p Ar r ow has b e e n p ressed t o o m a ny t i m es. L e f t A r r ow
a n d Rig h t A r row m ove t h e c u rsor l eft a n d r i g h t i n t h e cu rrent c o m m a n d l i n e b e i n g e d ited.
-

- 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
-

T h e Esc+ . k e y co m b i n at i o n causes t h e s h e l l to c o p y t h e l a st word o f t h e p revious c o m m a n d o n


t h e c u rrent c o m m a n d l i n e w h e re t h e c u rsor i s . I f u s e d repeate d l y, it wi l l cont i n u e to g o t h ro u g h
-
e a r l i e r c o m m a n ds.

Editing the com mand line -

W h e n used i ntera ctive l y, bash h a s a com m a n d l i n e- e d i t i n g featu re. T h i s a l l ows t h e u s e r to use


text editor c o m m a n d s to move a ro u n d wit h i n and m o d i fy the c u rrent com m a n d bei n g typed.
-
U s i n g t h e a r row keys to m ove wit h i n the c u rre n t co m m a n d and to step t h ro u g h t h e com m a n d
h i story w a s i n t ro d u ce d e a r l i e r i n t h i s session. M o re powerfu l e d i t i n g com m a n d s a re i nt rod u ced i n
t h e fo l l ow i n g t a b l e.
-

Useful command line -editing shortcuts

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 .

T h e re a re severa l ot h e r c o m m a n d l i ne-e d i t i n g c o m m a nds ava i l a b l e, but t h ese a re t h e m o s t usef u l


-
c o m m a n d s f o r beg i n n i n g users. T h e ot h e r com m a n d s can be fo u n d i n t h e bash(1 ) m a n page.

R References -

bash(1 ) , date(1 ) , file(1 ) , head(1 ), passwd( 1) , t ail(1 ), a n d wc (1 ) m a n p a g es

18 R H1 24- R H E L 7 - e n -1 -20140606 -

-
-

P ractice: B a s h C o m m a n d s a n d Key b o a rd S h o rtcuts


-

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
-

! number ! s t ring C t r l+ L e f t A r r ow C t r l+a

l��I
-

Ct rl+k h i s t o ry
-

Descript ion S h e l l com m a n d

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 .
-

Re-execute a s p e c i f i c co m m a n d i n t h e h isto ry l ist.


-

J u m p to the beg i n n i n g fo r the co m m a n d l i n e.


-

- 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 .

20 R H1 24- R H EL7-e n -1-20140606


-

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

J u m p to t h e b eg i n n i n g of t h e p revi o u s word o n C t r l + L e f t A r row


t h e co m m a n d l i ne.
-

- 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.
-

Re-ex e c u t e a rec e n t co m m a n d by m a tc h i n g t h e ! s t ring


... c o 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 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.
-

Re-ex e c u t e a s p e c i f i c co m m a n d i n t h e h i st o ry l ist. ! number


-

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
-

Perfor mance checklist -

I n t h i s l a b, you w i l l use t h e Bash s he l l to effic i e n t l y execute com m a n d s u s i n g s h e l l


meta c h a racters. -

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.

• C h a n g e the pa ssword of the s t u d e n t u s e r to T3s t 1ngT1me. -

• Execute co m m a n d s used to ident ify f i l e types a n d d i s p l ay parts of text f i l es .


-
Before you begin ...
Perform t h e fo l l o w i n g steps o n desktopX.
-
1. Log i nto yo u r d e s k t opX syste m ' s g ra p h i c a l l o g i n screen a s s t ud e n t .

2. O p e n a t e r m i n a l w i n dow that w i l l p rovide a bash prom pt.


-

3. C h a n g e s t uden t ' s password to T3 s t ln gT1me.

4. D i s p l ay t h e c u rre nt t i m e a n d date. -

5. D i s p l a y the c u rrent t i m e in t h e fo l l ow i n g fo r m a t : H H : M M : S S A/PM. H i nt: The format stri ng


that d i s p l ays t h a t output i s %r. -

6. What k i n d of f i l e i s / u s r /bin/clean - binary - files? Is i t rea d a b l e by h u m a ns?


-
7. U se t h e we com m a n d a n d bash s h o rtcuts to d i s p l ay t h e size of
/ u s r /bin/clean - binary - files.
-
8. D i s p l a y the fi rst 1 0 l i nes of / u s r /bin/clean - binary - files.

9. D i s p l a y t h e l a st 10 l i nes at the botto m of t h e / u s r /bin/clean - binary - files f i l e.


-

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.

12. Use bash h i story to d i s p l ay j u st the t i me. -

13. F i n i s h yo u r sess i o n with t h e bash s h e l l .


-

22 R H 1 24- R H E L 7 - e n -1 -20140606 -

-
-

Sol ution
-

Solution
-

I n t h i s l a b, you w i l l use t h e B a s h s h e l l to effic i e n t l y execute co m m a n d s u s i n g s h e l l


m eta c h a ra cters.
-

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.

- • C h a n g e t h e password of t h e s t u d e n t user to T3s t 1 n gT1me.

• Execute com m a n d s used to i d e n t i fy file types and d is p l a y pa rts of text f i l es.


-
Before you begin ...
Perform the fo l lo w i n g steps o n d e s ktopX.
-
1. Log i nto you r d e s k t opX syste m ' s g ra p h i c a l l o g i n screen as st u d e n t .

2. O p e n a termi n a l w i n d ow t h a t wi l l prov i d e a bash p ro m pt.


-

S e l ect A p p l i c a t i o n s > Ut i l it i e s > Te r m i n a l .

- 3. C h a n g e s t ud e n t 's pa sswo rd to T3s t 1 ngT1me.

Use the pas swd c o m m a n d to c h a nge the password. Be s u re to p rov i d e the o r i g i n a l


- pa ssword, s t u d e n t , fi rst.

[student@desktopX - ] $ passwd
Changing password for user student .
Changing password for student .
-

( current ) UNIX password : st udent


New password : T3s t lngT1me
Retype new password : T3s t ln gT1me
passwd : all authentication tokens updated successfully .
-

- - --------- -----�

-
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
�----� ---�-- ----- - ·--- �--� ----�------ ----��-

- 5. D i s p l a y the c u rrent time in the fo l l ow i n g fo rmat: H H : M M : S S A/PM . H i nt: The fo r m a t st r i n g


t h a t d i s p l ays t h a t o u t p u t i s %r.

- S pecify t h e +%r argument to date.

[ student@desktopX
10 : 14 : 07 AM
- ] $ d a t e +%r
,_.

6. W h a t kind of file i s / u s r / bin/clean - binary - files? I s it rea d a b l e by h u m a n s?


-

- 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 - ] $ file / u s r/bin/clean - binary - files


/usr/bin/clean- binary-files : POSIX shell script , ASCII text executable
-

7. U s e t h e we com m a n d a n d bash s h o rtcuts to d i s p l ay t h e s i z e of


/ u s r / bin/clean - binary - files.
-
The eas iest s h o rtcut to use is Esc+ . to re use the a rg u m e n t from t h e p revious co m ma n d.

[student@desktopX - ] $ w c <Esc> .
[student@desktopX - ] $ wc / u s r/bin/clean - binary - files
-

594 1780 13220 /usr/bin/clean - binary-files


-

8. D i s p l a y t h e fi rst 1 0 l i nes of / 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? -

[student@desktopx - ] $ head <Esc> .


[student@desktopX - ] $ head / u s r/bin/clean - binary - files
# ! /bin/sh
-

#
# 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 $

# Import j ava functions


-

[ -r "/usr/share/j ava- utils/j ava-functions" ] \ -

9. D i s p l ay t h e l a st 1 0 l i n es at t h e bottom of t h e / u s r /bin/clean - binary - files fi l e.

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
.

1 0. Repeat t h e p revi o us co m m a n d , b u t use t h e - n 20 o p t i o n to d i s p l ay t h e l a s t 20 l i n es i n t h e


f i l e. U s e c o m m a n d l i n e e d i t i n g to accom p l i s h t h i s w i t h a m i n i m a l a m o u nt o f keystrokes.
-

U p Ar row d i s p l ays the p revio u s co m m a n d . Ct r l +a m a kes the c u rsor j u m p to the beg i n n i n g


o f t h e l i ne. C t r l+Right Ar row j u m ps t o t h e next word, t h e n add t h e - n 2 0 o pt i o n a n d h i t
E n t e r t o execute t h e co m m a n d . -

[student@desktopX - ] $
. . . Output omitted . . .
t ail - n 20 / u s r/bin/clean - binary - files
-

11. E x e c u t e t h e d at e 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 t h e c u rrent date a n d t i m e. -

I [ student@desktopX - ] $ date
-

24 R H1 24- R H E L 7-en-1-20140606 -

-
-

I Thu Apr 3 10 : 48 : 30 PDT 2014


Solution
-

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.

D i s p l a y t h e l ist o f p revious co m m a n d s w i t h t h e h i s t o r y com m a n d to i d e n t ify t h e specific


-
d a t e command to be executed. Execute t h e com m a n d w i t h t h e ! number h istory com m a n d .

[ student@desktopX ]$
date +%X
! 44

10 : 49 : 56 AM
- -

13. Fi n i s h yo u r session with t h e bash s h e l l .

-
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
-

- R H1 24- R H E L 7-en-1 -20140606 25

-
-

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
-

Acces s i n g t h e Co m m a n d L i n e Using t h e Loca l Console


U s e t h e p hys i ca l console to view o u t p u t and i n put com m a n d s w i t h correct syntax u s i n g
-
t h e bash s h e l l .

Acces s i n g t h e Co m ma n d L i n e U s i n g t h e Des ktop


-
U s e the G N O M E g ra p h i c a l e n v i ro n m e nt to l a u nc h a p p l i cations, e s p e c i a l l y the g ra p h ica l
term i n a l prog ra m .

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 -

U s e t h e t a b co m p l et i o n , co m m a n d h i sto ry, a n d com m a n d l in e-ed i t i n g featu res o f t h e


B a s h s h e l l to execute co m m a n d s m o re effi c i e n t l y.
-

26 R H1 24- R H E L 7-en-1-20140606 -

-
red h at ®

®
TRAINING

C H A PT E R 2

MANAGING FILES F ROM THE


COMMAND LINE

Overview

Goal To copy, move, create, delete, and org a n ize files w h i l e


wo r k i n g from t h e B a s h s h e l l prompt.

Objectives • Identify t h e p u rpose fo r i m portant di rectories on a L i n ux


system.

• Specify files u s i n g a b s o l ute and re lative path n a mes.

• C reate, copy, move, and remove files and directories u s i n g


command- l i ne uti l it ies.

• Match o n e or more f i l e n a mes using shell expa n s i o n as


a rg u m e nts to s h e l l com ma nds.

Sections • The L i n ux F i l e System H i e ra rchy (a nd Pra c t i ce)

• Locat ing Files by N a m e (and Practice)

• M a n a g i n g Files U s i n g C o m m a nd- L i n e Too l s (and P ra c t ice)

• M atch i n g F i l e N a m e s U s i n g Pat h N am e Exp a n s i o n (and


Pract ice)

Lab • M a n a g i n g F iles with S h e l l Expa n s i o n

R H 1 24- R H E L7-en-1 -20140606 27


-

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.
-

The file system hierarchy


A l l f i l es o n a L i n u x syste m a re stored o n f i l e systems w h i c h a re o rg a n i zed into a s i n g l e inverted -
t ree of d i recto ries, k n o w n as a file system hierarchy. T h i s t re e is i nverted because t h e root of t h e
t ree i s s a i d t o be at t h e top o f t h e h i e ra rchy, a n d t h e b ra n c h e s of d i rectories a n d s u b d i rectories
st retc h below t h e root. -

• -

I
-

• • • • • • • • • • •
bin
........ b o ot dev etc home root ru n sbin tmp usr va r
-

I I . . ..

•• . . . �
·.

....
·... . .

I , .. .... I

• • •
..
. tmp -

a l ice bob eve bin loca l sbin tmp .. ... .. . . .....

Figure 2. 1 : Significant file system directories in Red Hat Enterprise Linux 7 -

T h e d i rectory I is t h e root d i rectory at t h e top of t h e f i l e syste m h i e ra rchy. T h e I c h a racter


i s a l so used a s a directory separator i n f i l e names. Fo r exa m p l e, i f e t c is a s u b d i rectory of the -
I d i recto ry, we co u l d ca l l t h a t d i rectory /etc. L i kewise, i f t h e /etc d i rectory conta i n e d a file
n a m ed issue, we co u l d refe r to t h a t file a s /etc/issue.
-
S u b d i rectories of I a re used for sta n d a rd ized pu rposes to o rg a n ize f i l e s by type a n d p u r pose.
This m a kes it e a s i e r to find f i l es. Fo r exa m p le, i n the root d i recto ry, the s u b d i rectory /boot is
used for sto r i n g f i l es needed to boot the system. -

' ,......._ ,
Note
� -

T h e fo l l ow i n g t e r m s a re e n c o u ntered i n descri b i n g f i l e syste m d i rectory contents:


- static i s content that rema i n s u n c h a ng e d u n t i l ex p l i c i t l y e d ited o r reconfi g u re d .
-
- dynamic o r variable i s c o n t e n t typica l l y m o d i f i e d o r a p pe n d e d by active p rocesses.
- persistent i s content, partic u l a r l y confi g u ra t i o n sett i n g s, that re m a i n after a reboot.
- runtime i s p rocess- o r syste m-specific content o r att ri butes c l e a red d u r i n g reboot.
-

T h e f o l l o w i n g t a b l e l ists some of the most i m porta nt d i rectories o n t h e system by n a m e a n d


-
p u rpose.

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
-

Important Red H a t Enterprise Linux directories

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.)
-

/ home Home directories w h e re reg u l a r users sto re t h e i r p e rs o n a l data a n d


confi g u ration f i l es.
-
/ root Home d i rectory for t h e a d m i n istrative s u pe r u s e r, root.
/tmp A wo r ld -writa b l e s pace for tem pora ry f i l es. F i l es w h i c h a re m o re t h a n 1 0
d a y s o l d a re d e l eted f r o m t h i s d i rectory a utomatica l l y. A n o t h e r tem pora ry
d i rectory exists, /var / t m p , i n w h i c h f i l es t h a t have n ot been accessed,
c h a n g e d , o r mod ified i n m o re than 3 0 days a re d e l eted a utomatica l l y.
-
/boot Files needed i n order to sta rt t h e boot p rocess.
/dev Conta i n s spec i a l device files which a re used by the syste m to access
-
h a rdwa re.

-
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.

- In o l d e r vers i o n s of Red H a t Enterprise L i n u x , these were d i s t i n c t d i rectories conta i n i n g


d iffe rent sets of f i l es . I n R H E L 7, t h e d i rector i es i n I a re sym b o l i c l i n ks to t h e matc h i n g
d i rectories i n / u s r .
-

- R H1 24- R H E L 7-en-1 -20140606 29

-
-

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

F i l esyste m H i e ra rchy Sta n d a rd


htt p : //www. pa t h n a m e.com/fhs

30 R H1 24- R H E L7-en-1-20140606 -

-
-

P ract ice: F i l e System H i e ra rchy


-

P ra ct i ce: F i l e Syste m H i e ra rc hy
-

-
Quiz

Match t h e fo l l ow i n g items to t h e i r cou nte r p a rts i n t h e t a b l e.


-

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 sta t i c , p e rs i st e n t syst e m


c o n f i g u ra t i o n d a t a .

T h i s i s t h e syst e m ' s root d i recto ry.

U s e r h o m e d i recto r i es a re l o cated u n d e r t h i s d i recto ry.


-

T h i s i s t h e root a c co u n t ' s h o m e d i recto ry.


-

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.

System a d m i n i st ra t i o n b i n a r i e s , fo r root u s e , a re h e re.

Te m po ra ry f i l e s a re sto red 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
-

D i rectory p u rpose Loca t i o n -

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.
-

C o n ta i n s i n sta l l e d softwa re p rog ra m s a n d l i b ra r i es .


-

32 R H 1 24- R H E L7 -e n -1 -20140606 -

-
-

Solution
-

Solution
-

Ma tc h t h e fo l l ow i n g items to t h e i r c o u nte r p a rts i n t h e t a b l e.

Di recto r y p u rpose Location

T h i s d i rectory conta i n s sta t i c , p e r s i s t e n t syst e m /etc


co n fi g u ra t i o n d a t a .
-

- T h i s i s t h e syst e m ' s root d i recto ry. I

-
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

T h i s d i rectory conta i n s d y n a m i c c o n f i g u ra t i o n d a t a , /va r


s u c h a s F T P a n d websites.
-

Re g u l a r u s e r co 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. /u s r/b i n


-

- Syste m a d m i n i st ra t i o n b i n a r i e s , fo r root use, a re h e re. /u s r/s b i n

- Te m p o ra ry fi l es a re sto red h e re. /t m p

-
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.
-

C o n t a i n s i n sta l l e d softwa re p ro g ra m s a n d l i b ra r i es. /u s r


-

- 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)

The path of a f i l e o r d i rectory specifies its u n i q u e f i l e system location. Fo l l o w i n g a f i l e path


traverses o n e o r more n a med s u b d i rectories, d e l i m ited by a fo rward s l a s h ( / ) , u nt i l the
dest i nation is reached . Sta n d a rd file behavior definitions apply to d i rectories (a lso ca l l ed folders)
t h e s a m e as other f i l e types.

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.

W h e n a u s e r logs in a n d opens a com m a n d w i n d ow, t h e i n i t i a l l ocation i s n o r m a l l y t h e user's


h o m e d i recto ry. System p rocesses also have an i n i t i a l d i recto ry. Users and p rocesses navigate to
other d i rectories as needed; the terms working directory o r current working directory refer to t h e i r
current location.

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.

Fo r sta n d a rd L i n u x file systems, t h e path name of a file, i n c l u d i n g a l l I c h a racters, m a y b e n o


-
m o re t h a n 4 0 9 5 bytes l o n g . Each c o m p o n e n t of t h e path n a m e separated by I c h a racters may
b e no m o re t h a n 255 bytes l o n g . File n a mes c a n u s e a n y U T F-8 encoded U n icode c h a racter
except I and t h e NUL c h a racter. (ASC I I c h a racters req u i re one byte; ot h e r Latin, Greek, H e b rew,
-
or Cyri l l ic c h a racters take two bytes; re ma i n i n g c h a racters in t h e U n icode Basic M u l t i l i n g u a l
P l a n e take t h ree; a n d n o c h a racter w i l l take m o re t h a n fo u r bytes.)
-
L i n u x f i l e systems- i n c l u d i n g , but not l i m ited to, ext4, X FS , BTR FS, G FS2, and G l u st e r FS-are case­
sensit ive. C reat i n g FileCase . txt and filecas e . txt in the same d i rectory res u l t s in two
u n i q u e f i l es. A l t h o u g h m a n y n o n - L i n u x file syste m s a re s u p p o rted in L i n u x , each has u n i q u e
-
f i l e n a m i n g r u l es. Fo r exa m p l e, t h e u bi q u itous V FAT fi l e system i s n o t case-sensit ive a n d a l l ows
o n l y one of the two exa m p l e fi l es to be created. H oweve r, V FAT, a l o n g w i t h M i c rosoft 's N T FS a n d
A p p l e ' s H FS+, h a s case preserving behavior. A l t h o u g h t h ese f i l e systems a re not case-s e n s i t ive
-
(e nforced p r i m a r i l y to s u p port backwa rd compat i b i l ity), t h ey d o d i s p l a y f i l e n a m e s w i t h t h e
o r ig i n a l ca p ita l ization used w h e n t h e fi l e w a s c reated.
-

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 -]$
-

- U se t h e c d co m m a n d t o c h a n g e d i rectories. W i t h a wo r k i n g d i rectory o f / home/ s t u d e n t ,


relat ive p a t h syntax is s h o rtest to rea c h t h e Videos s u b d i recto ry. The Docume n t s s u b d i rectory
i s then rea c h e d u s i n g a b s o l ute path syntax.

[student@desktopX -]$ cd Videos


-

[student@desktopX Videos] $ pwd


/home/student/Videos
[student@desktopX Videos] $ cd /home/student /Docume n t s
-

[student@desktopX Documents]$ pwd


/home/student/Documents
[student@desktopX Documents]$ c d
[student@desktopX -]$ pwd
-

/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.

- R H 1 24- R H E L7-e n-1-20140606 35

-
-

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
-

T h e t o u c h c o m m a n d n o r m a l l y u pd ates a f i l e ' s t i mesta m p to t h e c u rre nt d a t e a n d t i m e w i t h o u t


o t h e rwise modifying it. T h i s i s usef u l for c reat i n g e m pty f i l es, w h i c h can b e used for p ra ct ice,
-
s i n ce "to u c h i n g " a f i l e n a m e that does not exist causes the f i l e to be created. U s i n g t o u c h ,
p ract ice f i l e s a re c reated i n t h e Docume n t s a n d Videos s u b d i rectories.

[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

drwxr-xr - x . 2 student student 4096 Feb 7 14 : 02 Desktop


-

drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Documents


drwxr-xr - x . 3 student student 4096 Jan 9 15 : 00 Downloads
drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Music
drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Pictures
-

drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Public


drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Templates
drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Videos
[student@desktopX - ] $ l s - a
-

total 15
d rwx - - - - - - . 16 student student 4096 Feb 8 16 : 15
drwxr-xr - x . 6 root root 4096 Feb 8 16 : 13
-

- rw- - - - - - - . 1 student student 22664 Feb 8 00 : 37 . bash_history


- rw- r- - r- - . 1 student student 18 Jul 9 2013 . bash_logout
- rw- r- - r - - . 1 student student 176 Jul 9 2013 . bash_profile
- rw- r - - r- - . 1 student student 124 Jul 9 2013 . bashrc
-

drwxr-xr - x . 4 student student 4096 Jan 20 14 : 02 . cache


drwxr -xr - x . 8 student student 4096 Feb 5 11 : 45 . config
drwxr-xr - x . 2 student student 4096 Feb 7 14 : 02 Desktop
drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Documents
-

drwxr -xr-x . 3 student student 4096 Jan 25 20 : 48 Downloads


drwxr-xr-x . 11 student student 4096 Feb 6 13 : 07 . gnome2
drwx- - - - - - . 2 student student 4096 Jan 20 14 : 02 . gnome2_private
-

- rw- - - - - - - . 1 student student 15190 Feb 8 09 : 49 . ICEauthority


drwxr-xr - x . 3 student student 4096 Jan 9 15 : 00 . local
drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Music
drwxr-xr - x . 2 student student 4096 Jan 9 15 : 00 Pictures
-

d rwxr-xr-x . 2 student student 4096 Jan 9 15 : 00 Public


drwxr-xr-x . 2 student student 4096 Jan 9 15 : 00 Templates
drwxr -xr-x . 2 student student 4096 Jan 9 15 : 00 Videos
-

[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
-

F i l e n a m es b eg i n n i n g w i t h a dot ( . ) i n d icate f i l es hidden from n o r m a l view u s i n g ls


and o t h e r commands. T h i s i s not a s e c u rity featu re. H id d e n files keep necessary u s e r
- confi g u ra t i o n f i l es f r o m c l utte r i n g h o m e d i rectories. M a ny co m m a n d s process h i d d e n
f i l es o n l y with specific com m a n d - l i n e o p t i o n s , preve n t i n g o n e u s e r ' s confi g u ra t i o n f r o m
b e i n g accidenta l l y c o p i e d to o t h e r d i rectories o r u s e rs.
-

To p rotect file contents from i m p ro p e r viewing req u i res t h e u s e of file permissions.

[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 -]$ cd Videos


[student@desktopX Videos]$ pwd
/home/student/Videos
-

[student@desktopX Videos]$ cd /home/st u d e n t /Document s


[student@desktopX Documents]$ pwd
/home/student/Documents
-

[student@desktopX Documents]$ cd -
[student@desktopX Videos]$ pwd
/home/student/Videos
[student@desktopX Videos]$ cd -
-

[ student@desktopX Documents]$ pwd


/home/student/Documents
[student@desktopX Documents]$ cd -
-

[student@desktopX Videos]$ pwd


/home/student/Videos
[student@desktopX Videos]$ cd
[student@desktopX -]$
-

- 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
-

T h e cd . . com m a n d u s e s t h e . . h i d d e n d i rectory to move u p o n e l eve l to t h e parent d i recto ry,


without n e ed i n g to k n ow t h e exact parent n a m e. T h e other h i d d e n d i rectory ( . ) specifies t h e
-
current directory o n com m a n d s i n w h i c h t h e c u rrent l ocat i o n i s e i t h e r t h e s o u rce o r dest i n a t i o n
a rg u ment. avo i d i n g t h e need to type o u t t h e d i rectory's a b s o l ute path n a m e .

[student@desktopX Videos] $ pwd


/home/student/Videos
-

[student@desktopX Videos]$ cd
[student@desktopX Videos] $ pwd
.

/home/student/Videos
-

[student@desktopX Videos] $ c d
[student@desktopX -]$ pwd
/home/student
[student@desktopX -]$ cd
-

[ student@desktopX home]$ pwd


. .

/home
[student@desktopX home]$ cd
-

[student@desktopX /]$ pwd


[student@desktopX /]$ cd
I

[student@desktopX -]$ pwd


-

/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 -

b a s h ( 1) , cd(1), ls(1), pwd (1 ) , u nicode(7 ) , a n d u t f - 8(7 ) m a n pages

-
U T F-8 and U n icode
http://www. u tf-8.com/

38 R H 1 24- R H E L 7-en-1 -20140606 -

-
-

Practice: Locat i n g F i l es a n d D i rectories


-

P ra ct i ce: Locat i n g Fi l es a n d D i recto ries


-

-
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

L i st t h e c u r re n t u s e r ' s h o m e d i rectory ( l o n g fo r m at) i n


-
s i m p l est synta x , w h e n i t i s n ot t h e c u r re n t l ocat i o n .

- Ret u r n to t h e c u r re n t u s e r ' s h o m e d i recto ry.

-
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.

M ove u p two l eve l s f ro m t h e c u r re n t l o cat i o n .

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 to t h e b i n a r i e s l o cat i o n , from a ny c u r r e n t l ocat i o n .


-

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 .
-

M ove to t h e b i n a r i e s l ocat i o n , fro m t h e root d i rectory.


-

- 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
-

M a t c h t h e fo l l ow i n g items to t h e i r cou nterparts i n t h e tab l e.

Action to a ccomp l is h Com m a n d

L i st t h e c u r re n t u s e r ' s h o m e d i rectory ( l o n g f o r m at) i n Is -I �


s i m p l est sy n t a x , w h e n i t i s not t h e c u r re n t l ocat i o n . -

Ret u r n to t h e c u r re n t u s e r ' s h o m e d i recto ry. cd -

-
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 -

M ove u p two l eve l s f r o m t h e c u r re n t l ocat i o n . c d . . /. .

L i st t h e c u rrent l o c a t i o n ( l o n g format) w i t h h i d d e n f i l es. Is -a l


-

M ove to t h e b i n a r i e s l ocat i o n , from a ny c u r re n t l ocat i o n . cd / b i n


-

M ove u p to t h e p a re n t of t h e c u rrent l ocat i o n . cd ..


-

M ove to t h e b i n a r i e s l o cat i o n , f ro m t h e root d i rectory. cd b i n


-

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.

File management commands


<note) (note)
A c t i v i ty S i ng l e s o u rce M u l t i p l e sou rce
<5l
Copy f i l e cp f i l e1 f i l e 2 c p f i l e1 f i l e2 f i l e3 d i r
<1l <4l
M ove f i l e mv f i l e1 f i l e2 mv f i l e1 f i l e2 fi l e3 dir
<5l
Re m ove f i l e r m f i l e1 rm -f f i l e 1 f i l e2 f i l e3
<6l
C reate d i re ctory m kd i r d i r m kd i r -p p a r1 /p a r2 /d i r
<2l <4l
Copy d i rectory c p - r d i r1 d i r2 c p -r d i r1 d i r2 d i r3 d i r4
<3l <4l
M ove d i rectory mv d i r1 d i r2 mv d i r1 d i r2 d i r3 d i r4
<2l <5l
R e m ove d i rectory r m -r d i r1 rm -rf d i r1 d i r2 d i r3
Ol
N ote: The res u l t i s a re n a me.
<2l
The " re c u rsive" option is req u i red to p rocess a s o u rce d i recto ry.
<3l
l f d i r2 exists, t h e res u l t is a m ove. If d i r2 doesn't exist , t h e res u l t is a
re n a m e.
<4l
T h e l a st a r g u m e n t m u st be a d i recto ry.
<5l
use c a u t i o n w i t h "force" o pt i o n ; you w i l l not be prom pted to confi r m
yo u r a ct i o n .
<6l
use c a u t i o n with " c reate parent" option; typing errors a re not c a u g h t .

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.

[ s t ud e n t@des ktopX -]$ mkdir Video/Watched


m k d i r : cannot c r e a t e d i r e c t o ry · video/Watc h ed ' : No s u c h file or d i r e c t o r y

The m kdir fa i l ed because Videos was m i s s p e l l e d . "Video" does n ot exist as a locat i o n i n w h i c h


to c reate t h e Wat ched s u b d i rectory. I f a p were u s e d , t h e user wou l d not have received a n e r r o r
-

m e s s a g e a nd n o w h a v e t w o d i rectories, Video a n d Videos.

[ s t u d e n t@des k t o pX - ] $ mkdir Videos/Watched


[ s t u d e n t@d e s k t opX - ] $ cd Docume n t s

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

[student@desktopX Documents]$ mkdir P roj ectX Proj ectY


-

[ student@desktopX Documents]$ mkdir -p Thesis/Chapt e r 1 Thesis/Chapt e r 2


[student@desktopX Documents]$ c d
Thesis/Chap t e r 3

[ student@desktopX -]$ ls - R Videos Docume n t s


Documents :
-

ProjectX P r oj ectY Thesis thesis_chapterl . odf thesis_chapter2 . odf


Documents/Proj ectX :
-

Documents/Proj ectY :
Documents/Thesis :
-

Chapter1 Chapter2 Chapter3


Documents/Thesis/Chapter1 :
-

Documents/Thesis/Chapter2 :
Documents/Thesis/Chapter3 :

Videos :
blockbuster1 . ogg blockbuster2 . ogg Watched -

Videos/Watched :
[ student@desktopX -]$
-

T h e l a st m k d i r c reated t h ree Cha pterN s u bd i rectories with o n e co m m a n d . T h e - p parent option -

c reated t h e missing parent d i rectory Thesis.

Copy files -

T h e c p c o m m a n d copies o n e o r m o re fi l es to become new, i n d e pe n d e n t f i l es. Syntax a l l ows


copy i n g an e x i st i n g f i l e to a new f i l e i n the cu rrent o r a nother d i recto ry, o r copying m u l t i p l e f i l e s
into a n o t h e r d i rectory. I n a ny dest i n a t i o n , new f i l e n a mes m u st be u n i q u e. I f t h e new f i l e n a m e i s -

not u n i q u e, t h e copy co m m a n d w i l l ove rw rite t h e e x i s t i n g f i l e.

[student@desktopX -]$ cd Videos


[student@desktopX Videos] $ c p block b u s t e r1 . ogg bloc kbus t e r 3 . ogg
-

[student@desktopX Videos]$ ls
total 0
- rw- rw- r - - . 1 student student 0 Feb 8 16 : 23 blockbusterl . ogg
-1

- rw- r w- r - - . 1 student student 0 Feb 8 16 : 24 blockbuster2 . ogg


·-·

- r w- rw- r - - . 1 student student 0 Feb 8 19 : 02 blockbuster3 . ogg


d r wx rwxr - x . 2 student student 4096 Feb 8 23 : 35 Watched
[ student@desktopX Videos]$
-

W h e n copy i n g m u lt i p l e f i l es with o n e c o m m a n d , t h e l a st a rg u m e n t m u st b e a d i rectory. Copied -

f i l es reta i n their ori g i n a l n a m es i n t h e n ew d i recto ry. Conf l i c t i n g fi l e n a m e s t h a t exist at a


dest i n a t i o n m a y be ove rwritte n. To p rotect users from accidenta l l y ove rwrit i n g d i rectories with
-
contents, m u l t i p l e f i l e c p com m a n d s i g n o re d i rectories specified as a sou rce. Copy i n g n o n - e m pty

i
d i rectories, with contents, req u i res the - r recursive option.

[ student@desktopX Videos]$ cd . . /Docume n t s


[ student@desktopX Documents]$ cp t h e sis_chap t e r 1 . odf
-

cp : omitting directory 'Thesis '


t h e sis_chapt e r 2 . odf Thesis Proj ectX I

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 /

42 R H1 24- R H EL 7-e n-1-20140606 -

-
-

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 Proj ectY Thesis thesis_chapter1 . odf thesis_chapter2 . odf


-

. /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]$
-

- I n t h e fi rst c p com m a n d , Thesis fa i l ed to copy, b u t t hesis_c hapt e r l . odf a n d


t hesis_chapt e r 2 . o d f succeeded. U s i n g t h e - r recursive option, copy i n g Thesis s u cceed ed.

-
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

- rw- rw- r - - . 1 student student 0 Feb 8 16 : 24 thesis_chapter1 . odf


- rw- rw- r - - . 1 student student 0 Feb 8 16 : 24 thesis_chapter2 . odf
[student@desktopX Documents]$
-

[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
-

drwxrwxr-x . 2 student student 4096 Feb 11 11 : 58 Proj ectX


drwxrwxr-x . 2 student student 4096 Feb 11 11 : 55 Proj ectY
drwxrwxr-x . 5 student student 4096 Feb 11 11 : 56 Thesis
-

- rw- rw- r - - . 1 student student 0 Feb 11 11 : 54 thesis_chapter2_reviewed . odf


. /Proj ectX :
total 0
-

- rw- rw- r - - . 1 student student 0 Feb 11 11 : 58 thesis_chapter1 . odf


- rw- rw- r - - . 1 student student 0 Feb 11 11 : 58 thesis_chapter2 . odf
. /Proj ectX/Thesis :
-

total 0
. /Proj ectY :
-

total 0
. /Thesis :
total 12
-

drwxrwxr - x . 2 student student 4096 Feb 11 11 : 59 Chapter1


drwxrwxr-x . 2 student student 4096 Feb 11 11 : 56 Chapter2
drwxrwxr-x . 2 student student 4096 Feb 11 11 : 56 Chapter3
-

-
. /Thesis/Chapter1 :

- R H124- R H E L 7-e n -1-20140606 43

-
-

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
-

- rw- rw- r- - . 1 student student e Feb 11 11 : 54 thesis_chapter1 . odf


. /Thesis/Chapter2 :
total e
-

- rw-rw- r- - . 1 student student 0 Feb 11 11 : 54 thesis_chapter2 . odf


. /Thesis/Chapter3 :
-

total 0
[student@desktopX Documents]$ -

T h e fi rst mv com m a n d i s a n exa m p l e o f rena m i n g a f i l e . T h e seco n d causes t h e f i l e t o b e


re l ocated t o a nother d i rectory. -

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.

[student@desktopX Documents]$ pwd


-

/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
-

[student@desktopX Documents]$ rm - r Thesis/Chapt e r l


[student@desktopX Documents]$ ls Thesis
total 8
-1 -

drwxrwxr-x . 2 student student 4096 Feb 11 12 : 47 Chapter2


drwxrwxr-x . 2 student student 4096 Feb 11 12 : 48 Chapter3
[student@desktopX Documents]$ r m - ri Thesis
rm : descend into directory 'Thesis ' ? y
-

rm : descend into directory 'Thesis/Chapter2 ' ? y


rm : remove regular empty file 'Thesis/Chapter2/thesis_chapter2 . odf ' ?
rm : remove directory ' Thesis/Chapter2 ' ? y
y

rm : remove directory 'Thesis/Chapter3 ' ? y


-

rm : remove directory 'Thesis ' ? y


[student@desktopX Documents]$ -

After r m fa i l ed t o d e l ete t h e Chap t e r 1 d i recto ry, t h e - r recursive o pt i o n s u cceed e d . T h e l a st


-
rm com m a n d pa rsed into e a c h s u b d i rectory f i rst, i n d iv id u a l l y d e l et i n g conta i n e d f i l es before
re moving each now-em pty d i recto ry. U s i n g - i w i l l i n t e ract ive l y p rompt for each d e l et i o n . This is
essentia l l y t h e o p posite of - f which will force t h e d e l et i o n w i t h o u t p ro m p t i n g t h e u s e r.
-

T h e rmd i r com m a n d d e l etes d i rectories o n l y if e m pty. Removed d i recto ries ca n not b e


u n d e l eted.

[student@desktopX Documents]$ pwd


-

/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]$ -

T h e rmd i r com m a n d fa i l e d t o d e l ete n o n - e m pty P r o j ec tx, b u t rm - r s u cceed e d .


-

44 R H 1 24- R H E L 7 - e n -1 -20140606 -

-
-

Command-line file ma nagement


-

References
-

cp(1), m k d i r (1 ) . mv(1 ) , rm(1 ) , a n d rmd i r (1 ) m a n pages

- R H 1 24- R H E L 7-en-1 -20140606 45

-
-

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
-

P ra ct i ce: Com m a n d - L i n e Fi l e M a n a g e ment


-

Guide d exercise -

I n t h i s l a b , you w i l l practice efficient tec h n i q u e s for c rea t i n g a n d o rg a n i z i n g f i l e s u s i n g


d i rectories, f i l e copies, a n d l i n ks. -

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. -

Before you begin...


Log i nto yo u r student acco u n t on serverX. Beg i n i n yo u r home d i recto ry. -

0 1. I n yo u r h o m e d i recto ry, create sets o f e m pty practice fi l es to u s e f o r t h e re m a i n d e r of


t h i s l a b . If t h e i nt e n d e d co m m a n d is not i m med iate l y recog n i ze d , stude nts a re expected -

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.

C reate six f i l es with names of the form s napX . j pg.


-

C reate s i x f i l es with n a m e s of the form filmX . avi.

-
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

0 2. Fro m yo u r home d i rectory, move t h e s o n g f i l e s into you r Music s u b d i recto ry, t h e


s n a ps h ot f i l e s i nto yo u r Pic t u r e s s u b d i recto ry, a n d t h e movie f i l es i nto your Vid eos
s u b d i recto ry. -

W h e n d i s t r i b u t i n g f i l e s fro m o n e l ocat i o n to m a ny l ocat i o n s , fi rst c h a n g e to t h e d i rectory


conta i n i n g the source f i l es. Use the s i m p l est path synta x , a bs o l ute o r re l at ive, to reac h -

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
-

D 3. I n yo u r h o m e d i recto ry, create t h ree s u b d i rectories for o rg a n i z i n g yo u r f i l e s i nto p rojects.


Ca l l t hese d i rectories f r iends, family, and wo r k . C reate a l l t h re e with o n e com m a n d . -

46 R H 1 24- R H E L7-e n-1-20140606 -

-
-

G u i d e d exercise
-

Yo u w i l l u s e t h ese d i rectories to rea rra n g e yo u r f i l es into p rojects.

[student@serverX -]$ mkdi r


[student@serverX -]$ ls
-
friends family wo rk
-1

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.
-

Copy f i l es (a l l types) conta i n i n g n u m b e rs 1 a n d 2 to t h e friends fo l d e r.

- Copy f i l e s (a l l types) conta i n i n g n u m be rs 3 a n d 4 to t h e fa m i l y fo l d e r.

W h e n co l l e ct i n g fi les from m u lt i p l e l ocat i o n s i n to o n e l ocat i o n , c h a n g e to t h e d i rectory


- that w i l l conta i n the destination f i l es. Use the s i m p l est path synta x , a b s o l ute o r re l a t i ve, to
reac h the s o u rce for each f i l e m a n a g e m e n t task.

[student@serverX -]$ cd f riends


[student@serverX friends]$ cp -/Music/song 1 . mp3 -/Music/song2 . mp3 -/Pic t u res/
-

[student@serverX friends]$ ls
snap1 . j pg -/Pic t u res/snap2 . j pg -/Videos/film1 . avi -/Videos/film2 . avi .

[student@serverX friends]$ c d . . /family


-1

[student@serverX family]$ c p -/Music/song3 . mp3 -/Music/song4 . mp3 -/Pic t u res/


-

[student@serverX family]$ ls
snap3 . j pg -/Pic t u res/s nap4 . j pg -/Videos/film3 . avi -/Videos/film4 . avi
- -1

D 5. Fo r yo u r w o r k p roj ect, you w i l l c reate a d d i t i o n a l copies.


-

[student@serverX family]$ cd . . /wo r k


[student@serverX work]$ c p -/Music/song5 . mp3 -/Music/song6 . mp3 -/Pic t u res/
[student@serverX work]$ ls
-
snapS . j pg -/Pic t u res/snap6 . j pg -/Videos/film5 . avi -/Videos/film6 . 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.

C h a n g e to yo u r home d i recto ry. Atte m p t to de lete both the fa m i ly and f r i e n d s p rojects


-
with a s i n g l e rmdir com m a n d .

[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

- R H 1 24- R H E L 7-e n-1 -20140606 47

-
-

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

D 8. D e l ete a l l t h e files i n t h e work p roject. b u t d o not d e l ete t h e wo r k d i rectory.

[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

D 9. F i n a l l y, from yo u r h o m e d i rectory, use t h e r m d i r co m m a n d to d e l ete t h e w o r k d i rectory.


-
The c o m m a n d s h o u l d s ucceed n ow that it i s e m pty.

[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.
-

- T h e cu rrent u s e r ' s h o m e d i recto ry.


- u s e r name U s e r u s e rname ' s h o m e d i recto ry.
-
-+ The cu rrent worki n g d i recto ry.
-- T h e prev i o u s wo r k i n g d i rectory.
-
[abc . . . ] A n y o n e c h a racter i n t h e e n c l osed c l ass.
[!abc . . . ] Any o n e c h a racter not i n t h e e n c l osed c l ass.
-
["abc . . . ] Any o n e c h a racter not i n t h e e n c l osed c l ass.
<n
[ [: a l p h a :] ] A n y a l p h a betic c h a racter.
-
[ [: l ower:] ] A n y l ower-case c h a racter.° )
<n
[ [ : u p pe r : ] ] A n y u p p e r-case c h a racter.
- [ [: a l n u m :J J A n y a l p h a betic c h a racter o r d i g it. m
)
[ [: p u nct:JJ Any p r i n ta b l e c h a racter n ot a space o r a l p h a n u m e r i c .°
<1l
- [ [: d i g it:] ] A n y d i g it, 0 - 9.
[ [: s pace: ] ] A n y o n e w h itespace c h a racter; m a y i n c l u d e tabs, newl i ne, o r ca r r i a g e
ret u r n s , a n d f o r m f e e d s as we l l a s spa ce. <n
-
<1l
N ote p re-set P O S I X c h a racter c l ass; a dj u sts fo r c u rrent loca l e.

- R H 1 24- R H E L 7-en -1 -20140606 49

-
-

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 -]$ mkdir glob ; cd glob


[student@desktopX glob] $ touch alfa b r avo charlie delta echo able
-

[student@desktopX glob] $ ls
baker cast dog easy

able alfa baker bravo cast charlie delta dog easy echo
[student@desktopX glob] $
-

Fi rst. s i m p l e pattern matches u s i n g * a n d ?. -

[student@desktopX glob] $ ls a *
able alfa
[student@desktopX glob] $ ls * a *
-

able alfa baker bravo cast charlie delta easy


[student@desktopX glob] $ ls [ ac ] *
able alfa cast charlie
[student@desktopX glob]$ ls ????
-

able alfa cast easy echo


[student@desktopX glob] $ ls ?????
baker bravo delta
-

[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 -

u s e r n a me, if o n e matches. I f n o u s e r n a m e matches, t h e n a n a ct u a l t i l d e fo l l owed by t h e string of


c h a racters w i l l be ret u r n e d .

[student@desktopX glob] $ ls -/glob


-

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.

[student@desktopX glob] $ e c ho {Sunday , Monday , Tuesday , Wednesday} . log


Sunday . log Monday . log Tuesday . log Wednesday . log
-

[student@desktopX glob] $ echo file{ l . . 3} . txt


filel . txt file2 . txt file3 . txt
[ student@desktopX glob] $ echo file{ a . . c } . t x t
-

filea. txt fileb . txt filec . txt


[student@desktopX glob]$ e c h o file { a , b } { l , 2 } . txt
fileal . txt filea2 . txt filebl . txt fileb2 . txt
[student@desktopX glob] $ echo file{a{ l , 2 } , b , c } . tx t
-

fileal . txt filea2 . txt fileb . txt filec . txt


[student@desktopX glob] $ -

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
-

d isadva ntages: 1 ) it ca n b e e a s y to v i s u a l l y c o n f u s e b a c kt i c ks with s i n g l e q u ote m a rks, a n d 2 )


backticks ca n not b e n ested i n s i d e b a c kt i c ks. T h e $ ( command ) f o r m c a n n e st m u lt i p l e com m a n d
-
expa n s i o n s i n s i d e e a c h other.

[student@desktopX glob]$ echo Today is


Today is Wednesday .
' d ate +%A ' .

[student@desktopX glob]$ echo The time


-

The time is 26 minutes past 11AM .


is $ ( date +%M } minutes past $ ( da t e +%1%p ) .

- [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.

[student@desktopX glob]$ host=S ( host n ame ) ; e c h o Shost


-

desktopX
[student@desktopX glob]$ echo " * * * * * h o s t n ame is $ { h o s t } * * * * * "
hostname is desktopX
-

[student@desktopX glob]$ echo You r u s e rname variable is \$USER .


Your username variable is $USER .
* * * * * * * * * *

-
[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]$ echo "Will var iable


Will variable desktopX evaluate to desktopX?
- $ho s t evaluate to S ( host name } ? "

[student@desktopX glob]$ echo ' Will var iable


Will variable $host evaluate to $( hostname) ?
Shost evaluate to S ( host name } ? '

-
[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

- R H1 24- R H E L 7 -en -1-20140606 51

-
-

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

P ract i ce: Pat h N a m e Expa n s i o n


-

Quiz -

Match the fo l l owing items to their counterparts i n the tab l e.

G�881 IG
-

* [ [:d i g i t : ] ] * [ [: u p p e r: ] ] *

Req u ested m a t c h to f i n d Patte r n s

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 filena mes e n d i n g in " 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 w h e re f i rst c h a ra cte r i s n ot " 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

52 RH124- R H E L 7-e n-1-20140606 -

-
Sol ution
-

Solution
-

Match t h e fo l l ow i n g ite m s to t h e i r cou nterparts i n t h e ta b l e.

Req u ested m a t c h to fi n d Pa tte r n s

O n l y f i l e n a m es beg i n n i n g w i t h " b " b*


-

O n l y f i l e n a mes e n d i n g i n " b " *b


-

O n l y f i l e n a m es c o n ta i n i n g a " b " *b*


-

O n l y f i l e n a m es w h e re f i rst c h a ra c t e r i s not " b " [ ! b] *


-

-
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: ] ] *

- R H 1 24- R H E L7-e n-1-201 40606 53

-
-

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
-

Perfor mance checkl ist -

I n t h i s l a b, you w i l l c reate, m ove, a n d re m ove f i l es a n d fo l d e rs u s i n g a variety of fi l e n a m e


matc h i n g s h o rtcuts. -

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. -

Before you begin ...


Perform the fo l l ow i n g steps o n serverX u n l ess d i rected ot h e rw i se. Log in a s s t ud e n t a n d beg i n
t h e l a b i n t h e h o m e d i recto ry.

1. To b e g i n , c reate sets o f e m pty p ractice f i l e s to u s e i n t h i s l a b. I f a n i n t e n d e d s h e l l e x pa n s i o n -

s h o rtcut i s n ot i m m e d i a t e l y recog n ized, st u d ents a re e x p e c t e d to u s e t h e s o l u t i o n to l e a r n


a n d practi ce. Use s he l l t a b co m p l et i o n to l ocate f i l e pat h n a m e s e a s i l y.

C reate a tota l of 12 f i l e s with n a m e s tv_seasonX_episodeY . ogg. R e p l a ce X w i t h t h e


season n u m ber a n d Y w i t h t h a t season's e p i sode, f o r t w o s e a s o n s o f s i x e p i sodes e a c h .

2. A s t h e author of a s u ccessf u l series of myste ry nove l s , yo u r next bestse l l e r ' s


c h a pters a re bei n g ed ited f o r p u b l is h i n g . C reate a tota l of e i g h t f i l e s w i t h n a mes
mys t e ry_ch ap t e rX . odf. R e p l a c e X with t h e n u m be rs 1 t h ro u g h 8. -

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 .
-

4. M ove t h e a p p ropriate TV episodes i nto t h e season s u b d i recto r i es. U s e o n l y two c o m m a n d s ,


specify i n g desti n a t i o n s u s i n g re l a t ive syntax.

5. To o rg a n ize t h e mystery book c h a pters, c reate a two- level d i rectory h i e ra rc hy w i t h o n e


c o m m a n d . C reate my_b e s t s e lle r u nd e r t h e exist i n g Documen t s d i recto ry, a n d c h a p t e r s
b e n e a t h t h e n e w my_bes t selle r d i recto ry. -

6. U s i n g o n e c o m m a n d , c reate t h ree more s u b d i rectories d i re ct l y u n d e r t h e my_be st selle r


d i recto ry. N a m e these s u b d i rectories edi t o r , plot_ch a nge, a n d vaca t ion. The create -

parent option is not n e e d e d s i n c e t h e my_b e s t selle r parent d i rectory a l ready exists.

7. C h a n g e to t h e chap t e r s d i recto ry. U s i n g t h e h o m e d i rectory s h o rtcut to specify t h e -

s o u rce f i l es, move a l l book c h a pters i nto t h e c h ap t e r s d i recto ry, w h i c h i s n o w yo u r c u rrent


d i recto ry. What i s t h e s i m p l est syntax to spec ify t h e dest i n at i o n d i rectory?
-
8. The fi rst two c h a pters a re sent to the editor for revi ew. To re m e m b e r to not mod ify t hese
c h a pters d u ri n g t h e rev i ew, m ove t h ose two c h a pters o n l y to the e d i t o r d i rectory. Use
relative syntax sta r t i n g from the c h a p t e r s s u b d i rectory. -

9. C h a pt e rs 7 a n d 8 w i l l be w ritten w h i l e o n vacat i o n . M ove t h e f i l es from chapt e r s to


vac a t ion. Use one com m a n d w i t h o u t w i l d c a rd c h a racte rs. -

10. With one c o m m a n d , c h a n g e t h e w o r k i n g d i rectory to the s e a s o n 2 T V e p i sodes l ocat i o n , t h e n


copy t h e fi rst episode o f t h e s e a s o n to t h e vacat ion d i recto ry.
-

54 R H 1 24- R H E L 7-en-1 -20140606 -

-
-

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 .

- 12. C h a pters 5 a n d 6 may need a p l ot c h a n g e. To p revent t h ese c h a n g es f r o m m o d ify i n g orig i n a l


f i l es, copy both f i l es i n to plot_change. M ove u p o n e d i rectory to vacat i o n ' s p a rent
d i recto ry, then u s e o n e co m m a n d from t h e re.

13. To t ra c k c h a n g es, m a ke t h ree backups of c h a pter 5. C h a n g e to the plot_change d i recto ry.


Copy mys t e ry_c h ap t e r s . odf as a n e w f i l e n a m e to i n c l ude t h e f u l l date ( Ye a r- M o- Da).
- M a ke a n o t h e r copy a p p e n d i n g the cu rrent t i m esta m p (as the n u m be r of seco n d s s i n ce
t h e epoch) to e n s u re a u n i q u e fi l e n a m e. A l s o m a ke a copy a p p e n d i n g t h e c u rrent user
($USER) to t h e file n a me. See t h e s o l u t i o n for t h e syntax of a n y you a re u n s u re of ( l i ke what
- a rg u m e n t s to pass t h e date).

N ote, we cou l d a l so m a ke t h e same backups of t h e c h a pte r 6 files too.


-

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.

- R H 1 24- R H E L 7-en-1 -20140606 55


-

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
-

I n t h i s l a b , you w i l l c reate, m ove, a n d re m ove f i l e s a n d fo l d e rs u s i n g a va r i ety of f i l e n a m e


matc h i n g s h o rtcuts. -

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. -

Before you begin ...


Perform the fo l l ow i n g steps o n serverX u n l ess d i rected ot herwise. Log in a s s t ud e n t a n d beg i n -

t h e l a b i n t h e h o m e d i rectory.

1. To beg i n , c reate sets o f e m pty practice f i l e s t o u s e i n t h i s l a b. I f a n i n t e n d e d s h e l l e x p a n s i o n -

s h o rtcut i s not i m m e d iately recog n ized, s t u d e n t s a re expected to u s e t h e s o l u t i o n to l e a r n


a n d practice. U s e s h e l l t a b co m p l e t i o n to l ocate f i l e path n a m es e a s i l y.
-

C reate a tota l of 1 2 fi l es with n a m es tv_seasonX_episodeY . ogg. R e p l a c e X w i t h t h e


season n u m be r a n d Y with t h a t sea s o n ' s e p i s o d e, f o r t w o seasons o f s i x e p i sodes e a c h .

I� student@serverX - ] $ touch tv_season { 1 . . 2 }_episode{ 1 . . 6 } . ogg


I [student@serverX - ] $ ls -1

2. As t h e a u t h o r o f a s u ccessfu l series o f mystery nove l s, you r next bestse l l e r ' s


c h a pters a re b e i n g ed ited f o r p u b l i s h i n g . C reate a tot a l o f e i g h t f i l es w i t h n a mes -
mys t e ry_chapt e rx . odf. Replace X with t h e n u m be rs 1 t h ro u g h 8.

! [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
-
..

5. To orga n ize t h e mystery book c h a pt e rs, c reate a two- l eve l d i rectory h i e ra rc h y w i t h o n e


com m a n d . C reate my_b e s t seller u nd e r t h e e x i s t i n g Docume n t s d i recto ry, a n d chapt e r s -

beneath t h e n e w my_b e s t seller d i recto ry.

[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
-

6. U s i n g o n e co m m a n d , create t h ree more s u b d i rectories d i rect l y u nd e r the my_bes t selle r


d i recto ry. N a m e these s u b d i rectories edit o r , plo t_c hange, a n d vacat ion. T h e create


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

- 7. C h a n g e to t h e chapt e r s d i recto ry. U s i n g t h e h o m e d i rectory s h o rtcut to s p ecify t h e


s o u rce f i l es, move a l l book c h a pters i nto t h e chapt e r s d i recto ry, w h i c h is now y o u r c u rrent
d i recto ry. What i s the s i m p l est syntax to s p e c ify t h e d e s t i n a t i o n d i rectory?

1· [student@serverx -]$ cd Document s /my_be s t selle r/chapters


-

[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 - �����

9. C h a pters 7 a n d 8 w i l l be w ritten w h i l e on va cat i o n . M ove t h e f i l es from chap t e r s to

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.

!,.:' [ student@serverX chapters] $


i
mv mys te ry_chap t e r 7 . odf mys t e ry_chapterB . odf . . I vacation
. I

�� tudent@serverx chapters] $
ls - 1
I
..
I


-
_ ls - 1 . . /vacation

- 1 0. W i t h one co m m a n d , c h a n g e t h e work i n g d i rectory to t h e season 2 TV episodes l ocat i o n , t h e n


c o p y t h e f i rst e p isode o f t h e season to t h e vacation d i recto ry.

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 /

-
.

11. W i t h o n e co m m a n d , c h a n g e t h e work i n g d i rectory to vacation, t h e n l ist i t s f i l es. E p i s o d e


2 i s a l so n e e d e d . Ret u r n to t h e season2 d i rectory u s i n g t h e previous working directory
-
s h o rtcut. This w i l l s u cceed if t h e last d i rectory c h a n g e was acco m p l ished w i t h o n e co m m a n d .
C o py t h e episode 2 f i l e i nto vacation. Ret u r n t o vacation u s i n g t h e s h o rtcut a g a i n .

[student@serverX season2] $ cd -/Documents/my_be s t seller/vacation


[student@serverX vacation] $ ls
-

[student@serverX vacation] $ c d -
-1

- [student@serverX season2] $ cp tv_season2_episode2 . ogg -/Document s/my_best selle r/


[student@serverx vacation] $ cd -
vacation

-
[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

12. C h a pt e rs 5 a n d 6 may need a p l ot c h a nge. To p revent t h e s e c h a n g e s f r o m modify i n g orig i n a l


fi les, copy both f i l e s i nto plo t_c h ange. M ove u p o n e d i rectory to vac a t io n ' s parent

-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_ ���������������������--�������������

13. To track c h a n ges, m a ke t h re e b a c k u p s of c h a pter 5. C h a n g e to t h e plot_change d i recto ry.


Copy mys t e ry_c hap t e r s . odf as a new f i l e n a m e to i n c l u d e t h e f u l l date (Year-M o-Da).
M a ke a n ot h e r copy a p p e n d i n g the c u rrent t i m esta m p (as the n u m be r of seco n d s s i nce
t h e epoch) to e n s u re a u n i q u e file n a m e. A l s o m a ke a copy a p p e n d i n g t h e cu rrent user
($USER) to t h e f i l e n a m e. See t h e s o l ution for t h e syn t a x of a ny you a re u n s u re of ( l i ke what -

a rg u m e nts to pass t h e date).

[student@serverx my_bestseller]$ cd plot_change


[student@serverx plot_change] $ c p mys t e ry_c hap t e r 5 . odf
-

mys t e ry_chapter5_$ ( da t e +

[student@serverx plot_change]$ c p myst e ry_chapt e r 5 . odf


%F ) . odf
mys t ery_chapter5_$ ( date +

[student@serverX plot_change] $ c p mys t e ry_chapt e r5 . odf


%s ) . odf

[student@serverx plot_change] $ ls
mys t e ry_chapter5_$USER . odf
-1

N ote, we cou l d a l so m a ke t h e s a m e backups o f t h e cha pter 6 f i l es too.


-
14. The p l ot c h a n ges we re not s u ccessf u l . D e l ete the plot_c h ange d i recto ry. First, d e l ete
a l l of the f i l es i n t h e plot_change d i recto ry. C h a n g e d i rectory u p one level beca u s e t h e
d i rectory can not 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 rm c o m m a n d without t h e recursive option. T h i s atte m pt s h o u l d fa i l . N o w u s e t h e rmdir
command, w h i c h wi l l succeed.

[student@serverx plot_change] $ rm myst e r y *


-

[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
-

1 5. W h e n t h e vacation is ove r, t h e vacat ion d i rectory is n o l o n g e r needed. D e l ete it u s i n g t h e


r m co m m a n d with t h e recursive o p t i o n . -

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

GETTING HEL P IN RED HAT


E N TER P RISE LINUX

Overview

Goal To resolve problems by u s i ng o n - l i n e h e l p syst e m s a n d Red


H a t su pport u t i l ities.

Objectives • Use the m a n L i n u x m a n u a l read e r.

• Use t h e p i nto G N U I nfo read e r.

• U se t h e Red Hat Pac k a g e M a n a g e r ( R PM ) package


d ocu mentation.

• Use the red h a t - s u p p o rt-tool c o m m a n d .

Sections • Read i n g Documentation U s i n g m a n C o m m a nd (and


Practice)

• Read i n g Documentation U s i n g p i nto C o m m a n d (a n d


Practice)

• Reading Documentation i n /us r/sha re/doc (a n d Practice)

• Gett i n g H e l p From Red Hat (and Practice)

Lab • Viewing a n d Print i n g H e l p Doc u mentation

R H 1 24- R H E L7-e n-1-201 40606 61


-

C h a pter 3. Gett i n g H e l p i n Red Hat E nterp rise L i n u x

Rea d i n g Doc u m e ntat i o n U s i n g m a n Co m m a n d

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.

I ntroducing the man command


The h i storica l L i n u x P ro g ra m m e r ' s M a n u a l , fro m w h i c h m a n pages o r i g i nate, was l a rg e e n o u g h
to b e m u l t i p l e pri nted b o o ks. E a c h conta i ned i nfo rmat i o n f o r specific types o f fi l es, w h i c h h a ve
become t h e sections l i sted b e l ow. A r t i c l e s a re refe rred to a s topics, as pages n o l o n g e r a p p l ies.

Sections o f the Linux manual -

Section Content type


1 User com m a n d s (both executable and shell programs) -

2 System ca l l s (kernel routines invoked from user space)


3 L i b ra ry f u n c t i o n s (provided by program libraries)
4 Specia l f i l e s (such as device files)
5 F i l e formats (for many configuration files and structures)
6 Games (historical section for amusing programs)
7 Conve n t i o n s . sta n d a rd s , a n d m i s ce l l a n e o u s (protocols, file systems)
-
8 System a d m i n istra t i o n a n d p r i v i l eg e d com m a n d s (maintenance tasks)
9 L i n u x ke r n e l A P I (internal kernel calls)
-

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.

To read specific m a n pages, use man topic. To p i c contents d i s p l ay one screen at a t i m e. U se


a r row keys for s i n g l e l i n e scro l l i n g o r t h e s pace b a r for t h e next scree n . T h e man com m a n d
s e a rches m a n u a l sect i o n s i n a config u red ord e r, d i s p l a y i n g po p u l a r sect i o n s f i rst. Fo r exa m p le,
man passwd d i s p l a ys pas swd(l ) by defa u lt. To d i s p l ay t h e man page topic from a specific
-
sect i o n , i n c l u d e t h e sect i o n n u m be r a rg u ment: man 5 pas swd d i s p l ays passwd(5).

62 R H 1 24- R H E L7-e n -1-20140606 -

-
-

I d e nt ify m a n pages by keyword


-

I dentify man pages by keyword


- The a b i l ity to eff i c i e n t l y searc h for topics and navigate man pages i s a c r i t i c a l a d m i n istration s k i l l .
T h e fo l l o w i n g ta b l e l ists basic man n a v i g a t i o n c o m m a n d s :

- Navigat ing man pages

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
-

DownA r row S c ro l l forwa rd (d own) one l i n e


U pA r r ow S c ro l l b a c k (u p) o n e l i n e
-
d S c ro l l forward (d own) o n e h a l f-screen
u S c ro l l backwa rd (up) o n e h a l f-screen
-
/s t ring Search forwa rd (d own) fo r s t ri n g i n t h e m a n page
n Repeat previous search fo rwa rd (d own) i n t h e m a n p a g e
-
N R e p e a t p revi o u s search bac kwa rd ( u p ) i n t h e m a n page
g G o to start of t h e m a n page.
-
G Go to e n d of t h e man page.
q E x i t man a n d ret u rn to t h e com m a n d s h e l l p ro m pt
-

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.

- Reg u l a r express i o n s a n d synta x a re d is c u s s e d i n Red Hat System Administration II, and


i n the regex(7) man topic.

- R H124- R H E L 7-en-1-20140606 63

-
-

C h a pter 3. Gett i n g H e l p i n Red Hat Enterprise L i n u x

Searching for man pages by keyword


A keyword search of m a n pages is perfo r med u s i n g man - k keyword, w h i c h d i s p l ays a l i st of -

keyword -m atc h i n g m a n page topics w i t h sect i o n n u m be rs.

[student@desktopX -]$ m a n - k pas swd


checkPasswdAccess ( 3 ) query the SELinux policy database i n the kernel .
-

chpasswd ( 8 ) update passwords in batch mode


ckpasswd ( 8 ) nnrpd password authenticator
fgetpwent_r ( 3 ) get passwd file entry reentrantly
getpwent_r ( 3 ) get passwd file entry reentrantly
-

passwd ( 1 ) - update user ' s authentication tokens


sslpasswd ( 1ssl) compute password hashes
-

passwd ( 5 ) password file


passwd . nntp ( 5 ) - Passwords for connecting to remote NNTP servers
passwd2des ( 3 ) - RFS password encryption -

Po p u l a r syste m a d m i n istration topics a re i n sect i o n s 1 (user c o m m a nds), 5 (fi l e fo r m ats), a n d 8


(ad m i n istrative c o m m a n d s). Ad m i n istrators u s i n g certa i n t ro u b l eshoot i n g too l s a l so use section
2 (system ca l l s) . The re m a i n i n g sect i o n s a re co m m o n l y for prog ra m m e r refe rence o r a d va n ced
-
a d m i n istra t i o n .

Note -

Keyword s e a rc h e s re l y on a n i n dex g e n e rated by t h e mandb(8) com m a n d , w h i c h m u st


be r u n as root. T h e c o m m a n d r u n s d a i l y t h ro u g h c r o n . d aily, o r by anac r o n t a b -
wit h i n a n h o u r of boot if out of date.

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 -

descr i p t i o n s l ike t h e - k . A fu l l-text search ca n u s e g reater systems resou rces a n d take


m o re t i me.
-

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 .

Before you begin ...


Pe rfo rm t h e fo l l ow i n g steps o n serverX u n l ess d i rected o t h e rw ise.

D 1. View the gedit(1) man page.

[student@serverX ] $
-
- -- -- -·--------

I
-
- ma n i g e d i t
- --
_ --------
_ ____� - ----�

- D 2. Research how to edit a specific fi l e using gedit from t h e co m m a n d l i n e.

gedit fil ename

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

D 4. Research t h e su(1) m a n page.

[student@serverX -]$
-
����- - - · ---··---------------- ---

man 1 su
._
, __
_ ----- - ··- - - · - --- -------

D 5. Research w h a t s u does w h e n t h e use rname a rg u m e n t is o m itted.

-
s u a s s u m es a use rname of root.

D 6. Research how s u beh aves when a s i n g l e d a s h option is used.


-
s u starts a c h i l d login shell (creat i n g login e n v i ro n m e n t by s o u rc i n g login scri pts).
Without the s i n g l e d a s h , a n o n - l o g i n c h i l d s he l l i s created, matc h i n g the u s e r ' s c u rrent
en vi ro n m e nt.
-

D 7. Cons u l t t h e pas swd(1) m a n page. Dete r m i n e t h e o pt i o n s t h a t w i l l l oc k a n d u n l o c k a u s e r


acco u n t w h e n t h i s co m m a n d is used by root.

[student@serverX -]$
-

man 1 pas swd

-
pas swd - 1 username

pas swd - u use rname


-

D 8. Locate t h e two p r i n c i p l es to re m e m b e r accord i n g to t h e pas swd man page a u t h o rs.


Searc h for the word " p r i n c i p l e".
-

- R H 1 24- R H E L7-e n-1-20140606 65

-
-

C h a pter 3. Gett i n g H e l p i n Red H a t E n t e r p rise L i n u x

• Protect yo u r password .

• C hoose a h a rd-to-g u ess password. -

D 9. C o n s u l t t h e man page d oc u m e n t i n g t h e syntax of the / e t c / pas swd fi l e. What i s stored


i n t h e t h i rd fie l d of each l i n e? -

The re l evant m a n p a g e is passwd(5), fo u n d w i t h man - f pas swd.


-
The U I D ( n u m e r i c u s e r I D) for e a c h acco u nt.

D 10. Which c o m m a n d w i l l l i st d eta i l ed i n formation a bout a z i p a rc h i ve?


-

zipinfo(1) fo u n d w i t h man - k zip

D 11. Which m a n page conta i n s a l ist of paramete rs t h at c a n b e passed to the ke r n e l at boot -

t i m e?

boo t p a r am(7 ) fo u n d with man - k boot -

D 12. W h i c h comma n d i s used to t u n e ext4 f i l e syste m p a ra m ete rs?

-
t u ne2fs(8) fou n d w i t h man - k ext4

66 R H124- R H E L7-en+ 20140606

-
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

This ( t h e Directory node ) gives a menu of maj o r t o p ic s .


Typing "q" exi t s , " ? " l i s t s a l l Info c o mm a n d s , "d" returns h e re ,
" h " g ives a p rime r t o r f i rst - timers ,
" mEmacs<Return>" visits the Emacs t o p i c , etc .

In Ema c s , you can c l i c k mou s e b u t t o n 2 o n a menu item or c ro s s r e f e r e n c e


to select i t .

* 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%

Figure 3. 1 : pinto Info document viewer, top directory

I nfo d o c u m entation is struct u red as hyper l i n ke d i nfo nodes. T h i s fo rmat is more f l e x i b l e t h a n


m a n p a g e s , a l lowing t h o rou g h disc u s s i o n of c o m p l ex c o m m a n d s a n d c o n ce pts. Like m a n pages,
i nfo n odes a re read fro m t h e c o m m a n d l i ne, using either t h e info o r pinto c o m m a nd s .

S o m e c o m m a n d s a n d u t i l ities h a v e both m a n pages a n d i nfo d oc u m e ntation; u s u a l l y, t h e i nfo


d o c u m e n t a t i o n w i l l be m o re in-depth. C o m p a re the d iffe rences i n tar d o c u mentation u s i n g man
a n d pinfo:

[ s t u d e n t@d e s k topX - ] $ man tar


[ s t u d e n t@d e s k t o pX - ] $ pinto tar

The pinfo info read e r i s m o re a d v a n ced t h a n t h e orig i n a l info c o m m a n d . D e s i g n ed to match


the lynx text web b rowser keyst rokes, it a l so adds color. I nfo nodes for a p a rt i c u l a r topic are
browsed with pinfo topic. Enter only pinto for the i nfo topics d i recto ry. New docu mentation
nodes become ava i l a b l e i n pinto w h e n their co rrespondin g software p a c kages a re i nsta l l e d .

R H 1 24· R H EL 7 ·en-1-20140606 67
-

C h a pter 3. Gett i n g H e l p i n Red Hat E n t e r p r i se L i n u x


-

GNU I nfo vs. man page navigation


-
The info co m m a n d uses d iffe rent n a v i g a t i o n keystrokes t h a n does man. T h e info c o m m a n d
wa s d e s i g n e d to match t h e keyst rokes of t h e hype rtext-awa re lynx web b rowser. C o m p a re t h e
k e y b i n d i n g s i n t h e fo l l owi n g t a b l e : -

pinfo and man, key binding comparison

N av 1 g a t 1 on pinto man

S c ro l l forward (d own) o n e screen PageDown o r Space PageDown o r Space


S c ro l l b a c kwa rd (up) one screen PageUp o r b PageUp o r b
-

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 -

D i s p l a y t h e top (up) of a topic HOME 1G


-
-
S c ro l l bac kwa rd (up) o n e h a l f-screen u
Scro l l forwa rd (down) to next hype r l i n k DownAr row -
-
O p e n topic at c u rsor location Enter -

Scro l l forward (d own) o n e l i n e - DownA r r ow or E n t e r


-
Scro l l b a c kwa rd (up) to p revious hype r l i n k U pA r r ow -

S c ro l l bac kwa rd (u p) o n e l i n e - UpAr row


Searc h f o r a pattern / s t ring / s t ri n g
D i s p l ay next node (c h a pter) i n topic n -

Repeat prev i o u s search forwa rd (d own) I then Enter n -

D i s p l ay p revi o u s n o d e (c ha pter) i n topic p -


-
Repeat p rev i o u s sea rc h bac kwa rd ( u p) N -

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

pinfo pinfo (Documentation for 'pinto ')


-
• 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

pinfo(1) a n d info(1 ) m a n pages

68 R H 1 24- R H E L 7-en-1 -20140606

-
-

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

I n t h i s l a b , you w i l l b rowse G N U I nfo d o c u m e nta t i o n u s i n g c o m m a n d - l i n e too l s.


-
O utcomes
U nd e rsta n d p ro g ra m d o c u mentation i n t h e G N U I nfo node syste m .
-
Before you begin...
Perfo r m t h e fo l l ow i n g steps o n serverX u n l ess d i rected ot h e r w i se.
-
D 1. I nvoke pinfo w i t h o u t a rg u m ents.

- ii [student@serverx - ] $ pinfo
I

- D 2. Navigate to the Common o p t ions to pic.

U s e U pAr row o r DownAr row u n t i l ( c o r e u t il s ) Common o p t ions i s h i g h l i g hted.


Press Enter to view this topic.
-
D 3. B rowse t h ro u g h t h i s info topic. Lea r n if l o n g -sty l e options c a n be a b b reviated.

- Use PageUp a n d PageDown to navigate t h ro u g h t h e topic. Yes, m a n y p rog ra m s a l low


l o n g options to b e a b b reviated.

- D 4. Dete r m i n e w h a t t h e sy m b o l s - - s i g n ify w h e n u s e d as a co m m a n d a rg u m e nt.

T h e sym b o l s - - s i g n ify the end of co m m a n d options and the start of co m m a n d


- arguments i n com p l ex co m m a nds w h e re t h e s h e l l ' s c o m m a n d - l i n e pars e r m i g ht not
correct l y m a k e t h e d i s t i n c t i o n .

- D 5. Without e x i t i n g p i n f o , m ove u p t o t h e G N U Co r e u t ils n ode.

Press u to move u p to the top node of the topic.

- D 6. M ove u p a g a i n , to the top topic.

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.

.. D 7. Searc h for t h e patte r n nano a n d s e l ect t h a t t o p i c .

Press I fo l l owed by t h e search patte r n " n a n o" . W i t h t h e topic h i g h l i g hted, p ress E n t e r .


-
D 8. I n t h e I n t rod u c t io n , l ocate a n d s e l e c t Command L i n e Options. B rowse t h e topic.

P ress E n t e r to s e l ect I n t r o d u c t ion, t h e n DownAr r ow a n d E n t e r to s e l ect Command


-
Line Options. Use the a r row keys to b rowse the topic.

D 9. M ove u p o n e l ev e l to ret u r n to I n t r od u c t ion. M ove to t h e next topic.


-

- R H 1 24- R H E L7-en-1-20140606 69

-
-

C h a pter 3. Gett i n g H e l p i n Red H a t Enterprise L i n u x

P ress u t o move u p o n e leve l . The n e w location w i l l b e nano's t o p i c 1 I n t r od u c t ion.


N ow p ress n. Yo u w i l l h ave m oved to nano ' s topic 2 Editor Basics.
-

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
!

D 12. S e l ect t h e Edit o r B a s i c s t o p i c . -

Press DownA r r ow to h i g h l i g h t Editor Bas i c s , t h e n p ress En t e r to s e lect t h i s topic.


-

D 13. Read t h e E n t e r i n g T e x t a n d Special F u n c t io n s s u btopi cs.

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 -

-
-

Rea d i n g D o c u m e ntat i o n i n /u s r/s h a re/d o c


-

Rea d i n g Doc u m e ntat i o n i n /u s r/s h a re/d oc


-

-
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.

Introducing package documentation


- I n a d d i t i o n to man a n d pinfo, deve l o p e rs m a y a l so c h oose to i n c l u d e d o c u m e n ta t i o n i n t h e i r
a p p l icat i o n ' s R P M d istribution package. W h e n t h e p a c k a g e is i n sta l l e d , f i l e s recog n ized as
d o c u m e nt a t i o n a re moved to / u s r / s hare/doc/packagename. Software package b u i l d e rs
- may i n c l u d e a nyt h i n g deemed h e l pf u l as a com p l e ment to, b u t not d u p l icat i n g , man pag es. G N U
packages a l so use / u s r /share/doc t o s u p p l e m e n t i nfo nodes.

- M ost p a c k a g es i n c l u d e f i l es desc r i b i n g package d istribution l i c e n s i n g . Some packages i n c l u d e


exte nsive P D F- o r H T M L-based d o c u m entat i o n . Accord i n g l y, a usef u l package b rows i n g method i s

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 .
-

M a ny packages a l s o i n c l u d e d eve l o p e r d o c u mentat i o n , s u c h a s a n A p p l icat i o n


Prog ra m m i n g I nte rface ( A P I ) s p e c ificat i o n , p rovided i n a pa c k a g e w i t h a n a m e e n d i n g
-
i n -devel o r s i m i l a r. Packages may i n c l u d e a d d i t i on a l fi l es, s u c h a s h e a d e rs ; u s ef u l
d o c u m e ntation u s u a l l y o n l y n e e d e d f o r softwa re deve l o p me n t o r com p i l i n g .

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.

- R H124- R H EL 7 -en -1 -20140606 71


-

C h a pter 3. Gett i n g H e l p i n Red Hat Enterp rise L i n u x


-

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

-
-

P ra c t i ce: Viewing Pa c ka g e D o c u m entation


-

P ra ct i ce: V i ewi n g Pa c ka g e Docu m e ntat i o n


-

-
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.

Before you begin ...


-
Pe rfo rm t h e fo l lo w i n g steps o n serverX u n l ess d i rected o t h e r w ise.

D 1. W h e re can you f i n d t h e l atest n ews a b o u t the vim p roject?

[student@serverx -]$ cd / u s r/share/doc


-

[student@serverX doc]$ less vim - common - * / README . tx t


-

View t h e vim-common R E A D M E and search for " n ews".


-
D 2. What is the wiki U R I for t h e yum packa g e?

-
i [student@serverx doc]$ less yum - 3 * / README
I

Search for " w i k i " i n / u s r/s h a re/doc/yum - 3 * / README.


-
D 3. What exa m p l e s a re prov i d e d for the com m a n d - l i n e be ca l c u l ator?

- I [student@serverX doc]$ ls -1 bc - * /Examples


'

Fo u n d i n t h e d i rectory / u s r /share/doc/bc - * / Examples.


-

... I [student@serverx doc]$


D 4. How wou l d you read the provided G R U B2 m a n u a l ?

firefox g r u b 2 - t ools - * /g r u b . html


i

Use firefox to d i s p l ay / u s r /share/doc/g r u b 2 - t ools - * / g r u b . h t ml.


-

D 5. What softwa re p rovi des its d o c u m e n t a s a s e p a rate package?

[student@serverx doc]$
[student@serverx doc]$
-
yum list * - doc *

[student@serverx -]$
cd

U s e yum t o d i s p l ay o n l y t h ose packages t h a t conta i n "-doc", "-docs", o r " - d oc u m e ntation"


i n t h e package n a me. When f i n is h e d , ret u r n to t h e home d i rectory.
...

... R H 1 24- R H E L 7-en-1-20140606 73

You might also like