10
Chapter 2 Files and File Processing Questions and Problems 1. What is a file in LINUX/UNIX? A file is a sequence of bytes. This means that everything in LINUX/UNIX is a file, including all hardware devices such as keyboard, mouse, disk drive, tape drive, and network interface card. 2. Does LINUX/UNIX support any file types? If so, name them. Does LINUX/UNIX support file extensions? Yes, LINUX/UNIX supports five file types. They are: ordinary file, directory, device/special file (block special and character special), symbolic link, and named pipe. BSD compliant LINUX/UNIX system support an additional file type, called socket. You can determine the type of a file by using the ls -l file_name command and observing the first character of the output line. The following characters are used to represent the file types. - ordinary d directory b block special file (a block device) c character special file (a character device) l symbolic link p named pipe (FIFO) s socket No, LINUX/UNIX does not support file extensions. Some LINUX/UNIX applications support/require file extensions. For example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the Java compilers require the .java extension. 3. What are special files in LINUX/UNIX? What are character special and block special files? Run the ls /dev | wc -w command to find the number of special files your system has. Every input/output hardware device in a LINUX/UNIX based system is represented by a file, called a special file. If

easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

  • Upload
    phungtu

  • View
    213

  • Download
    1

Embed Size (px)

Citation preview

Page 1: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

Chapter 2Files and File Processing

Questions and Problems

1 . W ha t i s a f i l e in L I N U X / U N IX ?

A f i l e i s a s e q u e n c e o f b y t e s . T h i s m e a n s t h a t e v e r y th i n g i n L I N U X / U N I X i s a f i l e , i n c l u d in g a l l h a r d w a r e d e v i c e s s u c h a s k e y b o a r d , m o u s e , d i s k d r i v e , t a p e d r i v e , a n d n e t w o r k in t e r f a c e c a r d .

2 . D o e s L I N U X /U N I X s u pp o r t a ny f i l e t y pe s ? I f s o , na m e t h e m . D o e s L I N U X /U N I X s up po r t f i l e e x t e ns i o ns ?

Y e s , L I N U X /U N I X s u p p o r t s f i v e f i l e t y p e s . T h e y a r e : o r d i n a r y f i l e , d i r e c to r y , d e v i c e / sp e c i a l f i l e ( b l o c k s p e c i a l a n d c h a r a c t e r sp e c i a l ) , sy m b o l i c l i n k , a n d n a m e d p i p e . B SD c o m p l i a n t L I N U X / U N I X s y s t e m s u p p o r t a n a d d i t i o n a l f i l e t y p e , c a l l e d s o c k e t . Y o u c a n d e t e r m i n e t h e t y p e o f a f i l e b y u s i n g t h e ls -l file_name c o m m a n d a n d o b se r v i n g t h e f i r s t c h a r a c t e r o f t h e o u t p u t l i n e . T h e f o l l o w i n g c h a r a c t e r s a r e u s e d to r e p r e s e n t t h e f i l e t y p e s .

- o r d i n a r y

d d i r e c to r y

b b l o c k s p e c i a l f i l e ( a b lo c k d e v i c e )

c c h a r a c t e r s p e c i a l f i l e ( a c h a r a c t e r d e v i c e )

l s y mb o l i c l i n k

p n a m e d p i p e ( F I FO )

s s o c k e t

N o , L I N U X / U N I X d o e s n o t s u p p o r t f i l e e x t e n s i o n s . So m e L I N U X / U N I X a p p l i c a t i o n s s u p p o r t / r e q u i r e f i l e e x t e n s io n s . F o r e x a m p l e , C c o m p i l e r s r e q u i r e t h e . c e x t e n s i o n , C + + c o m p i l e r s r e q u i r e t h e . c p p e x t e n s i o n , a n d t h e Ja v a c o mp i l e r s r e q u i r e t h e . j a v a e x t e n s i o n .

3 . W ha t a r e sp e c i a l f i l e s in L IN U X / U N I X ? W ha t a r e c ha r a c t e r s p e c ia l a n d b l o c k s pe c i a l f i l e s ? R u n t he ls /dev | wc -w c o m m a nd t o f in d t h e n um b e r o f s pe c i a l f i l e s y o ur s y s t e m ha s .

E v e r y in p u t / o u t p u t h a r d w a r e d e v i c e i n a L I N U X / U N I X b a s e d s y s t e m i s r e p r e s e n t e d b y a f i l e , c a l l e d a s p e c i a l f i l e . I f t h e d e v i c e p e r f o r m s i n p u t o r o u t p u t ( o r b o t h ) o n e c h a r a c t e r a t a t im e , t h e c o r r e s p o n d i n g s p e c i a l f i l e i s k n o w n a s a c h a r a c t e r s p e c i a l f i l e . I f t h e d e v i c e p e r f o r m s i n p u t / o u t p u t i n t e r m s o f m u l t i p l e ( b l o c k s o f ) b y t e s a t a t im e , t h e c o r r e s p o n d i n g s p e c i a l f i l e i s k n o w n a s a b l o c k sp e c i a l f i l e . A s s t a t e d i n p r o b l e m 2 , y o u c a n d e t e r m in e th e t y p e o f a f i l e b y u s i n g th e ls -l c o m m a n d a n d o b s e r v i n g t h e f i r s t c h a r a c t e r i n a n o u t p u t l i n e .

T h e e x e c u t i o n o f t h e ls /dev | wc -w c o m m a n d o n o u r s y s t e m d i s p l a y e d 2 , 3 6 0 . T h i s me a n s t h a t o u r s y s t e m h a s 5 , 0 5 2 s p e c i a l f i l e s .

4 . D r a w t h e h i e r a r c h ic a l f i l e s t r u c t ur e , s i m i l a r t o t he o ne g i v e n in F i g ur e 2 . 1 , f o r y o ur LI N U X / U N I X m a c h i ne . Sh o w f i l e s a n d d i r e c t o r i e s a t t h e f i r s t t w o l e v e l s .

Page 2: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

A l s o s ho w w he r e y o ur ho m e d i r e c t o r y i s , a l o ng w i t h f i l e s a n d d i r e c t o r i e s un d e r y o ur ho m e d ir e c t o r y .

O u r a n s w e r i s g i v e n in F i g u r e 1 . 2 .

5 . G iv e t h e c o m m a n d t ha t y o u c a n u s e t o l i s t t h e a b s o l u t e p a t h na m e o f y o u r h o m e d i r e c t o r y .

E a c h o f t h e f o l l o w i n g t h r e e c o m m a n d s l i s t s t h e a b s o l u t e p a t h n a m e o f y o u r h o m e d i r e c to r y .

echo ~

echo $HOME ( o r echo $home f o r T C s h e l l )

cd ; pwd ( O r , j u s t t h e pwd c o m m a n d r i g h t a f t e r y o u l o g o n . )

6 . W ha t s he l l a r e y o u us i ng ? W h a t i s t h e s e a r c h p a t h f o r y o u r s h e l l ? Ho w d i d y o u o bt a in y o u r a n s w e r ? W ha t c o m m a nd ( s ) d i d y o u us e ?

T h e f o l l o w i n g s e s s i o n s h o w s t h e a n s w e r s t o t h e q u e s t i o n s .

$ echo $SHELL

/bin/bash

$ echo $PATH

/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home3/faculty/msarwar/bin

$

7 . W r i t e do w n t h e l i n e i n t he / e t c /p a s s w d f i l e o n y o u r s y s t e m t ha t c o n t a i ns i n f o r m a t io n a bo u t y o u r l o g i n . W h a t a r e y o u r l o g i n s h e l l , u s e r I D , h o m e d i r e c t o r y , a n d g r o up I D ? D o e s y o ur s y s t e m c o n t a i n t he e n c r y p t e d p a s s w o r d i n t h e / e t c / pa s s w d o r / e t c / s h a do w f i l e ?

Se e t h e h i g h l i g h t e d p o r t i o n s i n t h e l i n e f o r d i f f e r e n t a n s w e r s .

msarwar:x:608:200:Syed Mansoor Sarwar:/home/faculty/msarwar:/bin/bash

L o g i n s h e l l /bin/bash ( B o u r n e A g a i n s h e l l )

U s e r I D 608

H o m e d i r e c t o r y /home/faculty/msarwar

G r o u p I D 200

O u r s y s t e m c o n ta in s t h e e c r y p te d p a s s w o r d s i n t h e / e t c / s h a d o w f i l e . I f y o u r s y s t e m i s o n a n e t w o r k w i t h a n N I S d a t a b a s e , y o u c a n d i s p l a y y o u r l i n e i n t h e N I S y p p a s s w d d a t a b a s e b y u s in g t h e ypcat passwd c o m m a n d a s s h o w n b e l o w . N o t e t h a t t h e s e c o n d f i e l d i n t h e o u t p u t i s y o u r e n c r y p t e d p a s s w o r d .

$ ypcat passwd | grep msarwarmsarwar:7PH6Ep.yDf4Wk:608:200:Syed Mansoor Sarwar:/home/faculty/msarwar:/bin/bash

$

8 . C r e a t e a d i r e c t o r y c a l l e d m e m o s i n y o u r h o m e d i r e c t o r y . G o in t o t h i s d i r e c t o r y a nd c r e a t e a f i l e m e m o . j a m e s b y us i n g o ne o f t h e e d i t o r s w e d i s c u s s e d in A p pe nd i x A . G iv e t h r e e pa t hn a m e s f o r t h i s f i l e .

Page 3: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

T h e c o mm a n d f o r c r e a t i n g t h e me mo s d i r e c t o r y i s mkdir ~/memos . I f y o u r s y s t e m i s n o t B SD c o m p l i a n t , u se t h e mkdir $HOME/memos c o m m a n d . T h e n u s e t h e cd ~/memos c o m m a n d t o m a k e m e mo s y o u r c u r r e n t d i r e c t o r y . U s e t h e e d i t o r o f y o u r c h o i c e t o c r e a t e t h e m e mo . j a me s f i l e i n t h i s d i r e c t o r y .

T h e t h r e e p a th n a m e s f o r t h i s f i l e a r e g i v e n b e l o w . T h e f i r s t i s t h e a b s o lu t e p a t h n a m e f o r t h e f i l e a n d r e m a i n in g a r e i t s r e l a t i v e p a t h n a m e s .

/ h o me / f a c u l t y / s a r w a r /m e m o s / m e m o . j a m e s

~ / me m o s / m e m o . j a m e s

$ H O M E / m e m o s / me m o . j a me s ( o r $ h o m e /m e mo s / m e m o . j a m e s )

m e m o . j a m e s o r . / m e m o . j a m e s ( i f m e m o s i s y o u r c u r r e n t d i r e c t o r y )

9 . G iv e a c o m m a nd l in e f o r c r e a t i ng a s u bd i r e c t o r y pe r s o na l u nd e r t h e m e m o s d i r e c t o r y t h a t y o u c r e a t e d in pr o b l e m 8 .

mkdir ~/memos/personal

1 0 . M a k e a c o py o f t he f i l e m e m o . j a m e s a n d p u t i t i n y o u r h o m e d i r e c t o r y . N a m e t h e c o p i e d f i l e t e m p. m e m o . G i v e t w o c o m m a n ds f o r a c c o m p l i s h in g t h i s t a s k .

cp ~/memos/memo.james ~/temp.memo

cp $home/memos/memo.james $home/temp.memo

cp $HOME/memos/memo.james $HOME/temp.memo

cp memos/memo.james temp.memo ( i f y o u a r e i n y o u r h o m e d i r e c to r y )

1 1 . G iv e t h e c o m m a n d f o r d e l e t in g t h e m e m o s d i r e c t o r y . Ho w do y o u kn o w t h a t t he d i r e c t o r y h a s be e n de l e t e d?

rmdir ~/memos ( i f t h e m e mo s d i r e c t o r y i s e m p t y )

rm -r ~/memos

I w i l l ma k e m y h o m e d i r e c t o r y m y c u r r e n t d i r e c t o r y a n d r u n t h e ls -l c o m m a n d t o m a k e s u r e t h a t t h e r e i s n o d i r e c to r y c a l l e d m e mo s i n m y c u r r e n t ( h o m e ) d i r e c to r y . Y o u c a n a l s o r u n t h e ls -l ~ c o m m a n d t o p e r f o r m t h e s a me t a s k .

1 2 . W hy d o e s a s h e l l pr o c e ss t e r m i na t e w h e n y o u p r e s s e s <Ctrl-D> a t t h e b e g i n n i n g o f a n e w l i ne ?

T h e p u r p o s e o f a s h e l l p r o c e s s i s t o r e a d s t a n d a r d i n p u t f i l e ( t h e k e y b o a r d b y d e f a u l t ) f o r c o m ma n d s a n d t r y t o e x e c u t e t h e m. <Ctrl-D> i s t h e e n d - o f - f i l e c h a r a c t e r i n L I N U X / U N I X . W h e n y o u p r e s s <Ctrl-D> o n a n e w l i n e , s h e l l t h i n k s t h a t i t h a s r e a c h e d th e e n d - o f - f i l e o f s t a n d a r d i n p u t . I t , t h e r e f o r e , t e r mi n a t e s i t s e l f a s t h e f i l e ( s t a n d a r d i n p u t ) i t r e a d s c o mm a n d s f r o m h a s e n d e d .

1 3 . G iv e a c o m m a nd l in e t o d i s p la y t y pe s o f a l l t he f i l e s i n y o u r ~ / l i nu x d i r e c t o r y t h a t s t a r t w i t h t h e w o r d c h a pt e r , a r e f o l l o w e d b y a d i g i t 1 , 2 , 6 , 8 , o r 9 , a nd e n d w i t h . e p s o r . p r n .

ls -l ~/linux/chapter[1,2,6,8,9].{eps, prn}

1 4 . G iv e a c o m m a nd l in e t o d i s p la y t he t y p e s o f a l l t h e f i l e s i n t h e p e r s o n a l d i r e c t o r y i n y o u r h o m e d i r e c t o r y t h a t d o no t s t a r t w i t h l e t t e r s a , k , G , o r Q a nd t h e t h i r d l e t t e r i n t h e i r n a m e i s n o t a d i g i t a nd no t a l e t t e r ( up pe r c a s e o r l o w e r c a s e ) .

T h e f o l l o w i n g c o mm a n d p e r f o r m s th e g iv e n t a s k u n d e r Ba s h . Re p l a c e t h e ! c h a n r a c t e r w i t h ^ i f t h e c o mm a n d i s t o b e e x e c u te d u n d e r t h e T C s h e l l .

ls -ld ~/personal/[!akGQ]?[!0-9A-Za-z]*

Page 4: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

1 5 . G iv e a c o m m a nd l in e f o r v i e w in g t h e s i z e s ( i n l i ne s a nd b y t e s ) o f a l l t he f i l e s i n y o ur pr e s e nt w o r k in g d i r e c t o r y ?

wc * or wc ./*

1 6 . L i s t 1 0 o p e r a t i o n s t ha t y o u c a n p e r f o r m o n L I N U X f i l e s .

1 . D i s p l a y i n g f i l e s

2 . Co p y i n g f i l e s

3 . M o v in g f i l e s

4 . Co m p a r i n g f i l e s

5 . Re m o v in g f i l e s

6 . P r in t i n g f i l e s

7 . D e t e r m in i n g f i l e t y p e

8 . D e t e r m in i n g f i l e s i z e

9 . F i n d i n g d i f f e r e n c e s b e tw e e n f i l e s

1 0 . D i s p l a y i n g r e p e a t e d o r u n iq u e l i n e s i n a f i l e

1 7 . G iv e a c o m m a nd l in e f o r v i e w in g t h e s i z e s ( i n l i ne s a nd b y t e s ) o f a l l t he f i l e s i n y o ur pr e s e nt w o r k in g d i r e c t o r y ?

Sa m e a s 1 5 . P r o b l e m r e p e a t e d b y m i s t a k e .

1 8 . V i e w t he / us r / i n c l ud e / b i t s / s o c ke t . h f i l e w i t h t h e m o r e ( o r l e s s ) c o m m a n d . W ha t a r e t h e v a l ue s o f s y m b o l i c c o n s t a n t s SO C K _ S TR EA M a n d S OC K_ D GR A M ? W h a t a r e t h e v a l ue s o f P F _ I N E T a nd A F _ I N E T ?

T h e v a lu e o f t h e g i v e n sy m b o l i c c o n s t a n t s a r e

SO CK _ ST R E A M = 1

SO CK _ D G R A M = 2

PF _ I N E T = A F _ I N E T = 2

PF _ I N E T 6 = A F _ I N E T 6 = 1 0

1 9 . W ha t do e s t h e tail -10r ../letter.John c o m m a nd d o ?

T h e c o mm a n d d i s p l a y s i n r e v e r s e o r d e r t h e l a s t 1 0 l i n e s o f t h e . . / l e t t e r . Jo h n f i l e .

2 0 . G iv e a c o m m a nd f o r v i e w i ng t he s i z e o f y o ur ho m e d ir e c t o r y ? G i v e a c o m m a n d f o r d i s p l a y i ng t he s i z e s o f a l l t h e f i l e s in y o ur ho m e d i r e c t o r y .

E x e c u te t h e ls -ld ~ or ls -ld $HOME o r ls -ld $home c o m m a n d a n d l o o k a t t h e f i l e s i z e f i e l d t o d e t e r mi n e t h e s i z e o f y o u r h o m e d i r e c t o r y .

E x e c u te t h e wc ~/* o r wc $HOME/* o r wc $home/* c o m m a n d t o d i s p l a y th e s i z e o f a l l t h e f i l e s i n y o u r h o m e d i r e c t o r y

2 1 . G iv e a c o m m a nd l in e f o r d i sp l a y i ng a l l t h e l in e s i n t h e S t u de nt s f i l e , s t a r t in g w i t h l i n e n um be r 2 5 .

tail +25 Students

2 2 . G iv e a c o m m a nd l in e f o r c o py i n g a l l t h e f i l e s a nd d i r e c t o r i e s u nd e r a d i r e c t o r y c o u r s e s i n y o ur ho m e d ir e c t o r y . A s s um e t h a t y o u a r e in y o ur ho m e d i r e c t o r y . G iv e a no t he r c o m m a n d t o a c c o m pl i s h t he a bo v e t a s k , a s s u m i n g t h a t y o u a r e n o t i n y o u r h o m e d i r e c t o r y .

Page 5: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

cp -r courses .

cp -r ~/courses ~ ( I f y o u r s y s t e m i s n o t B S D c o m p l i a n t , r e p l a c e ~ w i t h $HOME . )

2 3 . W ha t do t he f o l l o w in g c o m m a n ds d o ?

a ) rm -f ~/personal/memo*.doc

Fo r c e t h e r e m o v a l o f a l l t h e f i l e s i n ~ / p e r s o n a l d i r e c t o r y t h a t h a v e . d o c e x t e n s i o n a n d w h o s e n a m e s s t a r t w i t h t h e s t r i n g “ me mo ” .

b) rm -f ~/linuxbook/final/ch??.prn

Fo r c e t h e r e m o v a l o f a l l t h e f i l e s i n ~ / l i n u x b o o k / f i n a l d i r e c t o r y t h a t h a v e . p r n e x t e n s i o n a n d w h o s e n a m e s a r e 4 - c h a r a c t e r l o n g , s t a r t i n g w i t h t h e s t r i n g “ c h ” .

c ) rm -f ~/linuxbook/final/*.o

Fo r c e t h e r e m o v a l o f a l l t h e f i l e s i n ~ / l i n u x b o o k / f i n a l d i r e c t o r y t h a t h a v e . o e x t e n s i o n .

d) rm -f ~/courses/ece446/lab[1-6].[cC]

Fo r c e t h e r e m o v a l o f a l l t h e f i l e s i n t h e ~ / c o u r s e s / e c e 3 4 5 d i r e c to r y t h a t h a v e . c o r . C e x t e n s i o n ( i . e . , C o r C + + s o u r c e f i l e s ) w h o se n a m e s a r e 4 - c h a r a c t e r l o n g , s t a r t i n g w i th t h e s t r i n g “ l a b ” a n d e n d i n g w i t h a d i g i t 1 - 6 .

2 4 . G iv e a c o m m a nd l in e f o r m o v in g f i l e s l a b1 , l a b 2 , a nd l a b3 f r o m ~ / c o u r s e s / e c e 3 4 5 d i r e c t o r y t o a n e w la b s . e c e 3 4 5 d i r e c t o r y in y o ur ho m e d i r e c t o r y . I f a f i l e a l r e a dy e x i s t s i n t he de s t i na t io n d i r e c t o r y , t he c o m m a nd s ho u l d p r o m pt t h e u se r f o r c o n f i r m a t i o n .

mv -i ~/courses/ece345/lab[123] ~/newlabs.ece345

2 5 . G iv e a c o m m a nd t o d i s p l a y t h o s e l i ne s in t h e ~ / p e r s o n a l /P h o n e s d i r e c t o r y t ha t a r e no t r e p e a t e d .

uniq -u ~/perosnal/Phones

2 6 . Y o u ha v e a f i l e i n y o u r h o m e d i r e c t o r y c a l l e d t r y i t & . R e na m e t h i s f i l e . W ha t c o m m a nd d id y o u us e ?

Si n c e & i s a s h e l l me t a c h a r a c t e r , w e n e e d t o e s c a p e i t i n t h e mv c o m m a n d w i t h t h e \ c h a r a c t e r .

mv tryit\& tryit_new

2 7 . G iv e a c o m m a nd l in e f o r d i sp l a y i ng a t t r i b ut e s o f a l l t h e f i l e s t a r t i n g w i t h a s t r i n g pr o g , f o l l o w e d by z e r o o r m o r e c ha r a c t e r s a n d e nd i ng w i t h a s t r i ng . c in t h e c o ur se s / e c e 3 4 5 d i r e c t o r y i n y o u r h o m e d i r e c t o r y .

ls -l ~/courses/ece345/prog*.c

2 8 . R e f e r t o p r o b le m 2 7 . G i v e a c o m m a n d l i ne i f f i l e n a m e s h a v e t w o E ng l i s h l e t t e r s be t w e e n pr o g a nd . c . C a n y o u g i v e a n o t he r c o m m a n d l i n e t o a c c o m pl i s h t h e s a m e t a s k ?

ls -l ~/courses/ece345/prog[aA-zZ][aA-zZ].c

ls -l ~/courses/ece345/prog[a-zA-Z][a-zA-Z].c

2 9 . G iv e a c o m m a nd l in e f o r d i sp l a y i ng f i l e s c a l l e d g o t | c h a a nd M * A * S * H s c r e e n f u l a t a t i m e .

E s c a p e t h e s p e c i a l m e a n i n g o f t h e | c h a r a c t e r b y u s i n g t h e \ c h a r a c t e r .

Page 6: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

more got\|cha M*A*S*H

3 0 . G iv e a c o m m a nd l in e f o r d i sp l a y i ng t h e s i z e s o f f i l e s t ha t ha v e t h e . j p g e x t e n s io n a nd n a m e s e nd i ng w i t h a d ig i t .

wc *[0-9] . jpg3 1 . W ha t i s t he f i l e c o m pr e s s i o n? W h a t d o t h e t e r m s c o m p r e s s e d f i l e s a nd

de c o m pr e s s e d f i l e s m e a n? W h a t c o m m a n ds a r e a v a i l a b l e f o r p e r f o r m in g t h e c o m pr e s s i o n a n d d e c o m p r e s s i o n in L I N U X ? W h i c h a r e t he pr e f e r r e d c o m m a nd s ? W hy ?

Re d u c t i o n i n t h e s i z e o f a f i l e i s k n o w n a s f i l e c o m p r e s s i o n . C o m p r e s s i o n h a s b o t h s p a c e a n d t i m e a d v a n t a g e s . A c o m p r e ss e d f i l e t a k e s l e s s d i s k s p a c e , l e s s t i m e t o t r a n s m i t , a n d l e s s t im e to c o p y .

A c o m p r e s s e d f i l e c o n t a i n s t h e c o n t e n t s o f a f i l e a f t e r t h e y h a v e b e e n c o mp r e s s e d a n d a d e c o m p r e s s e d f i l e c o n t a i n s t h e c o n t e n t s o f t h e o r i g in a l f i l e ( a f t e r a c o m p r e s s e d f i l e h a s b e e n d e c o m p r e ss e d ) .

T h e r e a r e s e v e r a l L I N U X c o m m a n d s t h a t c a n b e u s e d f o r c o m p r e s s i n g a n d d e c o mp r e s s i n g f i l e s , n a m e l y compress , uncompress , gzip , a n d gunzip . T h e p r e f e r r e d c o m ma n d s a r e gzip a n d gunzip b e c a s u e t h e y w o r k b e t t e r t h a n o t h e r s . T h e gzexe c o m m a n d c a n b e u s e d to c o m p r e s s ( a n d u n c o m p r e s s ) e x e c u t a b l e f i l e s .

3 2 . Ta k e t hr e e l a r g e f i l e s i n y o ur d i r e c t o r y s t r uc t u r e : a t e x t f i l e , a P o s t S c r i p t f i l e , a nd a p i c t u r e f i l e — a nd c o m pr e s s t h e m by u s in g t h e compress c o m m a nd . W h ic h f i l e w a s c o m p r e s s e d t he m o s t ? W h a t w a s t he pe r c e nt a g e r e d uc t i o n i n f i l e s i z e ? C o m p r e s s t h e s a m e f i l e by u s i ng t he gzip c o m m a nd . W h ic h r e s u l t e d i n b e t t e r c o m pr e s s i o n , compress o r gzip? U n c o m p r e s s t h e f i l e s w i t h t h e uncompress a nd gunzip c o m m a nd s . S ho w y o u r w o r k .

D o i t y o u r s e l f .

3 3 . U s e t he find c o m m a nd t o d i s p l a y t h e n a m e s o f a l l t he he a de r f i l e s i n t h e / us r / i n c l ud e d i r e c t o r y t h a t a r e l a r g e r t h a n 1 0 0 0 b y t e s . W r i t e d o w n t h e c o m m a n d t h a t y o u us e d t o p e r f o r m t h i s t a s k .

T h e f o l l o w i n g s e s s i o n s h o w s a sa m p le r u n o f t h e c o m m a n d f o r p e r f o r m i n g th e g iv e n t a s k . N o t e t h a t t h e c o m ma n d o u tp u t i n c l u d e s d i r e c t o r i e s .

$ find /usr/include -size +1000c -print | more

/usr/include

/usr/include/pwdb

/usr/include/pwdb/_pwdb_macros.h

/usr/include/pwdb/pwdb_map.h

/usr/include/pwdb/pwdb_public.h

/usr/include/pwdb/pwdb_radius.h

/usr/include/pwdb/pwdb_shadow.h

/usr/include/pwdb/pwdb_unix.h

/usr/include/pwdb/radius.h

/usr/include/asm

$

Page 7: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

T h e f o l l o w i n g s e s s i o n s h o w s t h e c o mm a n d th a t d i s p l a y s t h e t o t a l n u m b e r o f f i l e s i n t h e / u s r / i n c l u d e d i r e c t o r y t h a t a r e l a r g e r t h a n 1 0 0 0 b y te s .

$ find /usr/include -size +1000c -print | wc -l

2849

$

3 4 . U s e t he find c o m m a nd t o r e m o v e a l l t he f i l e s i n y o u r h o m e d i r e c t o r y n a m e s c o r e a nd t h o s e h a v i ng t h e . b a k e x t e ns i o n . W ha t c o m m a nd d i d y o u u s e ?

T h e f o l l o w i n g s e s s i o n s h o w s t h e c o mm a n d f o r p e r f o r mi n g t h e g i v e n t a s k .$ find ~ \( -name core -o -name ‘*.bak’ \) -print -exec rm {} \;

[ output of the command ]

$

3 5 . U s e t he whereis c o m m a nd t o l o c a t e t he m a n ua l pa g e s f o r t he tar , strcmp , a nd socket c o m m a nd s / c a l l s . W ha t a r e t he a bs o l u t e p a t h na m e s f o r t h e f i l e s t ha t c o n t a i n t he s e m a n u a l p a g e s .

T h e f o l l o w i n g s e s s i o n s h o w s t h e c o mm a n d f o r p e r f o r mi n g t h e g i v e n t a s k . T h e a b s o l u t e p a t h n a me s f o r t h e ma n u a l p a g e s a r e h i g h l i g h t e d .

$ whereis tar strcmp socket

tar: /bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz

strcmp: /usr/share/man/man3/strcmp.3.gz

socket: /usr/share/man/man2/socket.2.gz /usr/share/man/man7/socket.7.gz /usr/share/man/mann/socket.n.gz

$

3 6 . U s e t he grep c o m m a nd t o s e a r c h t h e / u s r / i nc lu d e / b i t s / s o c k e t . h f i l e a nd d i s p la y t h e l i ne s t ha t c o n t a i n t he s t r i n g SO C K _ . W h a t c o m m a n d l i ne d i d y o u u s e ?

T h e f o l l o w i n g s e s s i o n s h o w s t h e c o mm a n d f o r p e r f o r mi n g t h e g i v e n t a s k , a l o n g w i t h t h e c o m m a n d o u t p u t .

$ cat /usr/include/bits/socket.h | grep SOCK_

SOCK_STREAM = 1, /* Sequenced, reliable, connection-based

#define SOCK_STREAM SOCK_STREAM

SOCK_DGRAM = 2, /* Connectionless, unreliable datagrams

#define SOCK_DGRAM SOCK_DGRAM

SOCK_RAW = 3, /* Raw protocol interface. */

#define SOCK_RAW SOCK_RAW

SOCK_RDM = 4, /* Reliably-delivered messages. */

#define SOCK_RDM SOCK_RDM

SOCK_SEQPACKET = 5, /* Sequenced, reliable, connection-based,

Page 8: easysemester.comeasysemester.com/sample/Solution-Manual-for-LINUX... · Web viewFor example, C compilers require the .c extension, C++ compilers require the .cpp extension, and the

#define SOCK_SEQPACKET SOCK_SEQPACKET

SOCK_PACKET = 10 /* Linux specific way of getting packets

#define SOCK_PACKET SOCK_PACKET

$