Unix Unit 1 Part 2
Unix Unit 1 Part 2
.· T h e F il e System
I
In this chapter, we begin our study of one of the two pilla
rs that supp ort UN IX- the file sys~cm.
UNIX looks at everything as a file and any UNIX syste
m has thou sand s of files. If you wntc l
program, you add one more file to the system. When you
compile it, you add some more. Files
grow rapidly, and if they are not organized properly, you'll
find it difficult to locate them. Just as an
office has separate file cabinets to group files of a similar
nature, UN IX also organizes its own files
in directories and expects you to do that as well. _. _ _
The file system in UNIX is one of its simple and conceptu
;lly clean features. It lets users a~ms
other files not belonging to them, but it also offers an adeq
uate security mechanism so ~utsiders
arc not able to tamper with a file's contents. In this chapter,
you'll learn to create directories! move
around within the system, and list filenames in these direc
tories'. We'll deal with file attnbutcs,
including the ones related to s~curity, in~ later chapter._
• •• ' :'
I • ---i
WHATYOUWILLLEARN ·-- ,~ --. •
• .. '.
• The initial categorization of files into three type's--0nlinar
y~ directory and device.
• The features of a UNIX filename. - • r ...... - . '. '.
• The hierarchical structure containing files and directorie
s, and the pare nt-c hild relationship
that exists between them.
.,
• The significance of the home directory and HOME variable.
• Navigate the file system with the cd and pwd commands
.
• Create and remove directories with mkdir and nndir.
• The significance of absolute and relative pathnames.
• U_se ls to list filenames in a directory in different formats.
A binary file, on the other hand, contains both printable and unprintable characters that cover the
entire ASCII range (0 to 255). Most UNIX commands are binary files, and the object code and
executables that you produce by compiling C programs are also binary files. Picture, sound and
video files are binary files as well. Displaying such files with a simple cat command produces
unreadable output and may even disturb your terminal's settings.
Ii) UNIX: Concepts and Applications
Caution: Never use a - at the beginning of a filename. You'll have a tough time getting rid of it!
A command that uses a filename- as argument often treats it as an option and reports errors. For
instance, if you have a file named -z, cat -z won't display the file but interpret it as an invalid option.
11
s pwd
/home/kumar
$ cd progs progs must be in current directory
$ pwd
/home/kumar/progs
Though pwd displays the absolute pathname, cd doesn't need to use one. The command cd Pl'lt
here means this: "Change your subdirectory to progs under the current directory." Using a path
causes no harm either; use cd /home/kumar/progs for the same effect.
When you need to switch to the /bin directory where most of the commonly used UNIX comma~
are kept, you should use the absolute pathname:
$ pwd
/home/kumar/progs
$ cd /bin Absolute pathname required here becaua
$ pwd bin' isn't in currmt directory
/bin '
I .
We can also navigate to /bin (or any directory) using a different type of pathname; we are comini
to that shortly. .
cd can also be used without any arguments:
s pwd
/home/kumar/progs
$ cd cd used without argumenll
$ pwd reverts to the home directory
/home/kumar
I
Attention, DOS users! This command invoked without an argument doesn't indicate the ~~rrcnl
directory. It simply switches to the home directory, i.e., the directory where the user ongmally
logged into. Therefore, ifyou wander around in the file system, you can force an immediate return
to your home directory by simply using cd:
$ cd /home/sharma
$ pwd
/home/shanna
$ cd Returns to home directory
$ pwd
/home/kumar
The cd command can sometimes fail if you don't have proper permissions to access the directori
This doesn't normally happen unless you deliberately tamper with the permissions ofthe directo~
The technique of doing that is described in Section 6.5. .
Note: Unlike in DOS, when cd is invoked without arguments, it simply reverts to its home directory. It
doesn't show you the current directory!
The File System t71 I
4.7 mkdir: MAKING DIRECTORIES
'
Directories are created with the 111kdir (make directory) command. The command is followed by
names of the directories to be created. A directory patch is created under the current directory like
this:
mkdir patch
You can create a number of subdirectories with one 11kdir command:
mkdir patch dbs doc Three directories created
So far, simple enough, but the UNIX system goes further and lets you create directory trees with
just one invocation of the command. For instance, the following command creates a directory tree:
mkdir pis pis/progs pis/data Creates /he directory tree
This creates three subdirector ies-pis and two subdirectories under pis. The order of specifying
th1; arguments is important; you obviously can't create a subdirectory before creation of its parent
directory. For instance:, you can't enter
$ mkdir pis/data pis/progs pis ..
mkdir: Failed to make directory •pis/data"; -'No such file or directory
mkdir: Failed to make directory "pis/progs" ; No such file or directory
Note that even though the system failed to create: the two subdirectories, progs and data, it has still
created the: pis directory.
Sometimes, the system refuses to create: a directory:
$ test
mkdir: Failed to make directory •test"; Pennission denied
This can happen due: to these: reasons:
• The: directory test may already exist.
• There may be an ordinary file by that name: in the current directory.
• The permissions set for the current directory don't permit the creation of files and directories
by the user. You'll most certainly get this message if you try to create a directory in /bin, /etc
or any other directory that houses the UNIX system's files.
We'll take up file and directory permissions in Chapter 6 featuring file attributes.
Like mkdir, nndir can also delete more than one directory in one shot. For instance, _the t~,.
directories and subdirectories that were just created with mkdi r ca~ be removed by ustng Ill~
with a reversed set of arguments: . I
rmdir pis/data pis/progs pis
Note that when you delete a directory and its subdirectories, a ~everse logic has to be applied.
following directory sequence used by mkdi r is invalid in ~di r:
$ nndir pis pis/progs pis/data
rmdi r: di rectory II pis 11 : Di rectory not empty
Have you observed one thing from the error message? nndi r has silently deleted the lowest ICl't
subdirectories progs and data. This error message leads to two important rules that you shou~
remember when deleting directories:
• You can't delete a directory with rmdirunless it is empty. In this case, the pis directory couldn't
be removed because of the existence of the subdirectories, progs and data, under it.
• You can't remove a subdirectory unless you a~e placed in a directory which is hierarchicalli
above the one you have chosen to remove.
The first rule follows logically from the example above, but the highlight on nndi r has significanct
that will be explained later. (A directory can also be removed without using rmdi r.) To illustrate tht
second cardinal rule, try removing the progs directory by executing the command from the same
directory itself: •
$ cd progs
$ pwd
/home/kumar/pis/progs
$ nndir /home/kumar/pis/progs
11
Trying to remove the cun-ent directory
rmdir: directory /home/kumar/pis/progs": Directory does not exist
To remove this directory, you must position yourself in the directory above progs, i.e., pis, and
then remove it from there:
$ cd /home/kumar/pis
$ pwd
/home/kumar/pis
$ nndfr progs
The mkdf rand rmdi r commands work only in directories owned by the user. Generally, a user is the
owner of her home directory, and she can create and remove subdirectories (as well as regular files)
in this directory or in any subdirectories created by her. However, she normally. won't be able to
create or remove files and directories in other users' directories. The concept of ownership wiJI be
discussed in Section 6.3. ·
Note: A subdirectoiy can't be removed with rmdir unless it's empty, and one is positioned in its parent
directoiy or above it. But we can remove a directoiy without using rmdf r also (discussed later).
r73I
The File System --1
Navigatio n often becomes easier b-y using a common ancestor (here, /home) as refcrePcc. UNIX
offers a shortcut -the relative pathnam e-that uses either the current or par~nt directory as
reference , and specifies the path relative to it. A relative pathname uses one ofthese cryptic symbols:
• . (a single dot}-Th is represents the current directory.
• .. (two dots)-Th is represents the parent directory.
We'll now use the .. to frame relative pathnam es. Assumin g that you arc placed in
/home/ku mar/prog s/data/tex t, you can use .. as an argument to cd to move to the parent directory,
/home/ku mar/progs /data:
$ pwd
/home/ku mar/prog s/data/tex t
$ cd . . Moves one level up
$ pwd
/home/ku mar/progs /data
This method is compact and more useful when ascending the hierarchy. The command cd
translates to this: "Change your directory to the parent of the current directory." You can combine
any number of such sets of .. separated by /s. However, when a / is used with .. it acquires a
different meaning; instead of moving down a level, it moves one level itp. For instance, to move to
/home, you can always use cd /hOllle. Alternatively, you can also use a relative pathname :
$ pwd
/home/kum ar/pis
$ cd .. /.. Moves two levels up
$ pwd
/home
Now let's turn to the solitary dot that refers to the current directory. Any command which uses the
current directory as argument can also work with a single dot. This means that the cp command
(5.2) which also uses a directory as the last argument can be used with a dot:
cp •• /sharma/ .profile . Afilenam ecan begin with a dot
This copies the file .profile to the current directory (.). Note that you didn't have to specify the
filename of the copy; it's the same as the original one. This dot is also implicitly included whenever
we use a filename as argument , rather than a pathname . For instance, cd progs is the same as
cd ./progs.
Note: Absolute pathnames can get very long if you are located a number of "generations" away from
root. However, whether you should use one depends solely on the number of keystrokes required when
compared to a relative pathname. In every case here, the relative pathname required fewer key depressions.
Depending on where you are currently placed, an absolute pathname can be faster to type.
J
1
lz!J f
UNIX: Concepts and PPlications
$ ls
08 packets.html Numer als first
TDC.sh Uppercase next
calendar Then lowercase
cptodos.sh
dept.1st
emp. l st
helpdir
progs
usdsk06x
usdsk07x
usdsk08x
What you see here is a complete: list of filenames in the: curren t direct ory arr~ngcd
in ASQ
collating sequence (numbers first, uppercase and then lowercase:), with one filenam
e in cad.
line. It includes directories also, and ifyou arc using Linux, you would probab ly sec the
dircctoric'
and ordinary filcs•in different colors.
µN~ _If your Linux_ system ~·t~the se.~ ~~ ~·sure·that ·you
Joggingm: . . ·:..!. . ,.. • • . ,.. . ~... , •
create 'ihis alias~I
!
,
alias l·s•'ls --color-:tty' ·~·--· :::·/ .--;,·•. ~·' _·.. :.- • \ ,· •• '. • . ,; "' .I
~ar e ~ m__~;}~ ;~~. ·~~; :~-~ aren otsu ~ ~-~e B o
~
Directories often contain many files, and you may simply be interc:stc:d in only knowing
whethm
particular file is available. In that case, just use 1s with the: filename:
$ ls calendar
calendar
and ifperl isn't availa!j,c, thc_systcm clearly says so:
$ Is perl
perl: No such file or directory
1scan also be used with multiple filenames, and has options that list most of the: file
attributes. In
the following sections, you'll sec some of these options.
4.11.11s Options
Is _has a large number of options (Table 4.I), but in this ·chapter, we'll presen t a handful of
them.
The other options will be taken up in later chapters. The section numbers arc appropriately
indicated
in the table.
Output in Multiple Columns (-x) When you have several fil~s, it's better to display the
filenames
in multiple columns. Modern versions ofl s do that by default (i.e., when used withou
t options),
but ifthat doesn't happen on your system, use the -x option to produc~ _a J_DUlticolumn
ar output:
s 1s -x
The File System r77
L I
08 packets.htm l T0C.sh calendar cptodos.sh
dept.1st emp.lst helpdir progs
usdsk06x usdsk07x usdsk08x ux2nd06
If your system needs to use the -x option to display multicolumn ar output, you can later customize
the command to display in this format by default (10.4).
Identifying Directories and Executables (-F) The output of 1s that you have seen so far merely
showed the filenames. You didn't know how many of them, if any, were directory files. To identify
directories and executable files, the -F option should be used. Combining this option with -x produces
a rnulticolumn ar output as well:
$ ls -Fx
08 packets.htm l TOC.sh* calendar* cptodos.sh*
dept.1st emp. l st helpdir/ progs/
usdsk06x usdsk07x usdsk08x ux2nd06
Note the use of two symbols, • and /, as type indicators. The * indicates that the file contains
executable code and the / refers to a directory. You can now identify the two subdirectories in the
current directory-h el pdi r and progs. •
Showing Hidden Files Al.so (-a) ls doesn't normally show all files in a directory. There are
cenain hidden files (filenames beginning with a dot), often found in the home directory, that
normally don't show up ~n the listing. The -a option (all) lists all hidden files as well:
$ ls -axF
./ .. / .exrc .kshrc
.profile . rhos ts . sh hi story .xdtsupCheck
.xfnitrc 08_packet~.html* roc:sh* calendar*
The file . profile contains a set of instructions that are performed when a user logs in. It is
conceptually similar to AUT0EXEC. BAT of DOS, and you'll know more about it later. The other file,
.exrc, contains a sequence ofstartup instructions for the vi editor. To display these hidden filenames,
you can either use the -a option or specify the filenames in the command line. •
The first two files (. and .. ) are special directories. Recall that we used the same symbols in relative
pathnames to represent the current and parent directories (4.10.1). These symbols have the same
meaning here. Whenever you create a subdirectory, these "invisible" .directories are created
automatically by the kernel. You can't remove them, nor can you write into them. They help in
holding the file system together. •
Note: All filenames beginning with a dot are displayed only when 1s is used with the -a option. The
directory . represents the current directory and .. signifies the parent directory. I
Listing Directory Contents, . l!1 ·t9e las·t example, you specifi,ed some ordinary filenames to 1s to
have a selective listing. However, the situation will be quite different ifyou specify tl).e two directory
names, hel pdf r and progs, inslead:,
, I!~1 UNIX: Concepts and Applications
, $ ls -x helpdir progs
helpdir:
fonns.obd graphics.obd reports. obd
progs:
array .pl cent2fah.pl n2words.pl name.pl
This time the contents of the directories are listed, consisting of the Oracle documentation in tht
he1pdi r directory and a number of perl program files in progs. Note that ls, when used with
directory names as arguments, doesn't simply show their names as it does with ordinary files.
Recursive Listing (-R) The -R (recursive) option lists all files and subdirectories in a directory
tree. Similar to the DIR /S command of DOS, this traversal of the directory tree is done recursively
until there are no subdirectories left:
$ ls -xR .
0B_packets.html T0C.sh calendar cptodos.sh
dept.1st emp.lst helpdir progs
usdsk06x usdsk07x usdsk0Bx _ ux2nd06
./helpdir:
forms.hlp graphics. hl p reports.hlp
./progs:
array.pl cent2fah.pl n2words.pl name.pl
The list shows the filenames in three sections-the ones under the home directory and those
under the subdirectories he1pdi rand progs. Note the subdirectory naming conventions followed;
./helpd ir indicates that-helpdir is a subdirectory under. (the current directory). Since
/home/kumar happens to be the current directory, the absolute pathname of this file expands to
/home/kumar/helpdir.
I
I
,,. •·\
Han dlin g Ord inar y File s
The last chapter examined the tools that handle directories. But users actually do most of their work
with ordinary (or regular) files, and it's natural that the UNIX system should feature a host of
commands to handle them. Although all of these commands use filenames as arguments, they were
not designed only to read files. In fact, many of them don't need to read a file at all. However, to
understand their basic functionality, we'll use them with filenames in this chapter.
We'll first consider the common file-handlin g commands that the DOS environmen t also offers,
except that the UNIX variety has more features. We'll also discuss those commands that show
differences between two files and convert files between DOS and UNIX formats. Finally, we'll
examine the important compression utilities with which we handle documents and software found
on the Internet. As we progressively discover the shell's features in later chapters, we'll learn to use
the same commands in other ways.
WHAT You WILL LEARN
1
• View text files with cat and more (or less)
• Use cat to create a file.
• The essential file functions--c opy with cp, remove with rm and rename with 111v.
• Print a file with lp (lpr in Linux).
• Classify files with file.
• Count the number oflines, words and characters with we.
• Display the ASCII octal value of text with od.
• Compare two files with~. COIII and diff.
• Compress and decompress files with gztp and gunzip.
• Create an archive comprising multiple files with tar.
• Perform both functions (compressin g and archiving) with zip and unzip.
Note: The {Ctrl-d] character is used to terminate input not only with cat, but with all commands that
accept input from the keyboard. ' I
cat is a versatile command. It can be used to create, display, concatenate and append to files. More
importantl)I it doesn't restrict itselfto handling files only; it also acts on a stream. You can supply the
input to cat not only by specifying a filename, but also from the output of another command. You'll
learn about all this in Chapter 8.
We'll conti nue to use the* as a short hand for multiple filena
mes. Ther e are other mctacharacters
too, and they are discussed in complete detail in Section 8.3.
Note: Whether or not you are able to remove a file depends, not on the file's permissions, but on the
Permissions you have for the directory. Directory permissions are taken up in Section 6.6.
5.3.1 m Options
Interactive Deletion (-i) Like in cp, the -i (interactive) option makes the command ask the user
for confirmation before removing each file:
$ rm -1 chapOl chap02 chap03
rm: remove chapOl (yes/no)? ?y
rm: remove chap02 (yes/no)? ?n
nn: remove chap03 (yes/no)? [Enter} tNo response-file not deleted
Ay removes the file, a~y other response leaves the file undeleted.
Recursive Deletion (-ror -R) With the -r (or -R) option, ra performs a tree walk-a thorough
recursive search for all subdirectories and files within these subdirectories. At each stage, it deletes
everything it finds. I'll won't normally remove directon·es, but·• used with this option, it will.
Therefore, when you issue the command
nn -r * Behaves partiaily like rmd f r
you'll delete all files in the current dir~ctory and all its subdirectories. Ifyou don't have a backup,
then these files will be lost forever. .--/'
Forcing Removal (-f) -------
I'll prompts forremoval ifa file is ~rite-protected. The -f option overrides
this minor protection and forces removal When you combine it with the -r option, it could be the
most risky thing to do:
nn -rf * Deletes everything in the a'"ent directory and below
. ,,I • \
I
yep, mv a
ii zj
. As shown i~ Fig. 5.1, cp
cp, 11v and r11 w_ork by modifying the directory entries of the files they access
number that is allotted by
adds an entry tp the directory with the name of the destination file and inode rm
t disturbing its inode number.
the kernel. ,replaces the name of an existing directory entry withou 1
removes ~m an entry from l;he pirectory.
ation are in the same directory.
This is a ratheri simplistic view, a~d is true only when source and destin
disk, the file is actually moved.
When you "mv~ a fitJ to directory that resides on a separate hard
systems create the illusion of a;
You'll. appreciate this better after you have understood how multiple file
' ·, '
single file system on your UNIX machine.
I
\ aa · UNIX: Concepts and Applicatio
ns
mv too bar
Filename lnode
Filename /node
Nu mb er
Number
386444 1'111 foo. bak 386444
.. 417585 .. 417585
bar 499770 ,.
bar 499770
foo.bak 509876
Fig. 5.1 Directory Status after cp,
mv and rm
Th e act ion of na also needs
entry. There could be "sim
to be studied further. A file is
not actually removed by de Ietin g its directory
. file in this or
ilar" entries (ones having the
another directoiy. We'll exa same inode number) .fo r th
mine this directoiy table aga ~ n we take up
file attributes in Chapter 11. in along with its pen nis sIo ns
w
----
5.5 more: PAGING OUTPUT
The: man command dis . . .
plays its output a page at a tim e. This is possible because 1t. •• d its output to
a pager p~o~ram. UNIX offers the sen s placed
.more pager (originally from Berkel
pg, the original pager of UNIX. ey) which has tod~l;~iscuss
Linux also offers more but 1ess is its
more in this section and note the standard pager.
exclusive features of less separately
in the aside on Linux.
To view the file chapOl, enter the
command with the filename:
more chapOl
Press q to exit
You'll see the contents of chapOl
on the screen, one page at a time. As.
also see the filename and percen the bottom ofthe screen, you'II
tage of the file that has been viewe
d:
--More--{17%)
The AT&T and BSD versions of more differ widely in their capabilities and command usage. The
POSIX specification on more is based on the BSD version. You have to try out the commands
shown in Table 5.1, as well as look up the man pages, to know whether they apply to your system.
110re has a fairly useful help screen too; hitting an h invokes this screen.
5.5.1 Navigation
Irrespective of version, more uses the spacebar to scroll forward a page at a time. You can also scroll
by small and large increments oflines or screens. To move forward one page, use
f or the spacebar
and to move back one page, use
b
Note: The search capability in 110re is not restricted to simple strings. Like many UNIX commands
(grep, sed and vi), more lets you use a regular expression to match multiple similar strings. Regular
expressions are discussed in several chapters of this text beginning with Chapter 13.
~U X : Pri~ting with 1 pr
-.._ •~ 1',• •
I
"'·
• . •
Unux uses Ber~\ey's pri us t tia 1
your nting syste
printer configured properly m ~c h supports the lp~ c~mmand for printing.
beforeyou can use l pr. Th ~o
job number: e command normally d~ sn ~r :,o Ut ~1
' • • • •
\l pr /et c/g ro ~p • ·,. •.
. •• •. ··_ .• . ••..'~-
••. •• _
Unux has a rich se t of too
ls that convert tett files to
Postscript Check whether yo
:z ps ~r enscrtpt o n ~
sy st em ._ ~ eventually
~aU~p lpr; yo~ don't have
u have the p ur
to do ~a t 0 :: u t to a
4
Systei_n V, you can print a
specific: nu er. of copies, ch
sp ec ific printer. You
I can also mail completionmb of the job:··· •
oose the title an d direct
. . i]
lp r -P hp4500 fo o. ps . ., •
t lp r -T •th e Li st of RF • • . . Printsonprinterhp4500
lp r -13 fo o. ps cs• fo o. ps •
•• :
• • • Us es thi s title .
i 1pr _-m foo. ps . . '. . . • .-. . . J
. .
'pq displays the print qu . . .
. • .
·
•..
•
,
• . Prints3 copies • •. •
. .. Mails message after
. completion
.
to
I
eue showing job numb ers. Using one or more job nu ents
l
mbers as argufl'I 1
t~ lc a n ~ e ~ ~
:. ~ t~ u e ~ ~ ~ ~
I lp nn - jo b ·s R :: ~ ~ 3
. · . (·· .··, •• •
. .. • .. Re
The 1pc command is mo oe sa U. Jobsownedby user
by th e administrator to co
nfigure the printer. We'I no
t dlsCU55 prfrtl~
5.7 fi le : KNOWING TH ..,..._. - , : : . . , . ~
E FILE TYPES
Even though we know (so
far) that files arc of three typ
these files. For instance, es, you may often need to know
a regular file may c~ntain more about
UN IX provides the f11 e plain text, a C p~ogram or
command to determine the executable code.
can use it with one or mo type offile, especially ofan ord
re filenames as arguments: inary file. Y~u
$ fi le archive.zip
archive.zip: ZIP archive
_____..