Adobe Scan 02 Jan 2023

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

The File System

In this chapter, we begin our study of one of the two pillars that support UNIX-the file svste.

UNIX looks at everything as a


any UNIX system has thousands of files. If you ystem
file and
file to the
write.
te a
program, you add one more
system. When you compile it, you add some more.
File
Orow rapidlv, 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, UNIX also organizes its own
in directories and expects you to do that as well.
file
The file system in UNIX is one of its simple and conceptually clean features. It lets users
access
other files not belonging to them, but it also offers an
adequate security mechanism so outsiders
are 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 directories. We'll deal with file
attributes,
including the ones related to security, in a later chapter.

WHAT You WiLL LEARN


.The initial categorization of files into three types-ordinary, directory and device.
.The features of a UNIX filename.
The hierarchical structure containing files and directories, and the parent-child 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 rmdir.
. The significance of absolute and relative pathnames.
. Use 1s to list filenames in a directory in different formats.

TOPICS OF SPECIAL INTEREST


.The significance ofthe important directories of the UNIX file system from a functional point
of view.
. How It Works: A graphic that shows how mkdir and rmdir affect a directory.
The File System 65

4.1 THE FILE


The file is a container for storing information. As a first approximation, we can treat it simply as a
scquence of characters. It you name a file foo and write three characters a, b and cinto it, then too
will contain only the string abc and nothing else. Unlike the old DOS files, a UNIX file doesnt
contain the cot (end-ot-file) mark. A file's size is not stqred in the file, nor even its name. All file
attributes are kept in a separate area of the hard disk, not directly accessible to humans, but oly to
the kernel.

UNIX treats directories and devices as files as well/A directory is simply a folder where you store
filenames and other directories) All physical devices like the hard disk, memory, CD-ROM, printer
and modem are treated as files. The shell is also a file, and so is the kernel. And ifyou are wondering
how UNIX treats the main memory in your system, it's a file too!
So we have already divided files into three categories:

Ordinary filc-Also known as regularfile. It contains only data as a strcam of characters.


.Directory file-lt's commonly said that a directory contains files and other directories, but
strictly speak1ng, it contains their names and a number associated with each name.

Device file-Alldevices and peripherals are represented by files. To read or writea device, you
have to pertorm these operations on its associated file.
There are other types of files, but we'll stick to these three for the time being. The reason why we
make this distinction between file types is that the significance of a file's attributes often depends
on its type. Read permission for an ordinary file means something quite different from that for a
directory. Moreover, you can't directly put something into a directory file, and a device file isn't
really a stream of characters. While many commands work with all types of files, some don't. For a
proper understanding of the file system you must understand the significance of these files.

4.1.1 Ordinary (Regular) File


An ordinary file or regular file is the most common file type. All programs you write belong to
this type. An ordinary file itself can be divided into two types:

.Text file
.Binary file
A text file contains only printable characters, and you can often view the contents and make sense
Out of them, All C and Java program sources, shell and perl scripts are text files. A text file contains
lines of characters where every line is terminated with the newline character, also known as linefeed
L). When you press (Enter/ while inserting text, the LF character is appended to every line. You
Won 't see this character normally, but there is a command (od) which can make it visible.

A binary file, on the other hand, contains both printable and unprintable characters that cover the
Cntire ASCII range (0 to 255). Most UNIX commands are binary files, and the object code and
are also binary files. Picture, sound and
CXecutables that you produce by compiling C programs
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.
66 UNIX: Concepts and Applications
4.1.2 Directory File
A directory contains no data, but keeps some details of the files and subdirectories that it cor
that it contains
The UNIX file system is organized with a number of directories and subdirectories, and Vor
also create them as and when you necd. You often necd to do that to group a set of files ou Can
pertainin
to a specitic application. This allows two or more iles in separatc directories to have the sa
same
filename.
A directory file contains an entry for every file and subdirectory that it houses. Ifyou have 20 l.
in a directory, there will be 20 cntries in the directory. Each entry has two components
files
.Thefilename.
.A unique identification number for the file or directory (called the inode number).
Ifa directory bar contains an entry for a file foo, we commonly (and loosely) say that the directors
bar
contains the file Though
foo. we'll be
often
using the phrase "contains the fle" rather tha
"contains the filename", you must not interpret the statement literally. A directory contains
the
filename and not thefile's contents.
You can't write a directory file, but you can perform some action that makes the kernel
write a
directory. For instance, when you create or remove a file, the kernel automatically updates its
corresponding directory by adding or removing the entry (inode number and filename) associated
with the file.
Note: The name of a file can only be found in its directory; the file itself doesn't contain its own name or
any of its attributes, like its size or access rights.
4.1.3 Device File
You'll also be printing files, installing software from CD-ROMs or backing up files to tape. All of
these activities are performed by reading or writing the file representing the device. For instance,
when you restore files from tape, you read the file associated with the
tape drive. It is advantageous
to treat devices as files as some of the commands used to access an ordinary file also work with
device files.
Device filenames are generally found inside a single directory structure, /dev. A device file is indeed
special; it's not really a stream of characters. In fact, it doesn't contain anything at all. You'll soon
learn that every file has some attributes that are not stored in the file but elsewhere on disk. The
operation of a device is entirely governed by the attributes of its associated file. The kernel identifies
a device from its attributes and then uses them to operate the device.
Now that you understand the three types of files, you shouldn't feel baffled by subsequent use of
the word in the book. The term "file" will often be used in this book to refer to any of these types,
though it will mostly be used to mean an ordinary file. The real meaning of the term should be
cvident from its context.
The File System
67
4.2 WHATS IN A (FILE)NAME?
On most UNIX systcms today, a filename can consist of up to 255 characters, though this figureis
normally not reached. Files may or may not have extensions, and can consist of practically any
ASCIl character except the / and the NULL character (ASCII value 0). You arc permitted to use
control characters or other unprintable characters in a filename. The following are valid filenames
in UNIA:
a.b.c.d.e
.last_time list. VBD-++bcd -
@#$%*abcd
The third filename contains three control characters (/Ctrl-v being the first). These characters
should detinitely be avoided in framing filenames. Morcover, since the UNIX system has a special
treatment tor characters like , *, ?,*, &among others, it is recommended that only the following
characters be used in filenames:
Alphabetic characters and numerals
. The period (.), hyphen (-) and underscore ().
doesn't need to have the
UNIX imposes no rules tor framing filename extensions. A shell script
it's the application that imposes
.sh extension, even though it helps in identification. In all cases,
filenames to end with .c, Oracle requires
the restriction. Thus the C compiler expects C program
users must also keep these
to have the .sql extension, and so forth. DOS/Windows
SQL scripts
two points in mind:
a.b.c.d.e is perfectly valid filename. A
A file can have as many dots embedded in its name;
a
filename can also begin with a dot or end with one.
and it's
.UNIX is sensitive to case; chap01, Chap01 and CHAPO1
are three different filenames,
possible for them to coexist in the same directory.
filename. You'll have a tough time getting rid of
it!
Caution: Never use a - at the beginning of a
treats it as an option and reports errors.
For
A command that uses a filename as argument often
invalid option.
instance, if you hav a file named -z, cat -z won't display the file but interpret it as an
4.3 THE PARENT-CHILD RELATIONSHIP
file system in UNIX is a collection of all of
All files in UNIX are "related" to one another. The inverted
device files) organized in a hierarchical (an
these related files (ordinary, directory and
been adopted by DOS and Windows, and visually represented
is
tree) structure. This system has also
in Fig. 4.1.
a top, which serves as the
reference
feature of every UNIX file system is that there is
The implicit root is actually a
root and is represented by a/ (frontslash).
point for all files. This top is called used by the system administrator to log
different from the user-id root
directory. It is conceptually to represent the root directory.
In this we'll be both the name "root and the symbol /
using
In. text,
68 UNIX: Concepts and Applications
root (/
bin dev etc home 1ib sbin stand tmp usr
Var
cat date who dsk rdsk
kumar sharma unix bin include sbin
fOq18dt 1ogin.sql progs safe
Fig. 4.1 The UNIX File
System Tree
The root
directory (/) has a number of
subdirectories under it. These
more subdirectories and other files under them.
subdirectories in turn have
For instance, bin and usr are two
under/, while a second bin and kumar are subdirectories directories directy
under usr.
Every file, apart from root, must have a parent, and it should be
parentage of a file to root. Thus, the home directory is the possible to trace the ultimate
parent of kumar, while / is the parent ot
home, and the grandparent of kumar. If you create a file
kumar will be the parent of this file. login.sql under the kumar directory, then
Ir's also
obvious that, in these parent-child
and kumar are both directories as they are both
relationships, the parent is always a
directory. home
parents of at least one file or directory. 1ogin.sqi is
simply an ordinary file; it can't have any directory under it.
4.4 THE HOME VARIABLE: THE HOME DIRECTORY
When you log on to thc system, UNIX automatically places you in a directory called the
home directory. It is crecated by the system when a user account is opened. If you log in using the
login name kumar, you'l| land up in a directory that could have the pathname /home/kumar (or
something clsc), You can change your home directory when you like, but you can also effect a
quick return to it, as you'll see soon.
69
The File System
The shell variable HOME knows your home directory:
$ echo $HOME
/home/kumar Frst/ represents the root d1rectory
What you see above 18 an absolute pathname, which is simply a sequcnce of dircctory names
separated by slashes. An absolute pathname shows a file's location with rcference to the top, i.c.,
root. These slashes act as delimiters to file and directory names, cxcept that the first slash is a synonym
for root. The directory kumar is placed two levels below root.
lt's often convenicnt to refer toa file foo located in your home directory as SHOME/foo. Further
most shells (except Bournc) also the symbol for this purpose. So, SHOME/foo is the samec as
use
/foo in these shells. The ~ symbol is a little tricky to use because it can refer to any user's home
directory and not just your own. Ifuser sharma has the file foo in his home directory, then kumar
can access it as ~sharma/foo. The principle is this:
A rildefolloued by / (like -/foo) refers to one's own home directory, but when follouwed by a strng
(-sharma) refers to the home directory of that user represented by the string.
Note: The home directory is determined by the system administrator at the of
time opening a
user
account. Its pathname is stored in the file /etc/passwd. On many UNIX systems, home directories are
maintained under /home, but your home directory could be different (say, in /export/home). Even if you
have moved away from your "home", you can use the cd command to effecta quick return to it, as you'll
see soon.
4.5 pwd: CHECKING YOUR CURRENT DIRECTORY
UNIX encourages you to believe that, like a file, a user is placed in a specific directory of the file
from one directory to another, but at any point of time,
system on logging in. You can move around
you are located in only one directory. This directory is known as your current directory.
At any time, you should be able to know what your current directory is. The pwd (print working
directory) command tells you that:
$ pwd
/home/kumar
Like HOME, pwd displays the absolute pathname. As you navigate the file system with the cd command.
you'll be using pwd to know your current directory.
Note: It's customary to refer to a file foo located in the home directory as $HOME/foo. Depending on the
shell you use, it may be possible to even access foo as -/foo. One form is shell-dependent but the
both conventions followed in this text.
are
other
-

isnt, but naming


4.6 cd: CHANGING THE CURRENT DIRECTORY
You can move around in the file system by us1ng the cd (change directory) command. When used with
an argument, it changes the current directory to the directory specitied as argument, for instance, progs:
UNIX: Concepts and Applications

pwd
/home/kumar
progs must be n current dircctory
$cd progs
$pwd
home/kumar/progs
Though pwd displays the absolute pathname, cd doesn't need to use onc. The command cd n.
here
means this: your subdircctory to progs under the current dircctory."
"Change progs Using apathoname
causcs no harm cithcr; use cd /home/kumar/progs for the same cffcct.

When vou need switch to the /bin directory where


to most of the commonly used UNIX commar
1and
are
kept. you should use the absolute
pathname:
S pwd
home/kumar/progs
Scd /bin Absolute pathname required here because
S pwd bin isn't in current
/bin directory
We can also
navigate to /bin (or any directory) using a ditterent type of pathname; we are
to that shortly. comine
cd can also be used without
any arguments:
S pwd
home/kumar/progs
S cd
cd used without
pwd arguments
reverts to the home
/home/kumar directory
Attention, DOS users! This command invoked
without an argument doesn't indicate
directory. It simply switches to the home the current
directory, i.c.,
logged into. Therefore, if you wander around in the file the directory where the user originally
to
your home directory by system, you can force an immediate return
simply cd: using
$ cd /home/sharma
S pwd
/home/sharma
cd
pwd Returns to home
/home/kumar directory
The cd command
sometimes fail if you don't have
can
This doesn't
The
normally happen unless you deliberately proper permissions to access the dircctory
techniquc of doing that is describcd in Section 6.5.
tampcr with the permissions of the
directory
Note: Unlike in DOS,
when cd is invoked
doesn't show you the without arguments, it simply
current directory! reverts to its home
directOy
The File System

4.7 mkdir: MAKING DIRECTORIES


Directories are created with the mkdir (make dircctory) command. The command is followed by
names ofthe directories to be created. A directory patch is created under the current directory like

this:
mkdir patch

You can create a number ot subdirectories with one mkdir command

mkdir patch dbs doc Three directories created

with
So far, simple enough, but the UNIX system goes further and lets you create directory trees
iust one invocation command. For instance, the following command creates a directory tree:
of the
mkdir pis pis/progs pis/data Creates the directory tree

This creates three subdirectories-pis and two subdirectories under pis. The order ofspecitying
the arguments is important; you obviously can't create a subdirectory before creation of its parent
directory. For 1nstance, 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:

mkdir test
mkdir: Failed to make directory "test"; Permission 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
if to create a directory in /bin, /etc
by the user. You'll most certainly get this message you try
or other
any that houses the UNIX system's files.
directory
file attributes.
Well take up file and directory permissions in Chapter 6 featuring

4.8 rmdir: REMOVING DIRECTORIES


he rmdir (remove directory) command removes d1rectories. You simply have to do this to remove
the directory pis:
Directory muust be empty
rmdir pis
72 UNIX: Concepts and Applications
Like mkdir, rmdir also delete than dircctory in one Ror
shot. For instance
can more one

directories and subdirectorics that were just crcatcd with mkdir can be removed by ui
instance, the
yed by using three
with a reversed set of arguments: rmdi,
rmdir pis/data pis/progs pis

Note that when you delete a directory and its subdirectorics, a reverse logic has to be atal.
tollowing dircctory scqucnce used by mkdir is invalid in rmdir:
S rmdir pis pis/progs pis/data
rmdir: directory "pis": Directory not empty
Have vou observed one thing from the error
message? rmdir has silently deleted
the lowecs I
lecv
subdirectories progs and data. This crror mcssage lcads to two
rules
remember when dcleting directories:
important that you sheu

You can't delete a directory with


.
rmd irunless it is
empty. In this case, the
pis
dircctory couldr
be removed because of the existence of the
subdirectorics, progs and data, under it.
.You can't remove a subdirectory unless you
above the
are
placed in a
directory which is hicrarchical
one you have chosen to remove.

The first rule follows


logically from the example above, but the highlight on rmdir has
that will be explained later. (A significanc:
directory can also be removed without using rmdir.) To illustrate
second cardinal rule, try
removing the progs directory by executing the command from the the
directory itself: same
$ cd progs
pwd
/home/kumar/pis/progs
$ rmdir /home/kumar/pis/progs
rmdir: directory Trying to remove the current directory
"/home/kumar/pis/progs": Directory does not exist
To this
remove
directory, you must position yourself in the directory above progs, i.e.. pis,
then it from there:
remove and
S cd /home/kumar/pis
$ pwd
/home/kumar/pis
$ rmdir progs
The mkdir and rmdir commands work only in directories owned the
owner of her home directory, and by user. Generally, a user is
t
she
and remove subdirectories
can create
in this (as well as regular tilcs
directory or in any subdirectories created by her. However, she
create or remove files and directories
in other users' directories. The
normally won't be abte
discussed in Section 6.3. concept of ownership wIll

Note: A subdirectory can't be removed with


rmdir unless it's empty, and
directory or above it. But we can remove a one ispositioned in its parent
directory without using rmdir also (discussed later).
The File System 3
HOW IT WORKS: How Files and Directories are Created and Removed

Asmentioned in Section 4.1.2, a file (ordinary or directory) is associated with a narne and a number
called the inode number. When a directory is created, an entry comprising these two parametersis
made in the file s parent directory. The entry is removed when the directory is removed. The same holds
good for ordinary files also. Figure 4.2 highlights the effect of mkdir and rmdir when creating and
removing the subdirectory progs in /home/kumar

Filename lInode Filenamel Inode Filename Inode


Number Number Number
386444 mkdir progs 386444 rmdir progs 386444
417585 417585 417585
to0 499770 foo 499770 fo0 499770
progs 162112

Fig. 4.2 Directory Entry after mkdir and rmdir

Later in this chapter, we'll discuss the significance of the entries, . and that you'll find in every
this directory for
directory. In this chapter and Chapters 5 and 11, well be progressively monitoring
changes that are caused by some of the file-handling commands.

4.9 ABSOLUTE PATHNAMES

Many UNIX commands use file and directory names as arguments, which are presumed to exist

in the current directory. For instance, the command

cat 1ogin.sql

exists in your current directory. owever, if vou are placed in


will work only if the file login.sql
can't obviously use the above command,
/usr and want to access login.sql in /home/kumar, you
but rather the pathname of the file:

cat /home/kumar/10gin.sql
file's location must be determined with
As stated before, ifthe first character ofa pathname is /, the
above, is called an absolute pathname.
respect to root (the first /). Such a pathname, theone
as
have to descend one level
When you have more than one / in a pathname, for each such/, you
the file system. Thus, kumar is one level below home, and two levels below root.

demarcate the varous levels, you have a mechanism


When you specify a file by using frontslashes to identucal absolute pathnames.
a UNIX systenm can have
O 1denufying a file uniqucly. No two files in but in different directories; their pathnames will also
can have two files with the s a m e name,
1Ou
be differcnt. Thus, the filc /home/kumar/progs/c2f.pl can cocxist with the file

/home/kumar/safe/c2f.pl.
| 74 UNIX: Concepts and Applications
4.9.1 Using the Absolute Pathname for a Command
More than not, UNIX command by cxecuting its disk file. When you specify the das.
oftcn a

command, the system has to locate the


runs

file date from a list of directories specified in the pdate


variable, and then execute it. However, if you know the location of a particular command, Yo PATH
precede its name with the complete path. Since date resides in /bin (or /usr/bin), you can alsoCan
the absolute pathname: Use

$ /bin/date
Thu Sep 1 09:30:49 IST 2005

Nobody runs the date command like that. For any command that resides in the directories specifie
in the PATH variable, you don't need to use the absolute
pathname. This PATH, you'll recall (2.4.j
invariably has the directories /bin and /usr/bin in its list.
If you execute programs residing in some other
directory that isn't in PATH, the absolute pathname
then needs to be specified. For
example, to execute the program less resid1ng in /usr/local/bin
you need to enter the absolute pathname:

/usr/local/bin/less
If you are
frequently accessing programs in a certain directory, it's better to include the directory
itself in PATH. The technique of doing that is shown in Section 10.3.

4.10 RELATIVE PATHNAMES


You would have noted that in a
previous example (4.8), we didn't use an absolute pathname to
move to the directory progs. Nor did we use one as an
argument to cat (4.9):
cd progs
cat login.sql

Herc, both progs and login.sql are


presumed
exist in the current directory. Now, if progs also
to
contains a
directory scri pts under it, you still won't need an absolute
that directory: pathname to change t0
cd progs/scripts
progs is 1n current directory
Here we have a
pathname that has a/, but it is not an absolute pathname because it doesn't begi
with a/. In these three
examples, we used a rudimentary form
of relative
they are
generally not labeled as such. Relative pathnames, in the sense pathnames thoug
discussed next. they are known, a
4.10.1 Using . and.. in Relative Pathnames
Ina
preceding example (4.8), you changed your
directory (/home/kumar/pis) by using cd with directory
from
/home/kumar/pis/progs
absolute pathname: an
to its pa
rent

cd /home/kumar/pis
The File System
Navigation often Decomes easier by
fers shortcut-the relative
offer a using a
common ancestor
pathname-that (herc, /home) as reference. UNIA
eference, and specities
rete the
path relative uses cither the current or
it.
to A relative
pathname uses one of these cryptic parent dirCctory as
.(asingle dot)This
(two dots)-I'his representsthethe current directory.
symbols:
represents parcnt
We'll now use the to
directory.
.
frame relativc
/home/kumar/progs/data/text, you pathnames. Assuming that you
/home/kumar/progs/data:
can use .. as an
argument to cd to move
are
placed in
to the parcnt directory,
pwd
/home/kumar/progs/data/text
Scd
$ pwd Moves onc level up
/home/kumar/progs/data
This method is compact and more useful when
translates this: "Change
to ascending the hierarchy. The command cd
your
any number of such sets of
directory to the parent of the current directory." You can combine
separated by
different meaning; instead of moving down a
.

/s. However, when / is used with a


it acquires
it
acquires aa ..

level, it moves one level


up. For instance, to move to
/home, you can always use cd /home.
Alternatively, you can also use relative pathname: a
$ pwd
/home/kumar/pis
$cd../.. Moves
$ pwd two levels up
/home
Now lets 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 A filename can begin with a dot

his copies the file .profile to the current directory (.). Note that you didn't have to specify the
lename 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
number
root. However, whether you should use one
depends solely the
on
Compared toa relative pathname. In every case here, the relative pathname required fewer keydepressions.
ofkeystrokes required when
on where you are currently placed, an
absolute pathname can be faster to type.
Epending

4.11 1s: LISTING DIRECTORY CONTENTS


obtain list ofall filenames in the current directory.
already used the 1s command (1.4.9) to a
u have
Let's execute it
again:
Application5
and
UNIX: Concepts
76
Numerals first
Is Uppercase next
08packets.html Then lowercase
TOC.sh
calendar

cptodos.sh
dept.Ist
emp. Ist
helpdir
progs
usdsk06x
usdsk07x

directory arranged in ASo


usdsk08x
iienamcs n the current
ist Or
is a complete
What vou sce here then lowercase), with one filename i. cach
tirst, uppercase and
(numbers
collating sequence would probably see the directo
includes directories also, and if you are using Linux, you ories
line. I
colors.
in different
and ordinary files
doesnt show these colors, make sure that you create this alias ater
LINUX: If your Linux system
logging in:
alias 1s='ls --color=tty'

not supported by the Bourne shell.


Aliases are discussed in Section 10.4, but note that they are

Directories often contain many files, and you may simply


be interested in only knowing whethera
1s with the filename:
particular file is available. In that case, just use
$1scalendar
calendar

and if perl isn't available, the system clearly says so:

Slsperl1
perl: No such file or directory
ofthe file attributes. In
ls can also be used with multiple filenames, and has options that list most

the following sections, you'll see some of these options.

4.11.1 1s Options
1s has a large number of options (lable 4.1), but in this chapter, we'll present a handful of them
The other options will be taken up in later chapters. The section numbers are appropriately indicatcu
in the table.
When you have several files, it's better to display the tilenames
Output in Multiple Columns (-x)
in muliple columns. Modern versions of ls do that by default (i.e., when used without options
but if that doesn't happen on your system, use the -x option to produce a multicolumnar outpu
$1s -X
The File System 77
0 8p a c k e t s . h t m l TOC.sh calendar cptodos. sh

dept.ist
emp.1st helpdir progs
usdsk06x
usdsk07x usdsk08x ux2nd06
can later customizC
necas to use the -X option to display multicolumnar output, you
Tfvoursystem
rhe command to display in this format by default (10.4).
and Executables of 1s that you have secn so far mercly
ldentifying Directores (-F) output The
w e r e dircctory files. To identify
showed the filenames. You didn't know how many of them,if any,
should be used. Combining this option with -X produces
directories and executable files, the -Foption
multicolumnar output as well:
a

$1s -Fx
calendar* cptodos. sh*
08_packets.html TOC.sh*
emp.1st helpdir/ progs/
dept.1st ux2nd06
usdsk06x usdsk07x usdsk08x
indicates that * the file contains
symbols, and /, as type indicators. The
*
of two
Note the use
the two subdirectories in the
code and the / refers to a directory. You can n o w identify
executable
and progs.
current directory-helpdir
in a directory. There are
Hidden Files Also ls doesn't normally show all files
(-a)
Showing home directory, that
hidden files (filenames beginning with a dot), often found in the
certain lists all hidden files as well:
don t show in the listing. The -a option (all)
up
normally
$ 1s -axF .kshrc
.exrc
.xdtsupCheck
profile . rhosts .sh history calendar*
xinitrc 08_packets.html* TOC.sh*

in. It is logs
contains a set of instructions that are performed when a user

The file .profile The other file,


to AUTOEXEC.BAT of DOS,
and you'll know m o r e about it later.
conceptually similar these hidden filenames,
instructions for the vi editor. To display
.exrc, contains a sequence of startup
o r specify the
filenames in the command line.
can either use the -a option
you
s a m e symbols in relative
directories. Recall that we used the
The first two files (. and ..) are special have the s a m e
directories (4.10.1). These symbols
pathnames to represent the current and parent created
create a subdirectory, these "invisible" directories are

meaning here. Whenever you them, n o r can you write into


them. They help in
You can't remove
automatically by the kernel.
holding the file system together.

displayed only when 1s is used with the -a option. The


dot are
Note: All filenames beginning with a
directory.
directory. represents the current directory and.. signifies the parent
tilenames to 1s to
last example, you specitied some ordinary
Listing Directory Contents In the situation will be quite ditferent if you specify
the two directory
have a selective listing. However, the
names, hel pdir and progs, instead
78 UNIX: Concepts and
Applications

$ Is -x helpdir progs
helpdir: graphics.obd
reports.obd

forms.obd

progs: name.ppl
cent2fah.pl n2words.pl
array.pl
directorics are listed, consisting the Oracle documernt
of
This timc the contents of the
number of perl program c s
n progs. Note that 1s, when uc
helpdir directory and a wwth
directory names as arguments,
doesnt simpiy Show thCir names as it does with ordinar
es.
Recursive Listing (-R) The -R (recursive) option lists all files and subdirectories in a directe.
tree. Similar to the DIR /S command of D06, this traversal of the directory tree is done recursi

until there are no subdirectories left: rSTel


$ ls -xR
08_packets.html1 TOC.sh calendar cptodos.sh
dept.Ist emp.1st helpdir progs
usdsk06x usdsk07x usdsk08x ux2nd06

/helpdir:
forms.hlp graphics.hlp reports.hlp
/progs
array.pl cent2fah.pln2words.pl name.p
The list shows the filenames in three
sections-the ones under the home
under the subdirectories helpdir and directory and those
progs. Note the subdirectory naming conventions
./helpdir indicates that hel pdir is a subdirectory under followed
/home/kumar happens to be the current directory, the absolute . (the current
directory). Since
/home/kumar/hel pdi r. pathname this file
of tu expands
Table 4.1 Options to 1s
Option Description
-X
Multicolumnar output
Marks executables with *, directories with
Shows all filenames / and symbolic links with
-R Recursive list
beginning with a dot including . and ..

Sorts filenames in reverse order


(ASCII collating sequence by default)
-1 Long listing in ASCII
-d dirname Lists only dirname if collating sequence showing seven attributes of a file
dirname is a directory (6.
-t Sorts filenames
by last modification time (6.2)
-1t Sorts listing by last (11.6)
modification time
by last access time (1 (11.6)
-u Sorts filenames
-Iu Sorts by ASCII 1.6)
-lut collating sequence but
sorted by last access time listing shows last access time (11.6)
As above but
Displays inode number (11.1) (1i.6)
The File System

4.12 THE UNIX FILE SYSTEM


Tet's conclde
We have learned to use the basic command set for handling files and dircctories.
thischapter by taking a cursory look at the structure of the UNIX file system. This
structure

SVRA release.
has beenchang1ng constantly over the ycars until AT&T proposed one in its
structure has been adopted
Though vendorimplementatuionsvary in detail,broadly the SVR4
by most vendors.

+.I which showsa standard UNIX file system. In real


heavily trimmed structure of
Refer to Fig. a

but for our initial


lifc. the root directory has many more subdirectories under it than shown,
we'll stick to the oncs presented below. It helps, from the administrative point o
comprehension,
The first group contains
view at least, to view system as comprising two groups of files.
the entire tile
made available during system installation:
the files that are

commands
directories where all the commonly used UNIX
/bin and /usr/bin-These arethe shows these
Note that the PATH variable always
(binaries hence the name bin) are found.
directories in its list.
can't execute but the system admin1strator
./sbin and /usr/sbin-If there's a command that you
be in one ofthese directories. You won't be able
to execute most
can, then it would probably administrator's PATH shows
(some, you can) commands
in these directories. Only the system
these directories.
files of the system. You can change a very
./etc-This directory contains the configuration Your login name
a text file in this directory.
important aspect of system functioning by editing
and /etc/shadow.
and password are stored in files /etc/passwd
on disk. There
device files. These files don't occupy space
/dev-This directory contains all
dsk and rdsk in this directory.
could be more subdirectories like pts,
files in binary form. You'll need to link your C programs
./1ib and /usr/1ib--Contain all library
with files in these directories.
used by C programs. The statement

/usr/include-Contains the
standard header files
in this directorv.
#include <stdio.h> used in most
C programs reters to the file stdio.h
subdirectories
are stored. There are separate
/usr/share/man--This is where the man pages
For instance, the man page
contain the pages for each section.
here (like man1, man2, etc.) that
the 1 in manl represents Section l of the
of Is can /usr/share/man/manl, where
be found in
have different names on your system (like smanl.
UNIX manual. These subdirectories may
Sman2, etc. in Solaris).
directories would change as more sottware and uilities are adde
Over time, the contents of these send and receive mail and
with their own files; they write programs,
to the system. Users also work
shown below:
also create temporary files. These
files are available in the second group
These tiles wiped
to create temporary files.
are
allowed
/tmpThe directories where users are

away regularly by the system.


Contains all your print jobs and your outgong and
var-The variable part of the file system.
incoming mail.
80 UNIX: Concepts and Applications

houscd hcrc, kumar would have his home di


/home On many systems users are
system may use a differcnt lOcation for homc director irectory
/home/kumar. 11owever, vour

On a busy sVstem, it's in directories belonging to the second group that you could experiene.
learn latcr to house some of thesc directory strs p
depletion of available disk space. You'll
separate file svstems so that depletion of space in ne tile system doesn't affect other
tures
file sucs
o
File svstem internals and administration are taken up toward the end of this text stem
4.13 CONCLUSION

Though UNIX is known to makc lhttle distinction between the various types of files, that war.
really cstablished in this chapter. lou uscd exclusive commands to handle directories (likepd .
Nasn
mkdir and rndir). These commands have no relevance when applied to ordinary or device files
appears that UNIX does care to some extent about the type of filc it handles. In the next cha
we look at ordinary files using yet another set of commands meant for them. pte

WRAP UP

Files have been assumed to be of three types. An ordinary file contains what you put into i
directory maintains the tilename and its associated inode number. A device file contains no dar
e kernel uses the attributes of the file to operate the device.
Executable files don't need any specitic extensions. A file doesn't contain its atributes nor coes
it contain the end-of-file mark.

The file system is a hierarchical structure, and the top-most directory is calied root. Files an:
directories have a parent-child relationship.
pwd tells you the current directory, and cd is used to change it. When used by itself, it switches
the home directory. The home directory is available in the shell variable HOME. A file foo in th
home directory is ofen referred to as $HOME/foo or -/foo.
mkdir and rmdir are used to create or remove directories. To remove a direciory bar with rmdir
bar must be empty and you must be positioned above bar.
A pothname is a sequence of directory and filenames separated by slashes. An absolute pathname
and denotes the file's location with respect to rool.
begins witha/ Arelative
pathname uses
symbols oand.. fo representthe file's location relative to the current and parent directen
th:

respectively.
By detault, 1s displays a list of filenames in ASCIl collating sequence, which accords pioriy t
this sequence-numbers, uppercase, lowercase. It can also
display hidden filenames beginning
with a dof (-a) anda recursive list (-R). However, when used with a
directory name as argume
1s displays the tilenames in the directory.

Test Your Understanding


4.1 How long can a UNIX filename be? Which characters can't be used in a tilename

You might also like