17CS35 Module -2- Unix File System - VTUPulse.com
17CS35 Module -2- Unix File System - VTUPulse.com
1. THE FILE
The file is the container for storing information.
Neither a file's size nor its name is stored in file.
All file attributes such as file type, permissions, links, owner, group owner etc are kept in
a separate area of the hard disk, not directly accessible to humans, but only to kernel.
1. Ordinary file – also called as regular file. It contains only data as a stream of characters.
2. Directory file – it contains files and other sub-directories.
3. Device file – all devices and peripherals are represented by files.
Ordinary File - ordinary file itself can be divided into two types-
1. Text File – it contains only printable characters, and you can often view the contents and
make sense out of them.
2. Binary file – it contains both printable and unprintable characters that cover entire ASCII
range.
Examples- Most Unix commands, executable files, pictures, sound and video files are
binary.
Directory File - a directory contains no data but keeps some details of the files and
subdirectories that it contains. A directory file contains an entry for every file and subdirectories
that it houses. If you have 20 files in a directory, there will be 20 entries in the directory. Each
entry has two components-
the filename
a unique identification number for the file or directory (called as inode number).
Device File - Installing software from CD-ROM, printing files and backing up data files to
tape. All of these activities are performed by reading or writing the file representing the device.
Advantage of device file is that some of the commands used to access an ordinary file also work
with device file. Device filenames are generally found in a single directory structure, /dev.
The feature of UNIX file system is that there is a top, which serves as the reference point
for all files.
This top is called root and is represented by a / (Front slash).
The root is actually a directory.
The root directory (/) has a number of subdirectories under it.
The subdirectories in turn have more subdirectories and other files under them.
Every file apart from root, must have a parent, and it should be possible to trace the
ultimate parentage of a file to root.
In parent-child relationship, the parent is always a directory.
$ 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
$mkdir patch
This creates three subdirectories – progs, cprogs and javaprogs under progs.
The order of specifying arguments is important. You cannot create subdirectories before
creation of parent directory.
For instance following command doesn‘t work
$rmdir progs
First it removes cprogs and javaprogs form progs directory and then it removes progs
fro system.
9. ABSOLUTE PATHNAME
Directories are arranged in a hierarchy with root (/) at the top. The position of any file
within the hierarchy is described by its pathname.
Elements of a pathname are separated by a /. A pathname is absolute, if it is described in
relation to root, thus absolute pathnames always begin with a /.
Following are some examples of absolute filenames.
/etc/passwd
/users/kumar/progs/cprogs
/dev/rdsk/Os3
Example
This method is compact and easy when ascending the directory hierarchy. The command
cd .. Translates to this ―change your current directory to parent of current directory‖.
The following command copies the file prog1.java present in javaprogs, which is present
is parent of current directory to current directory.
$pwd
/home/kumar/progs/cprogs
$cp ../javaprogs/prog1.java .
Now prog1.java is copied to cprogs under progs directory.
As like other files cat accepts more than one filename as arguments
In this the contents of the second files are shown immediately after the first file without any
header information. So cat concatenates two files- hence its name.
cat OPTIONS
Displaying Nonprinting Characters (-v)
cat without any option it will display text files. Nonprinting ASCII characters can be
displayed with –v option.
Numbering Lines (-n)
-n option numbers lines. This numbering option helps programmer in debugging
programs.
cat is also useful for creating a file. Enter the command cat, followed by > character and the
filename.
When the command line is terminated with [Enter], the prompt vanishes. Cat now waits to take
input from the user. Enter few lines; press [ctrl-d] to signify the end of input to the system
To display the file contents of new use file name with cat command.
$ cat new
This is a new file which contains some text, just to
Add some contents to the file new
cp can also be used with the shorthand notation, .(dot), to signify the current directory
as the destination. To copy a file „new‟ from /home/user1 to your current directory,
use the following command:
$cp /home/user1/new new destination is a file
$cp /home/user1/new . destination is the current directory
cp command can be used to copy more than one file with a single invocation of the
command. In this case the last filename must be a directory.
Ex: To copy the file ch1,chh2,ch3 to the module , use cp as
$ cp ch1 ch2 ch3 module
The files will have the same name in module. If the files are already resident in
module, they will be overwritten. In the above diagram module directory should
already exist and cp doesn‘t able create a directory.
UNIX system uses * as a shorthand for multiple filenames.
Ex:
$ cp ch* usp Copies all the files beginning with ch
cp options
Interactive Copying(-i) : The –i option warns the user before overwriting the
destination file, If unit 1 exists, cp prompts for response
$ cp -i ch1 unit1
$ cp: overwrite unit1 (yes/no)? Y
A y at this prompt overwrites the file, any other response leaves it uncopied.
rm options
Interactive Deletion (-i) : Ask the user confirmation before removing each file:
$ rm -i ch1 ch2
rm: remove ch1 (yes/no)? ? y
rm: remove ch1 (yes/no)? ? n [Enter]
A ‗y‘ removes the file (ch1) any other response like n or any other key leave the file
undeleted.
Recursive deletion (-r or -R): It performs a recursive search for all directories and
files within these subdirectories. At each stage it deletes everything it finds.
$ rm -r * Works as rmdir
It deletes all files in the current directory and all its subdirectories.
Ex: To rename the file csb as csa we can use the following command
$ mv csb csa
If the destination file doesn‘t exist in the current directory, it will be created. Or else
it will just rename the specified file in mv command.
A group of files can be moved to a directory.
Ex: Moves three files ch1,ch2,ch3 to the directory module
$ mv rename newname
mv replaces the filename in the existing directory entry with the new name. It doesn't
create a copy of the file; it renames it
Group of files can be moved to a directory
mv chp1 chap2 chap3 unix
----More--- (15%)
Navigation
f or Spacebar: to scroll forward a page at a time
b to move back one page
The ls outpu won‘t fit on the screen if there are too many files, So the command can
be used like this:
ls | more
The pipeline of two commands where the output of two commands, where the output
of one is used as the input of the other.
$ wc ofile
4 20 97 ofile
wc offers 3 options to make a specific count. –l option counts only number of lines, -
w and –c options count words and characters, respectively.
$ wc -l ofile
4 ofile
$ wc -w ofile
20 ofile
Multiple filenames, wc produces a line for each file, as well as a total count.
$ wc -c ofile file
97 ofile
15 file
112 total
$ od –b file
0000000 164 150 151 163 040 146 151 154 145 040 151 163 040 141 156 040
0000020 145 170 141 155 160 154 145 040 146 157 162 040 157 144 040 143
0000040 157 155 155 141 156 144 012 136 144 040 165 163 145 144 040 141
0000060 163 040 141 156 040 151 156 164 145 162 162 165 160 164 040 153
0000100 145 171
-c character option
Now it shows the printable characters and its corresponding ASCII octal
representation
$ od –bc file
od -bc ofile
0000000 164 150 151 163 040 146 151 154 145 040 151 163 040 141 156 040
T h i s f i l e i s a n
0000020 145 170 141 155 160 154 145 040 146 157 162 040 157 144 040 143
e x a m p l e f o r o d c
0000040 157 155 155 141 156 144 012 136 144 040 165 163 145 144 040 141
o m m a n d \n ^ d u s e d a
0000060 163 040 141 156 040 151 156 164 145 162 162 165 160 164 040 153
s a n i n t e r r u p t k
0000100 145 171
e y
The UNIX file system allows the user to access other files not belonging to them and without
infringing on security. A file has a number of attributes (properties) that are stored in the inode. In
this chapter, we discuss,
ls –l to display file attributes (properties)
Listing of a specific directory
Ownership and group ownership
Different file permissions
For example,
$ ls -l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap02
drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir
drwxr-xr-x 3 kumar metal 512 may 09 11:05 progs
Directories are easily identified in the listing by the first character of the first column,
which here shows a d.
The significance of the attributes of a directory differs a good deal from an ordinary
file.
To see the attributes of a directory rather than the files contained in it, use ls –ld with
the directory name. Note that simply using ls –d will not list all subdirectories in the
current directory. Strange though it may seem, ls has no option to list only directories.
File Ownership
When you create a file, you become its owner. Every owner is attached to a group
owner. Several users may belong to a single group, but the privileges of the group are
set by the owner of the file and not by the group members. When the system
administrator creates a user account, he has to assign these parameters to the user:
File Permissions
UNIX follows a three-tiered file protection system that determines a file‘s access rights. It is
displayed in the following format: Filetype owner (rwx) groupowner (rwx) others (rwx)
For Example:
-rwxr-xr-- 1 kumar metal 20500 may 10 19:21 chap02
rwx r-x r--
owner/user group owner others
The first group has all three permissions. The file is readable, writable and executable
by the owner of the file.
The second group has a hyphen in the middle slot, which indicates the absence of
write permission by the group owner of the file.
The third group has the write and execute bits absent. This set of permissions is
applicable to others.
You can set different permissions for the three categories of users – owner, group and
others. It‘s important that you understand them because a little learning here can be a
dangerous thing. Faulty file permission is a sure recipe for disaster.
A file or a directory is created with a default set of permissions, which can be determined by
umask. Let us assume that the file permission for the created file is -rw-r-- r--. Using chmod
command, we can change the file permissions and allow the owner to execute his file.
Relative Permissions
chmod only changes the permissions specified in the command line and leaves the
other permissions unchanged.
Its syntax is:
chmod category operation permission filename(s)
chmod takes an expression as its argument which contains:
user category (user, group, others)
operation to be performed (assign or remove a permission)
type of permission (read, write, execute)
Initially,
-rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart
$chmod u+x xstart
-rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart
The command assigns (+) execute (x) permission to the user (u), other permissions
remain unchanged.
$chmod ugo+x xstart or chmod a+x xstart or chmod +x xstart
$ls –l xstart
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart
Let initially,
Then, it becomes
$ls –l xstart
-rwx—x--x 1 kumar metal 1906 sep 23:38 xstart
Absolute Permissions
Here, we need not to know the current file permissions. We can set all nine permissions
explicitly. A string of three octal digits is used as an expression. The permission can be
represented by one octal digit for each category. For each category, we add octal digits. If
we represent the permissions of each category by one octal digit, this is how the
permission can be represented:
We have three categories and three permissions for each category, so three octal digits
can describe a file‘s permissions completely. The most significant digit represents user
and the least one represents others. chmod can use this three-digit string as the expression.
Using relative permission, we have,
$chmod a+rw xstart
Using absolute permission, we have,
$chmod 666 xstart
$chmod 644 xstart
$chmod 761 xstart
will assign all permissions to the owner, read and write permissions for the group and
only execute permission to the others.
777 signify all permissions for all categories, but still we can prevent a file from being
deleted.
000 signifies absence of all permissions for all categories, but still we can delete a file.
It is the directory permissions that determine whether a file can be deleted or not.
Only owner can change the file permissions. User cannot change other user‘s file‘s
permissions.
But the system administrator can do anything.
-rw-r—r- -
----------
This is simply useless but still the user can delete this file.
On the other hand,
-rwxrwxrwx
The UNIX system by default, never allows this situation as you can never have a secure
system. Hence, directory permissions also play a very vital role here .
Directory Permissions
It is possible that a file cannot be accessed even though it has read permission, and can be
removed even when it is write protected. The default permissions of a directory are,
rwxr-xr-x (755)
$mkdir c_progs
If a directory has write permission for group and others also, be assured that every user
can remove every file in the directory. As a rule, you must not make directories
universally writable unless you have definite reasons to do so.
chown
Changing ownership requires super user permission, so use su command
$ls -l note
-rwxr----x 1 kumar metal 347 may 10 20:30 note
Once ownership of the file has been given away to sharma, the user file permissions that
previously applied to Kumar now apply to sharma. Thus, Kumar can no longer edit note
since there is no write privilege for group and others. He cannot get back the ownership
either. But he can copy the file to his own directory, in which case he becomes the owner
of the copy.
chgrp
This command changes the file‘s group owner. No super user permission is required.
#ls –l dept.lst
In this chapter we considered two important file attributes – permissions and ownership.
After we complete the first round of discussions related to files, we will take up the other
file attributes.