^ECTION 3.
4 DIRECTORIES 71
is the user login
directory. The name of the home directory
has home
a
Each user colorcd subdirectory named adams,
id. For cxample,
in Figurc 3.3, the
id or the user
dircctorics.
and zeke arc all home
joan, tran,
Working Directory
in at any point in a session. When
directory
cuent,
is the o e that we are
The working. If we have subdirectories, we will
or
directory is our bome difeclory.
we stan. the working subdirectories as needed during
to one or morc
most likcly move
from our honne directory
our working directory changes automatically.
we ehange directory,
a session)When
Parent Directory are in
above the working directory. When we
our
The parent directory immediately
is
is one of the system directories)
In our environment, the
home directory, its parent
When we move from our
of our home directory
is a directory known as staff.
parent becomes the parent directory.
our home directory
home directory to a subdirectory,
As we saw in Chapter 1,
and file in the system must have a name)
Every directory their filenames. If you examine Figure 3.3
when we refer to files in a command, we use
files that have the same names as
carefully. however. you will note that there are some
more than just
should be obvious, therefore, that we need
files in other directories. It and the file-
While you might think that the directory
the filename to identify them. examination of Figure 3.3
name would be enough
to uniquely identify a file, a further
shows that directory names can also be duplicated.
we need to specify the
file's path from the root
To uniquely identify a file, therefore, absolute pathname, a list of
the file. The file's path is specificd by its
dirèctory to
The absolute pathname for a file or a direc-
all directories separated by a slash character (). cannot eas-
If know only the person's name. you
tory is like an address of a person) you
if know a person's name, street address,
ily find that person On the other hand, you the world. This full or absolute
locate anyone in
city, state, and country, then you
can
that reason, UNIX also provides a shorter pathname
pathname can get quite long. For relative pathname.
pathname is known as a
under certain circumstances; this shorter
Absolute Pathnames
root to the
As we have statcd, an pathname specifies the full path from the
absolute
for the gray shaded files in
desired directory or file, Table 3.4 lists the lull pathname
also have absolute pathnames, we
cach directory in Figure 3.3. Because directories
also show the path to each of their directories.
TABLE 4 Absolute Pathnames for Some Files in Figure 3.3
File Absolute Pathname Directory Absolute Pathname
File
filel /etc/ fi lel /etc
file2 /usr/staff/adams/ file2 /usr/staff/adams
filel /usr /staff/joan/file1 /usr/staff/joan
Continued
72 CHAPTER 3 FILE SYSTEMSs
3.3-Continued
for Some Files
in Figure
Absolute Pathnames
ABIE 1 Directory Absolute Pathname
File Absolute Pathname
File /usr/staff/tran
/usr/staff/tran/filel
filel /usr/staff/zeke
/usr/staff/zeke/filel
filel
the time regardless of where we are in th
(Absolute pathnames can be used all
is that they change as directories and
directory hierarchy. Their main disadvantage
their subdirectories are moved around a system by the system administrator. For thar
reason, we would like to be able to locate files by finding their locations relatíve to our
current location.
ery pathame that siarts with a siash is an 2 soltafe paliinanme.
Relative Pathname
Wherever we are working in UNIX, we are always in a directory. When we start, we
are in our home directory, which is different from the root directory. Our current, or
working, directory is the directory we are in. We can move from one directory to
another anytime we need by changing our working directory.
If we need to reference a file in our
working directory, we
can use a relative rather
than the absolute
pathname.(A relative pathname is the path from the working direc
tory to the file. Therefore, when we refer to a file in the
use the filename with no
working directory, we simply
path. This works because when a relative pathname is used.
UNIX starts the search from the working
directory.
Similarly, from our working directory, we can fefer to a file in a
using a relative path from the working directory to the file. For subdirectory by
Figure 3.3 on page 70, if we are in the staff directory, we can example, referring to
refer to file8 in the
tran dircctory as shown below. In fact, as we
shall see shortly, we can refer to a
directory or file anywhere in the system by using a relative
pathname.
tran/file8
Reiative Pathname Abbreviations
The hone directory, working directory, and parent
that make it easy to refer to them, even when
directory all have abbreviations
we don't know their names. \
/Home Directory ())
The abbreviation of
user's home directory is the tilde
a
(-). When we use the tilde, the
shell uses the home directory pathname set for us
refer to our own home
by the system. When
we need to
directory, we can just use tilde. the For example,
file named
project.list in our home refer to a
to
in the next example.
directory.jwe would use the
pathname shown
DIRECTORIES
73
SECTION 3.4
-/project.1ist
however, that
This format works from any point in the dircctory hicrarchy. Note,
filc is requircd.
the slash separating the dircctory and thc and if we are permitted to
we know that thcre is a filc in another user's dircctory,
(lf cxample, from within
look at it. we can also refer to it by using his or her user id.)For next
as shown in the
gilberg. we can refcr to a file in the forouzan dirctory
example.
-forouzan/file1
Notice thc difference between these two file references. For our own home direc-
the tilde refers to our home directory.
tory, we just use the tilde. Without a user id,
or her user id.
To refer to another user's home directory, however, we must use his
In this case, a slash does not follow the tilde. In other words, a user's home direc
followed immediately by a user id. To refer
tory is the home directory abbreviation
to a specific file, the home directory is followed by a slash and the filename just as
in the first example.
Working Directory ())
The abbreviation for the working directory is a dot (.). While it may seem strange
that we need an abbreviation for the current directory, some UNIX commands (such
as find, page 108) require that the pathname for a start directory be specified even
when it is the current directory, In these cases, the dot abbreviation makes con-
in Figure 3.3 (page 70), we see that
structing the relative path easy. For example,
file6 is stored in tran's home directory. It can be referred to in four ways as
shown in the next example.
£ile6
/file6
-/Eile6
tran/file6
All of these pathnames are relative to the tran's home directory, which in this
case is also the current directory. We just used four different techniques to construct
the relative path.
Parent Directory (
parent of any directory is the direetory immediately above it in the directory
(The
path from the root. This means that every directory, except the root directory, has
a parent.In Figure 3.4, the parent of reports is adams. Likewise, the parent or
adams is the s t a f f directory, and the parent of'the s t a f f directory is the u s r
directory
74CHAPTER 3FILE SYSTEMS
usr dev etc
staff
Relative Pathnames for file3
1../usr/staff/joan/file3
2 usr/staff/joan /file3
adams Joan 3 staff/joan/fi le3
4 joan/file3
5 file3
b/joan/file3
reports file3 7../../joan/file3
f E Relative Pathnames for file3
The abbreviation for the
parent directory is two dots (. .). If reports is the cur-
rent directory, we can refer to its parent (adams)
using two dots (. .). Furthermore
we can refer to the staff directory as the parent of the parent (../..).
Creating Relative Paths
Given the directory structure shown in Figure 3.4, let's examine some possible rela-
tive pathnames for file3. As a point of reference, the absolute pathname for file3 is
/usr/staff/joan/Eile3
From /etc.
If our working
directory is letc, the shortest pathname is actually the absolute
pathname. We can use a relative pathname (. ./usr/staff/joan/ file3). but
it is not recommended because
it is longer than the absolute pathname. Because
UNIX will follow whatever pathname we
the shortest possible pathname.
provide, for efficiency we should use
From the root.
If working direclory is the root, then the relative
our
joan/file3. Note,Ihat the only diference betweenpathname
is usr/staff/
the absolute and relative
pathnames is the lirst slash representing the root itself.
From /usr.
Ifour working directory is/usr, Ihen the relative
file3. pathname is staff/joan/
From /usr/staff.
If our working directory is
file3. A word of caution is in order
/usr
/staff, then the
relative pathname is joan/
here. A commoi
path with a slash. In this case, thal would make it mistake is to start a relative
however, that whenever a pathname starts with a !joan/file3. Remember,
slash, it is an absolute
In this case, UNIX would begin its search for
the file pathname.
at the root, and because
75
IMPLEMENTATION
SYSTEM
SECTION 3.5 FILE
raise an
would
il
under the root,
named joan immediatcly a slas.
there is no directory never start with
addresses c a n
Relative
ile not lound.
cTor
is
pathname
then the relative
From /usr/staff/joan.
working directory
is
/usr/staff/joan,
relative pathname is just
I our is in the working dircctory, the
When the file
file3.
just
the lilename.
From /usr/staff/adams.
we a r e
out of the path
/usr/staff/adams,
then
is s o m e w h e r e on
the
working dircctory
If thercfore find a starting point
our
the file. We must would then be
from the root to
.
the relative path
this casc,
root to the file.° In configuration
path fronn the This a s s u m c s that we know the cxact directory
home directory,
joan/file3.
A safer pathh would
be to use joan's
above our home directory.
~joan/file3.
From /usr/staff/adams/reports.
abbreviation as
the home directory
Once again, there are two paths. The first uses
the first takes us
second uses two parent abbreviations;
shown in example 6. The This choice gives us . . /. ./
to adams and the second to the staff directory.
Again, the safer and
shorter path is ~joan/ file3.
joan/file3.
are summarized in Table 3.5.
These seven examples
Relative Pathname Examples
TABLE 3.5
Relative Path to file3
Working Directory faster.
Use absolute pathname; it's
/etc
usr/staff/joan/file3
staff/joan/file3
/usr
joan/file3
/usr/staff
/usr/staff/joan
file3
/joan/file3 or -joan/file3
6. /usr/staff/adams
. / . . / j o a n / f i l e 3 or -joan/file3
/usr/staff/adams/reports
5FiieSystem implemmentatiopn
sections
disk is formatted, space is divided into several sections. Sone
con-
When a
tain structural information about the disk itself. The last section contains the physical
files.In this section, we examine the UNIX (ile system to better understand how these
sections are related.
Disk storage can be conceived of as a continuous linear storage structure, starting
with track 0 on the first track surface and moving down through track 0 of al surfaces
before continuing with track I on the first surface.
We could use the absolute pathname, but that is generally inefficient, and often we don't know all of the
directories between the root and the directory containing the file.
76
CHAPTER 3 FILE SYSTEMS
Filc Systens
lnUNIX, a file system has four structural scctions known as blocks:
the
the bonti
super blorbloc
super block. the inode block, and the data block. The boot block,
inode blocks are fixed at the
tions on the disk even
beginning of the disk. They occupy the same
when the disk is
Figure 3.5. rcorganized) These blocks are shou n in
Boot Block
Inode Block
Super Block Data Blocks
FiGURE 3.5 ADisk File Format
Boot Block
When an
operating system is
started, a small program known as the boot program is
used to load the kernel into
memory.' The boot program, when present, is found at the
beginning of a disk in the boot block.
Super Block
The next block the disk, the super
on
block, contains
tem. Stored here are such items as the total size of information about the file sys-
the disk, how
empty, and the location of bad blocks on the disk. many blocks are
Inode Block
Following the super block is the inode (information node)
mation about each file in the data block. The file block, which contains infor-
as inodes. There is one
information is stored in records known
inode for cach file on the disk.
the file, most notably the owner of the They contain information abou
file, its file type,
shown in Figure 3.6, each inode contains the
address of its
permissions, and address.) As
corresponding file.
(Data Blocks
The data block contains several
types of files. First and foremost from the
of view, it contains all of the user liles; il is user's point
where data are stored. It the
special files that are related to user data: regular files, also contains
files, and FIFO files. Finally, it contains the characterdirectory files, symbolic link
socket system files. special, block special, and
Booting the system, also known as bootstrapping, Is lernm
a
manual system initialization process. While its meanung created
by early programmers to describe
their has been lost over
the years, the term
ues to be used to describe the
process. contin
IMPLEMENTATION 77
SECTION 3.5 FILE SYSTEM
Files
Inodes-
Owner Group Type Permis'n Time Address
FIGURE 1.6 Inodes
Directory Contents
Given the concept of inodes pointing to files, the directory becomes a very simple
structure. Remember that the directory is itself a file. Its contents are a set of inode-file
entries containing the filename and its corresponding inode A directory is seen in
Figure 3.7. In this figure, each file is paired with an inode, which as we haveseen contains
the address of and other information about the file. This pairing of flename and
inodes is the basis of a UNIX concept called links.
joan
file1 0156
file2 2146
files 1421
FIGURE 3.7 A Directory of Three Files
Links
A link is a logical relationship between an inode and a file that relates the name of a file
to its physical location. UNIX defines two types of links: hard links and symbolic links.
Hard Links
In a hard link structure, the inode in the directory links the filename directly to the phys-
ical file. While this may sound like an extra level of structure, it provides the basis
for multiple file linking) which we will see shortly. The hard link concept is shown in
Figure 3.8.
Symbolic Links
A symbolic (or soft) link is a structure in which the inode is related to the physical file
through a special file known as a symbolic linki (page 69). This structure is shown in
Figure 3.9.
78
CHAPTER 3 FILE SYSTEMS
joan
file 1 0156
file2 2146
file3 1421
inodes files
FIGURE 3.8 A Hard Link
joan
file1 0156-
file2 2146
file3
1421
inodes file
Symbolic
link
3.9
FIGURE A Symbolic Link
As you study Figure 3.9, if you come to the conclusion that a
as efficient as a hard link, you would be correct. symbolic link is not
Soft
cific situations: links to directories, which must be
links were designed for two spe
other file symbolic, and links to files on
systems.)Similarly, for technical reasons beyond the scope of this text, t s
notpossible to use a hard link to a file that is on another file
work well, however. Figure 3.10 shows the system. Symbolic lina
structural design of a
how it links a filename to a file in another file symbolic link anu
system.
file1 0156
joan
file2 2146
tile3 1421
File System A Symbolic
inodes unk
File SystemB inodes Physical
File
FIGURE3.10 Symbolic Links to Different File Systems
SECTION 3.s FILE SYSTEM IMPLEMENTATION 79
e b Multiple Links
uro a One of the advantages provided by the inode design is thc ability to link two or
anoait more different filenames to one physical file. The filenames can be in the same
directory or in different directories. This makcs the multilink structure a convenient
and efficicnt method of sharing filcs. Files are commonly shared among a team
working on a large system)Additionally, given a user with a large file system, it may
be convenient to share aAighly used file among several directories., In either case,
the important point to note is that although there are several different references tao the
file, it exists only once.
I n UNIN, a physical file has only one iode, but it can have many names.
When a file is created, an entry containing its name and inode link is stored in its
o directory. To share the file, the same entry can be created in another directory. As we
wil
see,
l the filename in the second directory entry can use the same ora different hle-
name. Both entries, however, link to the same inode, which in turn points to the phys-
ical file. In Figure 3.11, we show three different files all pointing to one inode.
Assuming that all permissions are properly set, both Juan and Tuan could update the
file. On the other hand, the permissions could be set so that only one could update and
the other could only read.
JUuan tuan
file141 file1 111
i or bor file2 2146
file3 14
inodes physical file
FIGURE 3,IMultipleLinks to One File
Hard links can be used only with files; soft links can be used with both files and
directories.
Current and Parent Directories
In all of our examples so far, we have excluded two entries: one that represents the
curreht directory (.) and one that represents the parent directory (. .). These entrnes
provide the inode entry for these two directories so that we can reference them when
2 imo uarnecessary. Session 3.2 contains an inode directory listing (formoreinformation on the
list command, see page 81). We used the options to print all entries (-a) and inode