You are on page 1of 47

DIRECTORY STRUCTURE

Introduction
 Several users create thousands and millions of files in a file system.
The Directory structure is used by the file system to correctly organize these
files. The directory structure has the entries of the all the files related to that
directory. A directory is considered as a file, that contains information about
the other files.

 It is a collection (listing) of related / connected files on the storage device


(disc). The directory structure, as well as the files, are stored on the disc.
The directory may store some or the entire file attributes. We always store
various files like songs, images, videos, etc. All these files are stored in
some directory.

 A directory stores, arranges, and segregates files and folders using a


hierarchical structure. It should be stable and scalable; it should not
fundamentally change, only be added to.
What is a File?
What is a File?
 A file is a collection of records. A block comprises one or more records that
are unique to a single entire file. Records can cross-block borders in a
Spanned organization. Records cannot cross block boundaries in an Un-
spanned organization.

 Data or applications can be stored in files. There are two types of files:
simple and complex. Both the operating system and the software that
creates the file decide the structure of the file. Files are kept in a file system,
which can be found on a disc, tape, or main memory.

 File entries or attributes include filename, file type, location, size,


protection/permission, timestamp, ownership, and directory information.

 File operations are Create, Write/append, Read, Seek, Delete, Truncate,


Open, close
Introduction
Various types of information is stored in a directory; these include
1. Name: - Name is the name of the directory, which is visible to the
user.
2. Type: The directory type; such as single-level directory, two-level
directory, tree-structured directory, and Acyclic graph directory.
3. Location: Location of the device where the header of a file is
located.
4. Size: - The number of words/blocks/bytes in the file.
5. Position: - The position of the next-read pointer and the next-write
pointer.
6. Protection: - Access control on the read/write/delete/execute.
7. Usage: - The time of creation, modification, and access, etc.
8. Mounting: - Whether the root of a file system is grafted into the
existing tree of other file systems.
Introduction
 To get the benefit of different file systems on the different operating
systems, a hard disk can be divided into the number of partitions
(volumes or mini disks) of different sizes. Each partition must have at
least one directory in which, all the files of the partition can be listed. A
directory entry is maintained for each file in the directory which stores
all the information related to that file.
Introduction
 A directory can be viewed as a file which contains the Meta data (a set of
data that describes and gives information about other data) of the bunch of
files.
Overview of Directory Structure
 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 of while maintaining a
directory are:
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.
Overview of Directory Structure
 A 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
searches for the file’s entry in the directory and when the match is
found, it obtains the file’s location.
Overview of Directory Structure
 File name: The name of the concerned file in the directory
 Type: The kind or category of the file
 Location Info: The location where the file is stored.
 Protection Info: Information whether the file can be accessed by the
other users in the system or not.
 Flag: The kind of directory entry
o D indicates that the file is a directory
o L indicates that the file is a link
o M indicates that the file is a mounted file system.

 The Misc info filed in the directory contains the miscellaneous


information about the owner of the file, the time of its creation, the time
at which the file was modified last.
Advantages of Directory Structure
 Logical Organization: Directory structures help to logically organize files
and directories in a hierarchical structure. This provides an easy way to
navigate and manage files, making it easier for users to access the data they
need.

 Increased Efficiency: Directory structures can increase the efficiency of the


file system by reducing the time required to search for files. This is because
directory structures are optimized for fast file access, allowing users to
quickly locate the file they need.

 Improved Security: Directory structures can provide better security for files
by allowing access to be restricted at the directory level. This helps to
prevent unauthorized access to sensitive data and ensures that important
files are protected.
Advantages of Directory Structure
 Facilitates Backup and Recovery: Directory structures make it easier
to backup and recover files in the event of a system failure or data loss.
By storing related files in the same directory, it is easier to locate and
backup all the files that need to be protected.

 Scalability: Directory structures are scalable, making it easy to add


new directories and files as needed. This helps to accommodate
growth in the system and makes it easier to manage large amounts of
data.
Types of Directory Structure
 There are various types of directory structure:
1. Single-Level Directory

2. Two-Level Directory

3. Tree-Structured Directory

4. Acyclic Graph Directory

5. General-Graph Directory
Single-Level Directory
 There is only one directory called the Root directory. There is one big list of
all the files on the disk. All the files created by several users are in one
directory. The users are not allowed to create subdirectories under the root
directory. The directory contains one entry per each file present on the file
system.
Single-Level Directory
Advantages of Single-Level Directory
1. Implementation of a single-level directory is easy.
2. If all the files have a small size, then the searching of the files will be
easy.
3. The operations such as searching, creation, deletion, is very simple
since we have only one directory.
Single-Level Directory
Disadvantages of Single-Level Directory
1. We cannot have two files with the same name
2. If the size of the directory is large the searching will take so much
time.
3. Similar type of files cannot be grouped together.
4. There is a possibility of collision because the two files cannot have the
same name.
5. The task of choosing the unique file name is a little bit complex and it
limits the number of files in the system because most of the Operating
Systems limit the number of characters used to construct the file
name.
6. Protection cannot be implemented for multiple users.
7. The single-level directory is not useful for multi-user systems.
Two-Level Directory
 There is a master node that has a separate directory for each user. For
each user, there is a different directory present at the second level,
containing group of user's file. The system doesn't let a user to enter in
the other user's directory without permission. It can be practically
thought of as a folder that contains many folders, each for a particular
user, and now each user can store files in the allocated directory just
like a single level directory.

 For every user, there is a separate directory. Each user has their
own user files directory (UFD). No user can enter into the other user’s
directory without permission. At the next level, every directory stores
the files just like a single-level directory. Although not very efficient, the
two-level directory is better than a single-level directory structure.
Two-Level Directory
 The UFDs have similar structures, but each lists only the files of a
single user. system’s master file directory (MFD) is searched whenever
a new user id is Correct.

 Files now have a path: /user1/directory-name. Different users can have


the same file name (/user2/me and /user3/me). Because only one
user's list needs to be searched, searching is more efficient. However,
there is currently no way to group a user's files.
Two-Level Directory
Characteristics of Two Level Directory System
 Each files has a path name as /User-name/directory-name/

 Different users can have the same file name.

 We cannot group files having the same name into a single directory for a specific
user.

 Searching becomes more efficient as only one user's list needs to be traversed.

 The same kind of files cannot be grouped into a single directory for a particular user.

 Every Operating System maintains a variable as PWD which contains the present
directory name (present user name) so that the searching can be done appropriately.
Two-Level Directory
Advantages of two-level directory
 Searching is very easy.
 There can be two files with the same name in two different user
directories. Since they are not in the same directory, the same name
can be used.
 Grouping is easier.
 A user cannot enter another user’s directory without permission.
 Implementation is easy.
Two-Level Directory
Disadvantages of two-level directory
 One user cannot share a file with another user.
 Even though it allows multiple users, still a user cannot keep two
same type files in a user directory.
 It does not allow users to create subdirectories.
 It is not scalable.
Tree-Structured / Hierarchical Directory
 There is a root directory at the peak. The root directory contains directories for each
user. The users can, however, create subdirectories inside their directory and also store
the files.

 In Tree structured directory system, any directory entry can either be a file or sub
directory. Tree structured directory system overcomes the drawbacks of two level
directory system. The similar kind of files can now be grouped in one directory.

 This is how things work on our PCs. We can store some files inside a folder and also
create multiple folders inside a folder.

 The root directory is highly secured, and only the system administrator can access it.
There can be subdirectories inside the user directories. Although the user can read the
data of root, he cannot however modify or write it and he cannot also access another
user's directory.
Tree-Structured Directory
Tree-Structured Directory
Tree-Structured / Hierarchical Directory
 It’s only the System Administrator who has full access to the root directory.

 Searching is more efficient in this directory structure. The concept of a


current working directory is used. The files are accessed by their location
using the path. There are two types of paths to locate the file in this directory
structure; an absolute path or a relative path.

Absolute Path
 Here, the path for the desired file is described by considering the root
directory as the base directory.

Relative Path
 Here, the path depends on the current working directory of the system; either
the user’s directory is considered as the base directory or the desired file
directory is considered as the base directory.
Tree-Structured / Hierarchical Directory
 In tree structured directory systems, the user is given the privilege to
create the files as well as directories.

 In a tree directory structure, every directory or file has only one parent
directory except the Root Directory. So, there is a total separation
between the users which provide complete naming freedom. If a user
wishes to access another users file, it has to go through two or more
directories.
Tree-Structured Directory
Advantages of tree-structured directory
 Highly scalable compared to the previous two types of directories.
 Allows subdirectories inside a directory.
 Searching is easy because we may utilize both absolute and relative
paths.
 Allows grouping.
 Segregation of important and unimportant files is easy.
 Collisions are less likely in the tree-structures directory
Tree-Structured Directory
Disadvantages of tree-structured directory
 As one user cannot enter another user’s directory, this restricts
sharing of files.
 Too many subdirectories may make the search complicated.
 Users cannot modify the root directory’s data.
 Each file does not fit into the hierarchal model. We have to save the
files into various directories.
 Because a file might be found in several folders, it is inefficient
Tree-Structured Directory
Permissions on the file and directory
 A tree structured directory system may consist of various levels
therefore there is a set of permissions assigned to each file and
directory.

 The permissions are R W X are reading, writing and the execution of


the files or directory. The permissions are assigned to three types of
users: owner, group and others.

 There is an identification bit which differentiate between directory and


file. For a directory, it is d and for a file, it is dot (.)
Acyclic Graph Directory
 None of the directories so far discussed provides the flexibility to
access a file from multiple directories, i.e., we cannot access a
particular file or subdirectory from two or more directories.

 In the acyclic-graph directory a file or a subdirectory can be accessed


from multiple directories. Hence files can be shared between
directories. It is designed in such a way that multiple directories point
to a particular directory or file with the help of links.

 A practical example of this is a doc file shared between two users. If


any of the users makes a change in the file, the change is reflected for
both the users.
Acyclic Graph Directory
Acyclic Graph Directory
 These kinds of directory graphs can be made using links or aliases. We
have numerous names for the same file, as well as multiple paths to it.
Links can either be symbolic (logical) or hard link (physical).

o (i) symbolic link or soft link (specify a file path: logical) and
o (ii) hard link (actual link to the same file on the disc from multiple
directories: physical).

 If a file gets deleted in acyclic graph structured directory system, then


1. In the case of soft link, the file just gets deleted and we are left with a
dangling pointer.

2. In the case of hard link, the actual file will be deleted only if all the
references to it gets deleted.
Acyclic Graph Directory

Acyclic Graph Directory
Advantages of acyclic- graph directory
 Allows sharing of files or subdirectories from more than one directory.
 Searching is very easy.
 Provides more flexibility to the users.

Disadvantages of acyclic-graph directory


 Harder to implement in comparison to the previous three.
 Since the files are accessed from multiple directories, deleting a file
may cause some errors if the user is not cautious.
 If the files are linked by a hard link, then it is necessary to delete all
the references to that file to permanently delete the file.
General-Graph Directory
 This is an extension to the acyclic-graph directory. In the general-
graph directory, there can be a cycle inside a directory.

 Cycles are allowed inside a directory structure where numerous


directories can be derived from more than one parent directory. When
general graph directories are allowed, commands like search a
directory and its subdirectories for something must be used with
caution. If cycles are allowed, the search is infinite.

 The biggest issue with this type of directory layout is figuring out how
much space the files and folders have used up.
General-Graph Directory
General-Graph Directory
General-Graph Directory
Advantages of General-graph directory
 Compared to the others, the General-Graph directory structure is more
flexible.
 Cycles are allowed in the directory for general-graphs.

Disadvantages of General-graph directory


 It costs more than alternative solutions.
 Garbage collection is an essential step here
Operations on Directory
1. Searching
 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.

2. Creating
 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.

3. Deleting
 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 represented by dots.
Operations on Directory
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 table space.

5. Renaming
 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.

6. Link
 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.

7. Unlink
 If the file is unlinked and is only present in one directory its directory entry is removed. If the file
appears in multiple directories, only the link is removed.
Directory Implementation
 There are various types of algorithms which can be used for directory
implementation. The selection of a suitable algorithm for directory
implementation is an essential task because it directly affects system
performance. The directory implementation algorithm depends upon
the data structure.

 Two types of algorithms are often implemented:


o Linear List

o Hash Table
Directory Implementation
Linear List:
 It is the most straightforward algorithm used for directory
implementation. All the files in a directory are kept like a singly linked
list. Every file comprises of a pointer to the data blocks that are
allocated to it and the next file in the directory. It requires a costly
search on large directories
Directory Implementation
Hash Table
 There are some disadvantages in singly linked implementation of
directories. To remove this drawback, another method called a hash table is
used. In this method, the hash table is used with the linked list.

 For every file in a directory, there is a key-value pair that is generated. It is


stored in the hash table when it is generated. With the help of the hash
function on the file name, we can determine the key and key points to the
respective file that are stored in a directory.

 In a linear list, we have to search the entire list, but in hash table the
searching is quite efficient. With the help of the key, we only have to check
the entries of the hash table, and when we get the entry, then by using the
value, we will fetch the corresponding file.
Directory Implementation
Summary
 A Directory is considered as a file that contains information about the
other files and directories. It 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.
Summary
 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.

You might also like