Module3cont UNIX
Module3cont UNIX
These three types of files are discussed in detail in the following sections –
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
is 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.
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.
include sbin
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.
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.
4
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.
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
NOTE that cd command may fail when you do not have permission to access a directory.
5
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
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.
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.
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.
7
So, one can say that, if the pathname starts with / (root), we can say that it is absolute path.
Otherwise, it is relative path.
$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
8
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
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.
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
9
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
Here, the symbols * and / are type indicators. The * indicates that the file contains
executable code, and / refers to a directory.
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 –
This document can be downloaded from www.chetanahegde.in with most recent updates. 10
10
Notes for UNIX Programming (16MCA12)
$ 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
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
This document can be downloaded from www.chetanahegde.in with most recent updates. 11
11
Notes for UNIX Programming (16MCA12)
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.
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.
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 user-id (UID) – both its name and numeric representation
The group-id (GID) – both its name and numeric representation
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)
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 –
rwx r-x r--
Each group represents a category viz. owner, group owner and others (or world)
respectively. Every group contains any of the characters r, w, x and -. The meaning of
these is as below –
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
This document can be downloaded from www.chetanahegde.in with most recent updates. 13
13
Notes for UNIX Programming (16MCA12)
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.
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:
User category (user: u, group: g, others: o, All: a)
The operation to be performed (assign: +, remove: –, assign absolute permission: =)
The type of permission (read: r, write: w, execute: x)
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 –
This document can be downloaded from www.chetanahegde.in with most recent updates. 14
14
Notes for UNIX Programming (16MCA12)
When you are willing to assign a particular permission to all, then even the character a can
be omitted as below –
$ chmod +x test #assign(+) x to all
When same set of permissions has to be assigned to more than one file, then we can give
multiple files separated with space as –
$ chmod u+x test test1 test2
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 –
Now, let us see some examples of using the absolute permissions with the help of octal
digits.
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 –
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).
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
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.
This document can be downloaded from www.chetanahegde.in with most recent updates. 17
17
Notes for UNIX Programming (16MCA12)
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.
Now the file is virtually useless, as no one can do anything with it. But, user can still delete
this file. At the same time, one must be aware that giving all permissions to everyone is
dangerous. That is, having the following statement makes the file
readable/writable/executable for everyone.
$chmod 777 test
So, anyone can modify the contents of the file and it is a threat on security.
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.
The first entry in the above output is the inode number of the file test. No other file in the
same file system can have this inode number. Once the file is removed, the inode number
can be assigned to some other 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 –
NOTE:
1. Links provide protection against accidental deletion, especially when they exist in
different directories. Assume that a file emp.lst is in /home/data directory and
you have created a link for it in /home/imp_files directory. The deletion of the file
in one directory will not remove the file in the other directory and hence, you can
always get the file back.
This document can be downloaded from www.chetanahegde.in with most recent updates. 19
19
Notes for UNIX Programming (16MCA12)
2. Having multiple names for a single file is NOT same as having multiple copies of
single file. When a copy of one file is created, those two copies will have different
inode numbers, and their link count will be one. But, when a file is linked, then all
such files will have unique inode numbers and their link count will be more than one.
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.
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.
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.
/home/john/test.sh
/home/john/test2.sh
/home/john/ifEx.sh
/home/john/test1.sh
/home/john/Shell1.sh
/home/john/myDir/Shell1.sh
/home/john/second.sh
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.