Unix - Module 2
Unix - Module 2
Unix - Module 2
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 2
user also can create them, as and when required. Usually, a group of related files are kept in a
single directory. Sometimes, files with same name are kept in different directories.
A directory file contains an entry for every file and subdirectory it has. Each such entry has two
components viz. –
The filename
A unique identification number for the file or directory (called as the inode number)
Thus, a directory actually do not contain the file itself, rather, it contains only the file name and a
number. One cannot write into a directory file. But, the actions like creating a file, removing a file
etc. makes kernel to update the corresponding directory by creating/removing filename and inode
number associated with that file.
Device filenames are generally found inside a single directory structure, /dev. A device file not a
stream of characters. In fact, it does not contain anything. The operation of a device is completely
managed by the attributes of its associated file. The kernel identifies a device from its attributes
and then uses them to operate the device.
UNIX does not impose any rule for framing the extensions for filenames. Even shell scripts do not
require .sh as extension. It is used only for the convention. But, underlying programming languages
like C requires extension. Hence, in UNIX, a filename can contain any number of dots – say,
a.b.c.d.e is a valid filename in UNIX. A filename can begin/end with a dot. But, UNIX is case
sensitive and same is maintained in naming the files as well. Thus, test, Test, TEST all are different
files.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 3
The root directory (/) has a number of subdirectories under it. These subdirectories in turn have
more subdirectories and files under them. Figure 2.1 shows an example of UNIX file system tree
structure. Here, bin, dev etc. are directories under root (/). And, mthomas, stu1 are subdirectories
under home.
Every file, apart from root, must have a parent, and there will be a parent-child relationship path
from root to file. In the Figure 2.1, cp is child of bin and bin is child of /. That is, / is grandparent
of cp. Note that, in a parent-child relationship, parent is always a directory.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 4
The path displayed here is known as absolute pathname, which is a sequence of all directory names
separated by slash (/) starting from root. A file foo located in a home directory of the user can be
referred as $HOME/foo. In some of the shells, it can be referred as ~/foo. Here, the ~ symbol can
be used to refer any other’s file also. For example, if there is a file called foo in another user
richard’s directory also, then it can be referred as ~richard/foo.
Note that, a ~ (tilde) followed by / refers to one’s own home directory, but when followed by
a string (like ~richard), it refers to home directory represented by that string.
• If the user john is in his home directory and would like to move to subdirectory called as
progs, then the command should be given as –
$ cd progs # user is moved to progs directory now
$ pwd # verify this using pwd
/home/john/progs
• If the user would like to shift some other directory (but not his own subdirectory), then
absolute path name can be given. For example,
$ pwd #check current directory
/home/john/progs
$ cd /bin # change directory to /bin
$ pwd # verify new location
/bin
• When cd is used without argument, it will take the user back to his home directory, that is
where he had logged into.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 5
Ex1.
$ pwd #check current directory
/home/john/progs
$ cd #no arguments given
$ pwd
/home/john #back to home directory
Ex2.
$cd /home/tony #john moves to tony’s home directory
$pwd #verify
/home/tony
$cd #cd given without argument
$pwd #john is back to his home directory
/home/john
• The name of the directory to be created has to be given as argument. For example,
$mkdir docs
• One can create more than one directory with a single mkdir command as below –
$mkdir docs progs db # three directories created
Now, initially the directory test will be created. Then two subdirectories prgms and data will be
created under test. Note that, while creating such directory tree, the parent directory name has to
be given first.
• Sometimes, trying to create a directory may fail and error message may get displayed as –
$mkdir myDir
mkdir: Failed to make directory “myDir”; Permission denied
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 6
To remove (or delete) a directory, the rmdir command is used. Few important points about this
command are discussed here –
• A directory has to be empty before removing it. That is, it should not contain any files or
subdirectories.
• To remove one directory, use statement like –
$rmdir test #removes the directory test
• More than one directory (even in tree structure) can be removed at a time. For example,
$mkdir test/prgms test/data test
Note that, here prgms and data are two subdirectories under test. They should be given first
and then the parent directory test has to be mentioned.
• To remove a particular directory, you should be hierarchically above that directory. That is you
cannot remove any directory that is above your current directory and also, you cannot remove
your current directory as well.
• The commands mkdir and rmdir can work only in the directories owned by the user. But they
cannot be implemented on the directories of some other user.
it is assumed that the file test.sh is in current directory. If you would like to access the file in
some other directory, you have to give the command by specifying absolute pathname of that file
as –
$ cat /home/john/test.sh
Here, one can observe that the absolute pathname of a file starts with / (indication for root) and
goes one level down for every appearance of / (as a separator). That is, in the above example,
test.sh is at three levels down from root.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 7
We know that, more than one files with a same name may exists in different directories of UNIX
system. But, their absolute pathnames will be different. That is, no two files in the UNIX file
system can have same absolute pathnames.
Executing UNIX commands like this will not gain anything, it is just an illustration. But, if user’s
program resides in some other directory and to be executed from somewhere else, then the absolute
pathname will be helpful. For example, your current directory is /home/john. You would like to
run a file test.sh which is in /home/richard. Then you use the command
$ sh /home/richard/test.sh
A shell variable PATH includes the list of various pathnames – files in that can be executed
directly. If you want to execute many files within a particular directory, that pathname can be
included in the PATH variable. And each time, one need not use the complete absolute path for the
execution purpose.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 8
$pwd
/home/john/progs/data/text
$cd .. #observe space between cd and ..
$pwd #check now
/home/john/progs/data
Thus, the usage of .. moves the directory one level up. If we want to move more than one level
up, we can use .. more than once separated by / as shown below –
$pwd
/home/john/progs/data/text
$cd ../../.. #moves 3 levels up
$pwd
/home/john
Any command which uses the current directory as argument can work with single dot. For example,
to run a shell script (Refer the example Shell1.sh given in Section 1.7.2 of Module1), we can
use ./(dot with slash). Also, the single dot indicating current directory is useful in copying the
files. For example, assume there is a file shell1.sh in the current directory. And, we want to copy
it into directory myDir which is within current directory. Now we can use the command –
$cp ./shell1.sh ./myDir
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 9
• /bin and /usr/bin: Commonly used UNIX commands (binaries, hence the name bin) are
found here. PATH variable always shows these directories in its list.
• /sbin and /usr/sbin : The commands which can be executed only by system administrator
(but not by a normal user ) are kept in this directory. The PATH variable of system
administrator contains these directories.
• /etc : This directory contains the configuration files of the system. Modification of any text
file in this directory may affect the functionality of the system. Login name and password
of users will be stored in the files /etc/passwd and /etc/shadow respectively.
• /dev : This directory contains all device files. These files do not occupy space on the disk.
There can be more subdirectories like pts, dsk and rdsk in this directory.
• /lib and /usr/lib : This directory contains all library files in binary form. If you want to run
C program, you may need to link those programs with files in these directories.
• /usr/include : Contains standard header files like stdio.h, stdlib.h etc. used by C programs.
• /usr/share/man : All the man (manual) pages are stored here. There are separate
subdirectories like man1, man2 etc that contain the pages for each section.
The second group contains the files/programs/mails created by the users as shown below –
• /tmp : The directories where users are allowed to create temporary files. These files are
removed regularly by the system.
• /var: It is a variable part of the file system. Contains all printable jobs and
outgoing/incoming mails.
• /home: Home directories of all the users are stored here.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 10
When ls command is used to display the files, one can observe that (in most of UNIX systems,
but not always in Linux) the files will be displayed in alphabetical (rather ASCII) order. That is,
files starting with numeric first, then uppercase letters and then lowercase letters. This is known
as ASCII collating sequence. The files and directories are listed as a single column (older
versions of ls do so) and, if you are using Linux systems, then they might be showed up in
different colors.
To check whether a particular file exists in a directory or not, one can give the file name along
with ls command. For example,
$ ls test #checking whether the file test is there test
#file name is displayed if it exists
Here, the file name is displayed if that file exists in that directory. Otherwise, an error message will
be displayed as shown below –
$ ls test
test: No such file or directory
2.13.1 ls Options
The command ls has multiple options for various purposes. Some of them are discussed here.
• Output in Multiple Columns (–x) : When there are many files, it is better to display them
in multiple columns. Modern versions of ls do that by default (without any options), but if
it doesn’t happen in your system, you can use –x option as –
$ ls –x
Thesis Shell1.sh Shell2.sh ShellPgms Emp.txt cmd.c helpdir
• Identifying Directories and Executables (–F) : The ls command displays files as well as
directories. To know which of them are directories and executable files, one can use –F
option. When –F option is combined with –x, it produces multicolor output.
$ ls –Fx
Thesis/ Shell1.sh* Shell2.sh* ShellPgms/
Emp.txt cmd.c helpdir/
Here, the symbols * and / are type indicators. The * indicates that the file contains executable code,
and / refers to a directory.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 11
• Showing Hidden Files Also (–a): If we want to see hidden files also, we use –a (all) option
for ls. There are certain hidden files starting with a dot, which normally don’t get displayed
with just ls command.
$ ls –a
. .. .exrc Thesis
.emacs .gnome2 Shell1.sh
Note that, the first two files displayed are . and .. indicating current and parent directories.
• Listing specific directory contents: If you want to display the contents of only specific
subdirectories, you can give the name along with ls as shown below –
$ ls ACMPaper ShellPgms
ACMPaper:
acm.aux acm.bib acm.pdf runtex
acm.tex sig.pdf
ShellPgms:
caseEx.sh first.sh menu.sh test
hello.c
In the above example, ACMPaper and ShellPgms are directory names. When they are
given along with ls command, the files within them are displayed separately.
• Recursive Listing (–R): This option lists all files and subdirectories in a directory tree.
That is, contents of subdirectories also will be displayed recursively till there is no
subdirectory is left out.
$ ls –R
.:
Thesis Shell1.sh Shell2.sh ShellPgms
Emp.txt cmd.c
./Thesis:
Chap1.aux Chap1.bib Chap1.tex Chap1.pdf
Annex.aux Annex.pdf
./ShellPgms:
caseEx.sh first.sh menu.sh test
hello.c
./ShellPgms/helpdir:
Test.c here.sh try.sh
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 12
One can observe that, the –R option starts display with the current directory (.). Then it displays
the contents of all subdirectories under current directory. Later it goes one level down and so on.
$ ls -l
total 144
-rw-rw-r-- 1 john john 280 Jan 30 09:56 caseEx.sh
-rw-rw-r-- 1 john john 104 Feb 3 06:40 cmdArg.sh
-rw-rw-r-- 1 john john 199 Jan 29 10:58 ifEx.sh
-rw-rw-r-- 1 john john 217 Jan 19 09:25 logfile
drwxrwxr-x 2 john john 4096 Feb 6 05:48 myDir
-rwxrwxr-x 1 john john 29 Jan 22 10:04 myFirstShell
-rw-rw-r-- 1 john john 43 Jan 22 10:44 second.sh
The output of ls –l starts with total 144, indicates that a total of 144 blocks are occupied by these
files on disk. The 7 types of attributes/fields displayed by the command are discussed below –
• File Type and Permissions: The first column shows the type and permissions associated
with each file. If the first character in this column is – (hypen), then it is an ordinary file.
On the other hand, if the first character is d, then it is a directory. Then, there is a series of
r, w, x and – (hyphen) indicating file permissions read, write and execute. The hyphen
indicates absence of particular permission.
• Links: The second column indicates the number of links associated with the file. It is a
number of filenames maintained by the system of that file. Usually for ordinary files, it will
be just 1. But for directories, it will be number of files contained within that directory
(including current directory).
• Ownership: The creator of the file would be its owner. In the third column, it shows john
as the owner. The owner has full authority to modify the contents and permissions of a file.
Similarly, the owner of a directory can create modify or remove files in that directory.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 13
• Group Ownership: While opening a user account, a system administrator assigns the user
to some group. The fourth column represents the group owner of that file.
• File Size: Size of the file in bytes is shown as fifth column. The size is only a character
count of the file, but not the amount of space it occupies in the disk.
• Last Modification Time: The 6th, 7th and 8th columns indicate the last modification time
of the file. A file is said to be modified only if its contents have changed. If you change
only the permission or ownership of the file, its last modification time field will not get
affected.
Filename: The last field is the name of the file, usually in ASCII collating sequence.
2.13.3 ls –d option: Listing Directory Attributes
If we want to list the attributes of only the directory, but not its contents, we can use –d option as below –
$ ls –ld myDir
drwxrwxr-x 2 john john 4096 Feb 6 05:48 myDir
Several users may belong to a single group. The files created by group members will have the
same group owner. However, the privileges of the group are set by the owner of the file, but not
by group members.
When the system administrator creates a user account, he has to assign the following parameters
to the user:
The file /etc/passwd maintains the UID (both name and number) and GID (only the number). The
file /etc/group contains the GID (both name and number). To know your UID and GID, the id
command can be used as –
$id
uid=821(chetana) gid=822(STAFF)
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 14
Observe the first column representing permission for the file chap02. Here, the first character says
whether the file is ordinary file or directory. So, leaving it apart, consider next 9 characters as a
group of 3 characters each –
• r: indicates read permission – means, cat command can display the file
• w: indicates write permission – file can be edited with an editor
• x: indicates execute permission – the file can be executed as a program -: indicates
absence of the corresponding permission
Usually, the owner of a file will have all the three permissions. In the above example, the group
owner of the file chap02 has only read and execute permissions. The public or others or world has
only read permission.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 15
It is observed here that, by default the execute permission is not there even for the user (owner).
Keeping this status of the file test as a base, let us discuss different ways of using chmod command.
The argument for chmod is an expression consisting of some letters and symbols describing user
category and type of permission being assigned/removed. The expression contains three
components:
Now, consider the example of the file test taken before, and assign execute permission to it as
below –
Now, the user john got permission to execute the file test. If you want to assign execute permission
on file test to group and others also, then use the command as –
When you are willing to assign a particular permission to all, then even the character a can be
omitted as below –
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 16
When same set of permissions has to be assigned to more than one file, then we can give multiple
files separated with space as –
To remove a permission, the – (hyphen or minus) operator is used. For example, to remove read
permission from group and other, we can do as below –
$ ls –l test #verify
-rwx—x--x 1 john richard 853 Sep 5 23:38 test
Multiple expressions separated by comma can be given to chmod command. For example, to
remove the execute permission from all and then to assign read permission to group and others, a
single statement can be used as –
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 17
Now, let us see some examples of using the absolute permissions with the help of octal digits.
Ex 1. Assigning read and write(4+2=6) permissions to all –
$ chmod 666 test
$ ls –l test
-rw-rw-rw- 1 john richard 853 Sep 5 23:38 test
Note that, there is nothing like removing some permission. It is just reassignment of new set of
permissions to all.
Ex 3. To assign all permissions to owner, read and write permissions to group and only execute
permission to others –
$ chmod 761 test
$ ls –l test
-rwxrw---x 1 john richard 853 Sep 5 23:38 test
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 18
This makes all files and subdirectories found in the tree-walk (starting from ShellPgms directory,
includes all files in subdirectories) executable by all users. One can provide multiple directory and
filenames for this purpose. If chmod has to be applied on home directory tree, one can use any one
of the following –
$chmod –R 755 . #works on hidden files also
$chmod –R a+x * #leaves out hidden files
(NOTE: Berkeley Software Distribution (BSD) was a Unix operating system derivative
developed and distributed by the Computer Systems Research Group (CSRG) of the University
of California, Berkeley, from 1977 to 1995.)
To use chown command in BSD-based systems, we need the super-user permission. For that, the
su command is used as below –
$su
Password: ******** (this is root password)
#_ (This is another shell)
The command su lets us to acquire superuser status (Note that # is the prompt for admin, in most
cases $ is retained). Now, try to change the ownership of a file note which is currently owned by
john as below–
$ ls –l note
-rwxr----x 1 john metal 347 May 10 20:30 note
$ chown ricky note
$ ls –l note
-rwxr----x 1 ricky metal 347 May 10 20:30 note
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 19
Here, the ownership of the file note has been changed from john to ricky. The file permissions
previously assigned to john will now be of ricky. Now onwards, john is not the owner of this
file and he cannot read/write this file.
Assume that john is a member of two groups metal and dba. And he has created a file dept.txt in
metal group. He can change the group owner as below –
$ ls –l dept.txt
-rw-r--r-- 1 john metal 129 Jun 8 16:42 dept.txt
$ chgrp dba dept.txt
-rw-r--r-- 1 john dba 129 Jun 8 16:42 dept.txt
When the user is not a member of particular group, he cannot change the group owner of any file
to that group. Only superuser can do so.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 20
Observe that neither the name of the file nor the inode number is stored in the inode. The
directory stores these details along with the filename. When you use any command with filename
as an argument, the kernel first locates the inode number of the file from the directory and then
reads the inode to fetch data relevant to the file.
The ls command uses inode to fetch the attributes of a file. The –i option with ls command can
be used to check inode number of a file.
2.20 HARDLINKS
We have seen in the previous section that, the inode table does not contain the name of a file.
Reason is – a file can have multiple names. In that case, we say that the file has more than one link.
Multiple names provided to one single file are all having same inode number. The link count is
displayed when ls –l is used. It is usually one.
A link can be created to a file using ln command. The following command is used to create hard
link for an existing file emp.lst with a non-existing file employee:
Observe that both the files emp.lst and employee have same inode number, and the link
count is 2. One can link one more file say, emp.dat as below –
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 21
• One cannot have two linked filenames in two file systems. That is, one cannot link a
filename in the /usr file system to another in /home file system.
• One cannot link a directory even within the same file system.
The symbolic links or soft links will overcome these limitations. The symbolic link can be thought
of as a fourth type of a file (apart from 3 types discussed till now – ordinary, directory and device).
Unlike the hard link, a symbolic link doesn’t have the file’s contents. But, it simply provides the
pathname of the file that actually has the contents. Shortcut keys in windows are the best examples
for symbolic links.
Compared to hard links, one can find following differences in the listing of symbolic link file–
Original file and symbolic linked file have different inode numbers.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 22
• File type of note.sym shows l (el) indicating it is not an ordinary file, but a symbolic link file.
• Size of the file note.sym is just 4 – which is the length of the pathname it contains (note).
• The pointer notation at the end note.sym->note indicates that note.sym contains the
pathname for the filename note.
When we use cat command on note.sym, we are not opening the symbolic link file, but the
original note file. Removing note.sym will not affect much, because we can always create a link
again. But, if we remove note, we would lose the file containing data. In this case, note.sym
would point to a non-existent file and become a dangling symbolic link.
This default setting is transformed by subtracting the user mask from it to remove one or more
permissions. To understand this, we should know the current value of mask by using umask
command without arguments as –
$umask
0022
This is an octal number which has to be subtracted from the system default to obtain the actual
default. Hence, the actual default of files will be –
666 – 022 = 644 for ordinary files
777 – 022 = 755 for directories
Hence, when we create a new file on this system, the default permission of it would be –
rw-r--r--
The umask is a shell built-in command, though it exists as an external command. A user can use
this command to set a new default. For example,
$umask 000
The above command sets the umask to 000 and hence, any new file created will have the
permissions as 666-000 = 666 permission. That is, it would be rw-rw-rw-, which is dangerous
because anyone can write the file. Similarly, if you set
$umask 666
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ
Module 2: UNIX FILE SYSTEM (18MCA12) 23
Then, all files created will have permission as 666-666 =000, and it will be a useless file. So, the
mask has to be set carefully.
The criteria for selection has certain options (read UNIX manual page to know more options),
each one starting with – (hyphen). In the above example, it means that name of the files having
pattern *.sh have to be searched. That is, all files with .sh as extension have to be searched in
the given path. Then, the action to be taken is just –print. Hence, all the files with extension
.sh are printed in the given path /home/john.
By: Rama Satish KV, Asst Professor, RNSIT, B’lore. For latest updates visit: http://shorturl.at/ainTZ