0% found this document useful (0 votes)
126 views14 pages

Chapter 2: File System

The document discusses the file system in UNIX operating systems. It describes the three types of files - ordinary files which store user data, directory files which contain information about other files and subdirectories, and special files which reference physical devices. It also discusses concepts like filenames, file paths, working directories, and common file system commands like pwd to print the current working directory. The hierarchical structure of the UNIX file system is presented, with the root directory at the top of the tree containing standard subdirectories like bin, dev, etc, and user home directories under usr.

Uploaded by

Mahesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views14 pages

Chapter 2: File System

The document discusses the file system in UNIX operating systems. It describes the three types of files - ordinary files which store user data, directory files which contain information about other files and subdirectories, and special files which reference physical devices. It also discusses concepts like filenames, file paths, working directories, and common file system commands like pwd to print the current working directory. The hierarchical structure of the UNIX file system is presented, with the root directory at the top of the tree containing standard subdirectories like bin, dev, etc, and user home directories under usr.

Uploaded by

Mahesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

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.

2.1 TYPES OF FILES


All UNIX systems recognize three different types of files - Ordinary, Directory and Special files. It is quite unlike the conventional system where files are classified as sequential, indexed sequential or random. Ordinary Files :- These files are used to store data. They are generally created by editors, wordprocesors etc., They may be text files or program source code files or even an executable file. These files are known as 'flat files' as they have no structure. The user can impose any structure he desires be it sequential or indexed sequential. Ordinary files are completely under the user's control. Directory Files :-A directory file contains information about other files and subdirectories grouped under it. The directory file can be viewed as a table, where each row contains the name of the file and an address called the inode number. Ex: inode no. 347 453 319 filename. text.dat fib.c order.

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

2.2 FILES AND FILENAMES


A file is a collection of information that Files is stored on a secondary storage medium like magnetic tape or disk. are discrete entities in the UNIX file

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

2.3 FILE and DIRECTORY CONCEPT IN UNIX.


File is the fundamental unit that UNIX uses to store information. Everything in the UNIX system is a file. Though it appears to be an oversimplification of the fact, it has been the key design criteria in the development of the UNIX system. UNIX files can store anything that you can represent as a stream of bytes. They can store text such as book manuscripts; programs both source code as well as executable code; databases; bitmaps that may contain a screen image or picture, and so on. UNIX system considers any file to be a simple, undifferential sequence of bytes. It is upto the user or application program to impose any additional structure on the contents of a file to give it more meaning.

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

2.4 WORKING DIRECTORY


Whenever you log into the system, you log into a particular directory where you begin to work. This directory is known as the home directory. Every user has a unique home directory of his own. This will be assigned to the user when the system administrator creates an account for the user. You can freely create files and subdirectories in this directory and the UNIX system and other user wont touch them. The directory in which you are working at any time is known as your working directory or current directory. Your current directory is not fixed, unlike your home directory that is always fixed. You can change your current directory by means of the change directory (cd) command. Obviously, when you log into the system, your working directory will be your home directory.

2.5 PATH NAMES


As explained earlier, in the UNIX file system structure there are many levels in its hierarchy, with the root at the top. All files and directories are said to be descendants of the root. The root has a special status because you can find all the files in the system by starting at the root directory. Giving its path name may uniquely identify each file in the UNIX file system. A pathname to a file is a text string that identifies a file by specifying a path through the directory structure. Thus, the path-name consists of a sequence of component By convention it consists of (slash) character the system and names that lie along the path to the individual files. individual directory names separated by slashes. If you construct a path name beginning with a / understands that the pathname starts at the root directory. The path name that traces a complete path starting from the root directory traversing each successive intermediate directory that leads to the file is known as the full pathname or absolute pathname. The last component in the pathname is the target file, which is referenced. For example: /USR/FRANK/CPROG/PRIME.C Is the absolute pathname for the target files prime. C. The first slash stands for the root and the other slashes serve to separate each directory. A path name need not always have to start from the root but can be designated relative to the current working directory of the user. current directory. This is known as the relative pathname. Thus, the user can omit the initial slash and the directory name upto the

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

2.6 FILE SYSTEM RELATED COMMANDS


Now that we have some basic idea about the file system, we should know about the command that helps us to work effectively within the file system. These are the commands that help you to create, copy or rename files and view them.

2.6.1 Knowing where you are


The pwd (print working directory) command prints the name of the directory in which you are currently working. For Example: if your login name is ravi and you execute the pwd command, as soon as you login, then the UNIX system responds as follows : $pwd /usr/ravi $ This is your HOME directory where you will be positioned immediately when you log into the system. Note that pwd command provides the complete pathname of the current directory. This command will be quite useful when you are not sure where you are positioned and you need this information to get to another directory. Command Syntax : : pwd pwd Prints the file pathname of the directory in which you are currently working. Options Note : : None. In all the following examples we will assume that you are positioned at /usr/ravi

Description :

File System

2.7 LIST DIRECTORY CONTENTS WITH LS.


The ls command lists the names of all the files and sub-directories in a specified directory. If you do not specify a directory, 'ls' lists the names of files and directories in your current directory. If you want to list all the files in your current directory i.e./usr/ravi then give: $ ls account cprog letter note report pay.dat

$ 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.

File type --->

----> 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).

2.8 CREATING DIRECTORIES WITH MKDIR


The mkdir (make directory) command allows you to create one or more directories. It is recommended to use sub-directories in your home directory according to a logical and meaningful scheme that will facilitate the retrieval of information from your files. It is a good practice to put all the files pertaining to one subject together in a directory, so that you can easily find them later. For example: You could keep all the source files in one sub-directory and the data files in another. The command line format for making directories is as follows: $ mkdir dirname......

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

2.9 CHANGING DIRECTORIES WITH CD


When you first log in on the UNIX system, you are placed in your home directory. As long as you are working in it, it is also your current working directory. However, by the use of the cd, (change directory) command you can position yourself in some other directory, as well The syntax of the command is as follows: cd new dirctory-pathname This pathname can be any valid pathname and you can give either the relative or full pathname. For example: $ pwd /usr/ravi $ cd/etc $ pwd /etc Let us now see how to use relative pathnames : $ pwd /usr/ravi $ cd ../raju $ pwd /usr/raju $ cd $ pwd /usr/ravi

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 :

2.9.1 Changing file access permissions:


The chmod (change mode) command allows you to alter the permission modes of one or more files or directories. The access mode of any file or directory can be changed only by the owner or by the superuser. The command line syntax for chmod is $ chmod [who] opcode permission file/directory name The who argument defines the user class. It may be any one of the following: u g o a user (owner) group others all (owner, group and others)

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.

file(s) or directories to which the group permissions

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.

2.10 USING SOME SIMPLE COMMANDS


Lets start with a very simple command. Date, the one, which we have already seen. Type in date at the shell prompt and then press RETURN to indicate that the command line is complete. The command prints the day of the week, month, day, time and year as shown below: $ date Tue Aug 20 14:40:35 Ist 1990. It displays the login-ids, the Another simple command is who. This command is used to get information about all users who are logged in currently into the system. in, as shown below: $ who root rair Suresh about you. $ who am i rair tty02 Agu 20 14:30 Ist console tty02 tty07 Aug 20 Aug 20 Aug 20 13:50 14:30 14:15 Ist Ist Ist system's name for the terminal being used, day and time when the users had logged

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

You might also like