Several users create thousands and millions of files in a file system. To organize these files properly file system uses directory structure. The directory structure has the entries of the all the files related to that directory. We can consider that a directory is a file, that contains information about the other files.
Here, we will discuss some common schemes defining the directory structure and will also discuss some common maintenance operations that can be performed on directory structure. So let’s start to explore directory structure.
Content: Directory Structure in OS
A File system contains thousands and millions of files, owned by several users. The directory structure organizes these files by keeping entries of all the related files. The file entries have information like file name, type, location, the mode in which the file can be accessed by other users in the system.
The two important features that must be taken care while maintaining a directory are given below.
1. A user must have freedom of giving the desired name to the file, without taking into consideration that the same file names are used by other users on the system.
2. A user must be able to share the files he creates and he must also be able to access the files, created by other users.
Directory structure provides both the above-discussed features. A directory always has information about the group of related files. Whenever a user or a process request for a file, the file system search for the file’s entry in the directory and when the match is found, it obtains the file’s location from there.
Let us now study the directory structure with the help of the figure above. The field File name, contains the name of the concerned file in the directory, Type field indicates the kind or category of the file, Location Info field indicates the location where the file is stored.
Protection Info field contains the information whether the file can be accessed by the other user in the system or not. Flag field contains the kind of directory entry like value D in Flag field indicates that the file is a directory, value L indicates that the file is a link, value M indicates that the file is a mounted file system.
Types of Directory Structures
In this section, we will be discussing the most common types of directories, their benefits and drawbacks.
1. Single-level directory structure
Single level directory structure has only one directory which is called the root directory. The users are not allowed to create subdirectories under the root directory. All the files created by the several users are present in the root directory only.
As you can see in the diagram below all the file F1, F2, F3, F4 created by the different users are present at the root directory.
There is one drawback of Single-level directory structure, a user cannot use the same file name used by another user in the system. Even if the file with the same name is created the old file will get destroyed first and replaced by the new file having the same name.
2. Two-level directory structure
In Two-level directory structure, the users create directory directly inside the root directory. But once a user creates such directory, further he cannot create any subdirectory inside that directory. Observe the figure below, 4 users have created their separate directory inside the root directory. But further, no subdirectory is created by the users.
3. Hierarchical Directory Structure
In Hierarchical directory structure, the users can create directories under the root directory and can also create sub-directories under this structure. As the user is free to create many sub-directories, it can create different sub-directories for different file types.
Here, the files are accessed by their location using the path. There are two types of paths to locate the file in this directory structure
- Absolute Path
Here, the path for the desired file is described by considering the root directory as the base directory.
- Relative Path
Here, either the user’s directory is considered as the base directory or the desired file directory is considered as the base directory.
4. Tree Directory Structure
In a tree directory structure, except root directory, every directory or file has only one parent directory. So, there is a total separation between the users which provide complete naming freedom. Here, if a user wishes to access another users file, it has to go through two or more directories.
The tree directory structure provides an asymmetric way for the user to access the shared files of a different user. For example, a user can access a file of its own user directory with a shorter path than the other user.
5. Acyclic-Graph Directory Structure
This problem can be solved by the acyclic-graph directory structure. As this directory structure allows a directory or a file to have many parent directories. So, a shared file in a directory can be pointed by the other user directories who have access to that shared file using the links.
In the diagram below you can see that the directory having file F7 and F8 have two parent directories.
Operations on Directory
As discussed above, a directory contains the entries of all the related files. For organizing the directory in the better way the user must be able to insert, delete, search, list the entries in the directory. Below we will be discussing the operation that can be performed on the directory.
A directory can be searched for a particular file or for another directory. It can also be searched to list all the files with the same name.
A new file can be created and inserted to the directory or new directory can be created keeping in mind that its name must be unique under that particular directory.
If a file is no longer needed by the user, it can be deleted from the directory. The entire directory can also be deleted if it is not needed. An empty directory can also be deleted. When a directory is empty it is resembled by dot and dotdot.
4. List a directory
List of all the files in the directory can be retrieved and also the contents of the directory entry, for each file in a list. To read the list of all the files in the directory, it must be opened and after reading the directory must be closed to free up the internal tablespace.
The name of the file or a directory represents the content it holds and its use. The file or directory can be renamed in case, the content inside or the use of file get changed. Renaming the file or directory also changes its position inside the directory.
The file can be allowed to appear in more than one directory. Here, the system call creates a link between the file and the name specified by the path where the file is to appear.
- Directory is also considered as a file that contains information about the other files and directories.
- Directory keeps the entry of a group of all the related files and also the information like file names, types, location, etc.
- The single-level directory has only one root directory and no user is allowed to create subdirectories inside the root directory.
- The two-level directory allows the user to create a directory inside the root directory but do not allow creating further subdirectories.
- Hierarchical directory goes beyond two-level directory structure. Here, a user is allowed to create many subdirectories.
- In the tree directory, each directory has only one parent directory except the root directory.
- An acyclic graph structure, a directory can have more than one parent directory.
- The directories can undergo many maintenance operations like create, delete, list, rename, link, unlink, etc.
The directory structure is simply a file that keeps complete information of other files or directories in the file system.