Module -2
Unix File System
The File
The file is the container for storing information. File can be treated as sequence of containers. If
you name a file foo with three characters a, b and c into it, then foo will contain only the string
abc and nothing else. Unix file doesn’t contain the eof (end-of-file) mark. A file’s size is not
stored in the file, nor even its name. All file attributes are kept in a separate area of hard disk, not
directly accessible to humans, but only to the kernel. UNIX treats directories and devices as files
as well.A directory is a simple folder where user stores filenames and other directories. All
physical devices like hard disk, memory, CD-ROM, printer and modem are treated as files. Shell
and kernel is also file.
Unix has divided files into three categories
1. Ordinary Files
2. Directory file
3. Device file
1. Ordinary Files
It is also known as regular file. It contains only data as a stream of characters.
2. Directory file
It contains files and other sub-directories. It contains their names and a number associated with
each name.
3. Device File
All devices and peripherals are represented by files. To read or write a device, user has to
perform operations on its associated files.
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 can be divided into two types:
SDMIT, Ujire Page 1
i. Text File
ii. Binary File
i. Text File
A text file contains only printable characters and user can view the contents of the file. A text file
contains lines of characters where every line is terminated with newline character. It is also
known as linefeed(LF). When user press [Enter] while inserting text, the LF character is
appended to the every line.
ii. Binary File
A binary file contains 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.
2. Directory File
A directory contains no data, but keeps some details of the files and subdirectories that it
contains. The UNIX file system is organized with a number of directories and subdirectories.
User can create them as and when you need. Two or more files in separate directories can have
the same filename. A directory file contains an entry for every file and subdirectory that it
houses.
If there are 20 files in a directory, there will be 20 entries in the directory.
Each entry has two components:
1. The filename
2. A unique identification number for the file or directory (called the inode number)
A directory contains the filename and not the file’s contents.
3. Device File
SDMIT, Ujire Page 2
User will perform activities such as 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. 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 special. It is not a stream of characters. It doesn’t
contain anything at all. 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
What’s in a Filename ?
Filename consist of up to 255 characters. Files may or may not have extensions and can consist
of practically any ASCII character except the / and the NULL character (ASCII value 0). Users
are permitted to use control characters or other unprintable characters in a filename.
The following are valid filenames in UNIX.
1. .last_time
2. list.
3. ^V^B^D-++bcd
4. -{ } [ ]
5. @#$%*abcd
6. a.b.c.d.e
The third filename contains three control characters ([Ctrl-v] being the first). These characters
should definitely be avoided in framing filenames. It is recommended that only following
characters be used in filenames:
1. Alphabetic characters and numerals
2. The period (.), hyphen(-) and underscore(_)
• A file can have as many dots embedded in its name. i.e a.b.c.d.e is a perfectly valid
filename.
• A filename can also begin with a dot or end with one.
SDMIT, Ujire Page 3
• UNIX is sensitive to case. i.e chap01,Chap01 and CHAP01 are three different filenames
and it’s possible for them to coexist in the same directory.
The Parent-Child Relationship
All files in UNIX are related to one other. The file system in UNIX is a collection of all of these
related files(ordinary, directory and device files) organized in hierarchical (an inverted) structure.
In UNIX file system, there is a top, which serves as the reference point for all files. This top is
called root and is represented by / (frontslash). root is a directory.
Figure 2: The UNIX File System Tree
The root directory (/) has a number of subdirectories under it. These subdirectories in turn have
more subdirectories and other files under them. The home directory is the parent of mthomas. /
is the parent of home and the grandparent of mthomas. Every file, apart from root must have a
parent. It should be possible to trace the ultimate parentage of a file to root.
SDMIT, Ujire Page 4
In parent-child relationships, the parent is always a directory. home and mthomas are both
directories as they are both parents of at least one file or directory. .profile is a file. It can’t have
directory under it.
The HOME Variable: The HOME Directory
When user log on to the system, UNIX automatically places user in a directory called the home
directory. It is created by the system when a user account is opened. If user login using the login
name kumar, user will land up in a directory that could have the pathname /home/kumar. User
can change the home directory. The shell variable HOME knows home directory :
$echo HOME
/home/kumar First / represents the root directory.
Absolute pathname is simply a sequence of directory names separated by slashes. An absolute
pathname shows a file’s location with reference to the top, i.e root. These slashes act as
delimiters to file and directory names, except the first slash is synonym for the root. In above
example, the directory kumar is placed two levels below root.
pwd Checking Your Current Directory
When user log in to the system, he is placed in a specific directory of the file system. User can
move from one directory to another. But any point of time, user is located only in one directory.
This directory is known as user’s current directory. The command pwd(print working
directory) can be used for determining the current directory.
$ pwd
/home/kumar
pwd displays the absolute pathname. As user navigates the file system with cd command, user
can use pwd to know the current directory.
cd: CHANGING THE CURRENT DIRECTORY
SDMIT, Ujire Page 5
User can move around the UNIX file system using cd (change directory) command. When used
with the argument, it changes the current directory to the directory specified as argument, for
instance progs:
$ pwd
/home/kumar
$ cd progs
$ pwd
/home/kumar/progs
Here we are using the relative pathname of progs directory. The same can be done with the
absolute pathname also.
$cd /home/kumar/progs
$ pwd
/home/kumar/progs
$cd /bin
$ pwd
/bin
cd can also be used without arguments:
$ pwd
/home/kumar/progs
$cd
$ pwd
/home/kumar
cd without argument changes the working directory to home directory.
$cd /home/sharma
$ pwd
/home/sharma
$cd
/home/kumar
mkdir: Making Directories
SDMIT, Ujire Page 6
Directories are created with the mkdir (make directory) command. The command is followed by
the names of the directories to be created. A directory patch is created under the current
directory using following command:
mkdir patch
User can create a number of subdirectories with one mkdir command.
mkdir patch dbs doc
User can create directory trees with invocation of the command.
Following command creates a directory tree:
mkdir pis pis/progs pis/data Creates the directory tree
The above command creates three subdirectories – pis and two subdirectories under pis.
The order of specifying arguments is important. User can’t create a subdirectory before creation
of its parent directory.
For example , user 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
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:
1. The directory test may already exist.
2. There may be an ordinary file by that name in the current directory.
SDMIT, Ujire Page 7
3. The permissions set for the current directory don’t permit the creation of files and
directories by the user.
rmdir: Removing Directories
The rmdir (remove directory) command removes directories. The directory pis is removed as
follows:
rmdir pis Directory must be empty
rmdir can also delete more than one directory in one shot.
rmdir pis/data pis/progs pis
The above command deletes three directories and subdirectories.
When user deletes a directory and its subdirectories, a reverse logic has to be applied.
$ rmdir pis pis/progs pis/data
rmdir: directory “pis” : Directory not empty
1. User cannot delete a directory with rmdir unless it is empty. In above example, the pis
directory couldn’t be removed because of the existence of the subdirectories, progs and
data under it.
2. User can’t remove a subdirectory unless he is placed in a directory which is hierrachically
above the one he has chosen to remove.
$ cd progs
$ pwd
/home/kumar/pis/progs
$ rmdir /home/kumar/pis/progs
rmdir : directory “/home/kumar/pis/progs” : Directory does not exist
To remove this directory, user must position himself in the directory above progs i.e pis and then
remove it from there:
SDMIT, Ujire Page 8
$ cd /home/kumar/pis
$ pwd
/home/kumar/pis
$ rmdir progs
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 login.sql
will work only if the file login.sql exists in your current directory.
If user is placed in /usr and want to access login.sql in /home/kumar, then user cannot use above
command. User should use pathname of the file:
cat /home/kumar/login.sql
The first character of pathname is /. The file’s location must be determined with respect to root
(the first /). Such a pathname is called absolute pathname. When you have more than one / in a
pathname, for each such /, you have to descend one level in the file system. Thus kumar is one
level below home and two levels below root. No two files in a UNIX system can have identical
absolute pathnames. There can be two files with the same name, but in different directories.
Their pathnames will also be different. Thus, the file /home/kumar/progs/c2f.pl can coexist
with the file /home/kumar/safe/c2f.pl
Using the Absolute Pathname for a Command
UNIX command runs by executing its disk file. When user specify the date command, the
system has to locate the file date from a list of directories specified in the PATH variable, and
then execute it. If user knows the location of a particular command, user can precede its name
with the complete path. Since date resides in /bin (or /usr/bin), user can also use the absolute
pathname.
$ /bin/date
SDMIT, Ujire Page 9
Thu Sep 1 09:30:49 IST 2005
If you execute programs residing in some other directory that isn’t in PATH, the absolute
pathname that needs to be specified. For example, to execute the program less residing in
/usr/local/bin, user 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.
Relative Pathnames
Consider the following commands
cd progs
cat login.sql
In above example, both progs and login.sql are presumed to exist in the current directory. If
progs also contains a directory scripts under it, user still won’t need an absolute pathname to
change to that directory.
cd progs/scripts progs is in current directory
In above example, we have a pathname that has a /, but it is not an absolute pathname because it
doesn’t begin with a /.
Using . and .. in Relative Pathnames
Relative Pathname uses either the current or parent directory as reference and specifies the path
relative to it.
A relative pathname uses one of the following two cryptic symbols:
. (A single dot)- This represents the current directory.
.. (Two dots) - This represents the parent directory.
Assuming that you are placed in /home/kumar/progs/data/text, you can use .. as an argument to
cd to move to the parent directory /home/kumar/progs/data
SDMIT, Ujire Page 10
$ pwd
/home/kumar/progs/data/text
$ cd ..
$ pwd
/home/kumar/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”
To move to /home, user can use
cd /home
Alternatively,user can use a relative pathname
$ pwd
/home/kumar/pis
$ cd ../..
$ pwd
/home
Any command which uses the current directory as argument can also work with a single dot.
cp ../sharma/.profile .
The above line copies the file .profile to the current directory (.)
The UNIX FILE SYSTEM
UNIX file system contains a single root directory. Root directory contains many subdirectories
under it. Entire file system comprises of two groups of files. The first group contains the files
that are made available during system installation.
• /bin and /usr/bin - These are the directories where all the commonly used UNIX
commands (binaries) are found. PATH variable always shows these directories in its list.
• /sbin and /usr/sbin – These directories contains commands that user’s cannot execute
but system administrator can execute.
SDMIT, Ujire Page 11
• /etc - This directory contains the configuration files of the system. User can change a
very important aspect of system functioning by editing a text file in this directory. User’s
login name and password are stored in files /etc/passwd and /etc/shadow
• /dev – This directory contains all the device files. These files don’t occupy space on disk.
There could be more subdirectories like pts, dsk and rdsk in this directory.
• /lib and /usr/lib - It contains all libraries in binary form.
• /usr/include - It contains the standard header files used by C program. The statement
#include<stdio.h> used in most C programs refers to the file stdio.h in this directory.
• /usr/share/man – This is where the man pages are stored. There are separate
subdirectories (like man1,man2,etc) that contain the pages for each section.
Users also work with their own files. They write programs, send and receive mail and also create
temporary files. These files are available in the second group shown below:
• /tmp – The directories where users are allowed to create temporary files. These files are
wiped away regularly by the system.
• /var – The variable part of the file system. Contains all print jobs and outgoing and
incoming mail.
• /home - In many systems, users are housed here. The user kumar would have his home
directory in /home/kumar. System may use a different location for home directories.
SDMIT, Ujire Page 12