Chapter 2: File System
Chapter 2: File System
One of the most attractive and elegant features of UNIX system is its file system. It is the file system, in which the user stores and retrieves information. File management in the UNIX system is extremely flexible and powerful so much so that its file system concepts have been widely adopted in other operating systems. File system is the logical Unix structure that contains files. A file is a collection of information that is it stores the user's data and programs.
Unlike ordinary files, the contents of the directory are under the control of the system and not the user. Special Files :Special files are used to devices a file is created. reference physical devices. Every device connected to a UNIX system is treated as a file. For each of the All communications with a particular device is made through their corresponding files. Each device whether it is a terminal or a disk or a printer has an associated file for it.
File System
system. Every file in the UNIX system has a name by which you can refer to it. If you think of the file as a container in which you can put data, then filename refers to the container as a whole. Thus, when you refer to a file by its filename, you are refering to the whole of the data contained within the file. Thus you can copy or move or process the whole of the data within a file by just using the filename. A few conventions are to be followed while choosing a filename. Namely: File names can have a maximum consist of 14 characters of in most UNIX implementations. characters. Do not begin a file with a + or -, else they may be confused with options. Avoid using special characters such as *, ?, [, ], (, ), {, }, <, >, :, $, #, @, /. Do not begin a file with a. Unless you explicitly desire them to be hidden. Choose filenames that give you a clue to the contents or purpose of the file. For example, the Use file file which contains data regarding employees in ending all an organisation could be given name emp.dat . naming conventions such as 'C' source files with an extension C. Note: UNIX treats all the file names a a string of bytes and does not assign any meaning to any particular file. They may letters, numbers or certain ASCII
File System Every file in UNIX is assigned to a directory. A directory is a place within the file system as a whole that contains files. The directory itself is a specialised form of file, which contains not text, or executable programs but a list of file names and some other information related to the files. your desk, where you can put things. The directory is analogues to a file drawer in In UNIX system you put files in directories,
Because the system treats all files as a sequence of data bytes, you can put any type of file in any directory; in fact you can put a directory into another directory. A directory contained within another directory is called a sub-directory. The subdirectories can themselves contain files and/or more sub-directories. This helps you understand the file system as a whole, because it is a hierarchy of directories and sub-directories with no limit to the depth of nested subdirectories. UNIX FILE SYSTEM : The facility and files. of having sub-directories within a directory makes it possible to develop a tree like structure of directories Thus, the UNIX file system can be viewed as an At the very top of the structure is the root inverted tree.
directory (denoted by/). Every non-leaf node of the file system structure is a directory and the leaf nodes of the tree are either directories or files. Thus, the directories can be thought of as branches of a tree. The file system can be represented as follows: / __________________________________________________________ | | | | | | | | bin dev etc lib usr tmp UNIX lost+found | ---------| | mrj accell Each of these directories have several other files and directories within it. Under the root we have standard sub-directories which are basically the same for most UNIX systems. Bin : Consists of most commonly used user related command files. Dev : Contains the device files associated with various devices. Etc : Consists of files that are essentially required for system administration purposes. Lib : Consists of various library routines required for C programs usr : Consists of sub-directories for all user login accounts. Within usr there are mrj, accell as subdirectories as shown above.
File System
File System For example, if youre current directory is /usr/frank and you decide to access prime. c, then we could access it by giving the partial pathname cprog/prime.c
Description :
File System
$ As you can see the system responds by listing in alphabetical order all the files and sub-directories in the current directory /usr/ravi If you want to print the names of files and sub-directories in a directory other than your current directory without moving from your current directory, you must specify the name of that directory as follows: $ ls pathname Note: The pathname may be either the absolute pathname or the relative pathname. Suppose you want to list all the files & directories in /usr, then you could give the command as follows: $ ls /usr bin raju ravi
or $ ls
Both the commands perform the same task. Note that ls, does not distinguish between files & directories. Let us look at some frequently used options with the ls commands. When ls was used to display the contents of a directory in the preceding examples, the files were listed on the screen in alphabetical order, one name per line of output. This format is seldom useful when there are a large number of files in the directory, as the names quickly scroll off the top of the screen. Thus, if you would like to list all the files with several filenames in a single line you could use the -C or -X option as shown below: $ ls -C account c.prog $ ls -X account xx.sh letter pay.dat cprog letter reports text. pay.dat XX.sh
reports
text
File System
As you can see both the options display the contents of the directory with several names per line. The difference between the two options is that in the -C option files are sorted alphabetically down the columns of names, whereas in names are sorted alphabetically across the lines. Another useful option for ls is -a. Actually ls has been lying to you all along, not really listing the names of all the files in the directory. By default ls, will not display any files in the named directory beginning with a. (dot). If you want the ls to include all the files in your current directory (/usr/ravi) including those that begin with a. (dot), type ls-a as shown: $ ls -a account cprog leter reports $ Here, you can see several files that were not shown before. Most system maintains several hidden files depending upon the application and software installed. You can also create your own hidden files by having the first character of the filename a special meaning and are explained below: The . (dot) stands for the current directory and the ..(dot-dot) is the parent directory. Actually, these are real entries in each directory, because each directory includes a pointer for finding itself and its parent directory. Note : we can also combine two or more options as shown : $ ls -ac The -F option allows you to identify the type of each file. It adds a /(slash) to the end to each directory and *(star) to the name of each executable file. Note these extra characters are just meant to identify the different types and is not part of the filename. $ ls -CF accounts/ cprog/ letter* pay.dat reports/ text -X option the
Probably, the most informative ls option is -l, which displays the contents of the directory in long format, giving the file type, permissions, number of links, owner, group, size in bytes and the time of last modification for each file. The following number shows the length of the file or directory in bytes. Then it gives the month, day & time when the file was last modified. shows the name of the directory file. Finally, the last column
File System The figure given below identifies each column in the rows of output from ls-l command No. of links 3 owner name ravi d d For example if you run the ls -l command while in /usr/ravi $ ls -l total 30 $ The first line of the output (total 30) shows the amount of disk space the directory occupies, measured in blocks. Each of the remaining lines gives information about the files. The first character in each line (d, -, b, or c) tells you the type of file. d b c = = = = directory ordinary disk file block special file character special file group name size 96 rwxrwxrwx time/date last modified Oct 3 13 : 25 filename cprog.
----> permissions
Using this key, which are either letters or hyphens identify who has permission to read, write or execute the file or directory. The following number is the link count. For a file, this equals the number of users linked to that file. For a directory, this number shows the number of directories immediately under it plus two (for the directory itself & its parent directory) Next the login name of the file owner appears (here it is ravi), followed by the group name of the file or directory (project).
File System Again the argument dirname may be either the absolute or relative pathname. The paranthesis (....) indicates that you may specify more than one directory name on a single command line. For example, you can create sub-directories 'prog' & 'data' in your own home directory as shown: $ mkdir prog data $ ls -l Note: That if the directory name already exists, the mkdir command aborts the process and does not overwrite the existing directory. when trying to create an ordinary file with the same name. Command Syntax Options Description : : : : mkdir mkdir directory name..... None 'mkdir' creates a new directory (sub-directory) Removing directories with rmdir. This is in contrast with an ordinary file where it is possible to overwrite an existing ordinary file
File System Note: provides shorthand for the parent directory (i.e. one directory above the current directory) Thus, if you just type in cd followed by (RETURN) you will come back to your home directory, immaterial of where you were previously positioned in the file system . Command Syntax Options : : : cd cd directoryname None cd changes your position in the file directory to the directory specified. system from the current
Description :
The opcodes argument represent the operation to be performed by chmod. + - Add the specified permission(s) to the existing permissions. - - Remove the indicated permission(s) from the existing permissions. = - Assign the indicated permission absolutely. The permission stands for access modes. r - read permission w - write permission x - execute permission For every file and directory in the file system, there are three classes of users who may access them. owner The owner is the usually the system user who or group initially created the file directory. The owner has full control over restricting or permitting the same department like sales, these users
access to the file at any time. The letter u, denotes an owner. Several users, say persons working in may access the training etc., can be combined into a user group, so that are given. The letter g, denotes a group.
10
File System others The other category refers to all the other users of the system,
denoted by the letter o. Each of these user classes has its own independent access rights. That is, you can assign different permissions or the same permissions to each of these user classes. These access modes specify, what type of rights (read, write or execute), can each class of user (owner, group, other), have to a file or directory. The types of permissions are read A user who has read permission to a file can examine the contents of that file. Thus, the file may be displayed on a terminal, copied, compiled etc., if a user has read access to it. A user who has read permission to a directory can list the contents (with the ls command), of that directory. He can also check the contents of each file in that directory, provided he has read permission to that file. write - A user who has write permission for a file can that file. modify the contents of The file may be altered or deleted. A user who has write
permission for a directory can change the contents of that directory, that is he can create or delete files. Whether the user can alter a file in the directory, depends on the write permission of the file itself. execute -The execute permission allows the execution of a Note: file as a program. all files are not executable such as a data file or program text.
Generally an ordinary file (created through an editor etc.), is not given execute permission when it is created. Execute permission for a directory file is often called the search permission, which is a better term since directories may be searched but not executed. The user who has execute permission to a directory, can transfer control to it with the cd command, and thus, can also access the files in it. Hence, even it you have read or write permission to a file in a directory, these operations cannot be performed unless you have execute permission to that directory. Thus, taking into consideration the three types of permissions for each of the three types of users, we have a total of nine sets of combinations for these permissions. That is, read, write and execute permissions for the owner, group and other categories. rwx | rwx | rwx |
11
File System owner group other As shown in the figure above, the leftmost set of three letters refers to the permission set for the owner. The set in the middle is for the group and the last set for the others. Permission for any file or directory can be seen in the long listing of a directory (ls -l), as the first field, immediately after the file type. For example: -rw-r--r-The above mentioned permission set indicates that, the file is an ordinary file, with the owner having read and write access to it, the group having only read access and others having only read access. This is the default permission set assigned to any text file. The default permissions assigned to a directory is: drwxr-xr-x This means that the user can list the contents, create and delete files and have access to i.e. search the directory. Whereas the group and others can only list the contents and search in the directory, but they cannot create or delete files in the directory. For example: $ chmod u+x file1 This command grants execute permission to the user or owner. $ chmod a+x file1 Assigns execute permission for all the classes of users. $ chmod +x file1 Performs the same function as the one in the previous example. Permissions can also be changed by using the ocatal numbers 1 2 and 4. The octal weightage given to each permission is. 1 - read 2 - write 4 - execute Hence, a total of eight combinations are possible, which are. 0 1 2 3 4 5 6 7 For example: no permissions read only write only read and write execute only read and execute write and execute read, write and execute (all permissions).
12
File System $ chmod 631 file1 This command means, assign write and execute permission to user, read and write permission to group and only read permission to others for the file called file.
Another variation to the who command is 'who am i' which prints only information
At first sight using this command may seem like a dumb thing since you already know who you are. But suppose a person has left the terminal without logging off then this command can be used to find out who was previously using the terminal. Also, it may be used to find the terminal identity, which will be used with several other commands. Another simple command is cal. This command prints the calendar for a given month within a year or for a complete year. The syntax is Cal [month] year The month can be any number between 1 and 12. Year can be any number between 1 and 9999. Thus $ Cal 1990 Displays the entire calendar for the year 1990.
13
File System $ Cal 9 1990 displays the calendar for september 1990. A note of caution, since cal command accepts year from 1 to 9999, The command $ Cal 9 90 refers to September 90 AD not September 1990. (Try out this command for the year 1752. Do you notice any peculiarity?)
14