Directory Management
Directory management is the operating-system function responsible for creating, organizing, locating, accessing, and managing directories and the relationships between files and directories.
In simple words:
Directory management tells the OS how files and folders are organized and how their paths are resolved.
1. What Is a Directory?
A directory is a special structure used to organize files and other directories.
For example:
Home │ ├── Documents │ ├── notes.txt │ └── report.pdf │ ├── Pictures │ ├── photo1.jpg │ └── photo2.jpg │ └── Videos └── movie.mp4
Here:
Home Documents Pictures Videos
are directories, while:
notes.txt report.pdf photo1.jpg movie.mp4
are files.
2. Why Do We Need Directory Management?
Imagine having 100,000 files in one place:
file1 file2 file3 ... file100000
Finding and organizing files would be difficult.
Directories allow us to create a hierarchy:
University │ ├── CSE │ ├── Notes │ ├── Assignments │ └── Projects │ ├── ECE │ ├── Notes │ └── Projects │ └── Administration
So directory management provides:
- Organization
- Naming
- Searching
- File grouping
- Path resolution
- Access control
- File sharing
- Directory creation/deletion
3. Directory as a Mapping
Conceptually, a directory maps:
Filename → File information
For example:
Documents/ notes.txt → inode 105 report.pdf → inode 210 photo.jpg → inode 315
In a Unix/Linux-style filesystem:
Directory ↓ Filename ↓ Inode ↓ File metadata ↓ File data
This is an important relationship.
4. Directory Structure
The OS can organize directories in different ways.
Important directory structures are:
Directory Structures │ ├── Single-Level ├── Two-Level ├── Tree-Structured ├── Acyclic Graph └── General Graph
Let's understand each.
5. Single-Level Directory
The simplest structure.
All files are stored in one directory.
Root │ ├── file1.txt ├── file2.txt ├── photo.jpg ├── report.pdf └── data.csv
Advantages
- Very simple
- Easy to implement
- Easy to understand
Disadvantages
The biggest problem is name conflicts.
For example:
notes.txt notes.txt
Two users cannot easily have separate files with the same name.
Also, finding files becomes difficult when there are many files.
Example
Imagine:
Student 1 → assignment.txt Student 2 → assignment.txt
Both cannot have the same name in the same directory.
6. Two-Level Directory
Two-level directory structure creates a separate directory for each user.
Root │ ├── Uday │ ├── notes.txt │ └── project.py │ └── Rahul ├── notes.txt └── project.py
Now both users can have:
Uday/notes.txt Rahul/notes.txt
without conflict.
Advantages
- Avoids many filename conflicts
- Separate user directories
- Better organization than single-level
Disadvantages
- Sharing files between users can be difficult
- Limited directory hierarchy
7. Tree-Structured Directory
This is the most common conceptual directory structure.
Directories can contain:
- Files
- Subdirectories
Example:
Root │ ├── home │ ├── uday │ │ ├── Documents │ │ │ ├── notes.txt │ │ │ └── report.pdf │ │ │ │ │ ├── Downloads │ │ └── Pictures │ │ │ └── rahul │ └── Documents │ ├── etc ├── usr └── var
This forms a tree.
8. Root Directory
The top of a tree-structured directory system is called the root directory.
Linux:
/
Example:
/ ├── home ├── etc ├── usr ├── var └── tmp
Windows commonly starts with a volume/drive such as:
C:\
Example:
C:\ ├── Users ├── Windows └── Program Files
9. Parent and Child Directories
Suppose:
Documents └── College └── OS └── notes.txt
Then:
Documents → parent of College College → parent of OS OS → parent of notes.txt
And:
OS └── notes.txt
means:
OS = parent directory notes.txt = child entry
10. Absolute Path
An absolute path starts from the root of the filesystem hierarchy or, on Windows, from a drive/volume root.
Linux:
/home/uday/Documents/notes.txt
Windows:
C:\Users\Uday\Documents\notes.txt
It completely identifies the location.
11. Relative Path
A relative path starts from the current working directory.
Suppose current directory is:
/home/uday
Then:
Documents/notes.txt
is a relative path.
You don't need to write:
/home/uday/Documents/notes.txt
12. Special Directory Entries
Unix/Linux filesystems commonly provide:
. ..
.
Means:
Current directory
Example:
cd .
You stay in the same directory.
..
Means:
Parent directory
Example:
cd ..
Moves to the parent directory.
Example:
/home/uday/Documents
If you run:
cd ..
you move to:
/home/uday
13. Directory Operations
Operating systems provide several directory operations.
Directory Operations │ ├── Create ├── Delete ├── Open ├── Read/List ├── Search ├── Rename └── Traverse
14. Create a Directory
Example Linux command:
mkdir projects
Before:
home └── uday
After:
home └── uday └── projects
Conceptually, the OS:
- Creates directory structure
- Allocates required filesystem metadata
- Adds directory entry
- Updates filesystem metadata
15. Delete a Directory
Linux:
rmdir projects
Normally, rmdir removes an empty directory.
For a directory containing files, recursive removal may be used:
rm -r projects
This is powerful and should be used carefully.
16. List Directory Contents
Linux:
ls
Example:
Documents Downloads Pictures notes.txt
Detailed:
ls -l
Conceptually, the OS reads directory entries and displays their information.
17. Search in a Directory
Directory management also requires locating files.
For example:
/home/uday/Documents/report.pdf
The OS resolves:
/ ↓ home ↓ uday ↓ Documents ↓ report.pdf
This process is called path resolution.
18. Path Resolution
Suppose an application requests:
/home/uday/Documents/notes.txt
The filesystem resolves each component:
/ ↓ home ↓ uday ↓ Documents ↓ notes.txt
Conceptually:
Path ↓ Root ↓ Directory entry ↓ Next directory ↓ Next directory ↓ File
At each step, the OS/filesystem must locate the appropriate directory entry.
19. Directory Searching
Suppose a directory contains:
file1.txt file2.txt notes.txt report.pdf photo.jpg
The filesystem needs to find:
notes.txt
Simple implementations might use a linear search:
file1.txt → no file2.txt → no notes.txt → YES
For large directories, filesystems may use more efficient indexing/data structures.
Examples include:
- Hash-based structures
- Trees
- B-trees or related structures
- Filesystem-specific indexing
20. Tree-Structured Directory Example
Consider:
/ ├── home │ ├── uday │ │ ├── Documents │ │ │ ├── os.pdf │ │ │ └── cn.pdf │ │ │ │ │ ├── Downloads │ │ └── Projects │ │ └── website │ │ └── index.html │ │ │ └── rahul │ └── Documents │ ├── etc ├── usr └── var
Absolute path:
/home/uday/Projects/website/index.html
Relative to /home/uday:
Projects/website/index.html
21. Acyclic Graph Directory
A tree has one parent for each directory/file entry in the hierarchy.
But sometimes we want sharing.
An acyclic graph directory structure allows files or directories to be shared while avoiding cycles.
Example:
User A │ └── shared.txt ↑ │ User B ───┘
Both users can access the same underlying file.
Conceptually:
Directory A ───┐ ├──→ File X Directory B ───┘
There are multiple references to the same object.
22. Hard Links
In Unix/Linux, a hard link creates another directory entry referring to the same inode.
Conceptually:
Directory A │ └── file1.txt │ ↓ inode 500 │ ↓ data blocks Directory B │ └── file2.txt │ ↓ inode 500
Both names refer to the same underlying file object.
Therefore:
file1.txt file2.txt
can refer to the same data.
23. Symbolic Links
A symbolic link is a special file containing a path/reference to another file.
Example:
original.txt ↑ │ shortcut.txt
Conceptually:
shortcut.txt ↓ /home/uday/original.txt
Unlike a hard link, a symbolic link generally refers to a pathname, not directly to the same inode.
24. Hard Link vs Symbolic Link
| Hard Link | Symbolic Link |
|---|---|
| Refers to same inode | Refers to pathname |
| Same underlying file object | Separate link object |
| Usually cannot cross filesystems | Can generally cross filesystems |
| Target remains accessible through another hard link if one name is deleted | Can become dangling if target is removed |
| Commonly restricted for directories | Can point to directories |
Memory trick:
Hard link → same inode
Symbolic link → points to path
25. General Graph Directory
A general graph directory structure allows arbitrary links and therefore can contain cycles.
Example:
A ↓ B ↓ C ↓ A
Now:
A → B → C → A
This is a cycle.
The problem is that traversal could continue indefinitely.
A ↓ B ↓ C ↓ A ↓ B ↓ C ...
Therefore, systems that allow such structures need mechanisms to handle cycles.
26. Acyclic Graph vs General Graph
| Feature | Acyclic Graph | General Graph |
|---|---|---|
| Sharing | Yes | Yes |
| Cycles | No | Possible |
| Traversal | Easier | More complex |
| Cycle detection needed | Generally no | Yes |
| Example | Shared files/directories | Arbitrary links |
27. Directory Permissions
Directories also have permissions.
In Linux:
r = read w = write x = execute
But directory permissions have an important meaning.
Read (r)
Allows listing directory contents.
Write (w)
Allows creating/deleting/renaming entries, subject to other permission/security rules.
Execute (x)
Allows traversing/searching the directory.
This is extremely important.
28. Directory Permission Example
Suppose:
drwxr-x---
Breakdown:
Owner → rwx Group → r-x Others → ---
Owner can:
Read Write Traverse
Group can:
Read Traverse
Others have:
No access
29. Directory and File Are Different
A directory isn't simply an ordinary text file.
A directory is a filesystem-managed structure containing entries that associate names with filesystem objects.
Conceptually:
Directory │ ├── notes.txt → inode ├── report.pdf → inode ├── Documents → inode └── Pictures → inode
30. Current Working Directory
Every process generally has a current working directory used when resolving relative paths.
Example:
/home/uday/Documents
Then:
notes.txt
means:
/home/uday/Documents/notes.txt
Change directory:
cd /home/uday
Now:
Current directory = /home/uday
31. Directory Traversal
Traversal means moving through the directory hierarchy.
Example:
/ ↓ home ↓ uday ↓ Documents
Commands:
cd / cd home cd uday cd Documents
Or directly:
cd /home/uday/Documents
32. Directory Mount Points
In Linux, another filesystem can be mounted at a directory.
Example:
/ ├── home ├── etc └── mnt └── data
Suppose:
/dev/sdb1
is mounted at:
/mnt/data
Then:
/dev/sdb1 ↓ filesystem ↓ /mnt/data
The directory becomes the access point to that filesystem.
33. Directory Implementation
Internally, the filesystem must store directory information.
A simplified directory entry might contain:
+-----------------------------+ | Filename | +-----------------------------+ | Object/Inode reference | +-----------------------------+
Example:
Filename Inode -------------------- notes.txt 105 report.pdf 210 photo.jpg 315
The exact implementation differs between filesystems.
34. Directory Caching
Searching directories repeatedly can be expensive.
Operating systems/filesystems therefore use caching.
Conceptually:
Application ↓ Path lookup ↓ Directory cache / \ Hit Miss ↓ ↓ Fast Filesystem ↓ Storage
Linux, for example, uses mechanisms such as the dentry cache to speed up pathname lookup.
35. Directory Management and File Management
These are closely related.
| Directory Management | File Management |
|---|---|
| Manages directories | Manages files |
| Creates directories | Creates files |
| Deletes directories | Deletes files |
| Path resolution | File access |
| Directory hierarchy | File contents |
| Directory entries | File metadata |
| Traversal | Read/write |
They work together:
Directory ↓ Find file ↓ File management ↓ Read/write file
36. Directory Structure Comparison
| Structure | Organization | Main Advantage | Main Problem |
|---|---|---|---|
| Single-level | One directory | Simple | Name conflicts |
| Two-level | Directory per user | User separation | Limited sharing |
| Tree | Hierarchical | Excellent organization | More complex |
| Acyclic graph | Sharing | File/directory sharing | Link management |
| General graph | Arbitrary links | Flexible | Cycles |
37. Complete Directory Management Flow
Suppose you run:
cat /home/uday/Documents/os.txt
The conceptual process is:
Shell ↓ cat program ↓ open("/home/uday/Documents/os.txt") ↓ Kernel ↓ Path resolution ↓ / ↓ home ↓ uday ↓ Documents ↓ os.txt ↓ Filesystem metadata ↓ File data blocks ↓ read() ↓ Terminal
This demonstrates how directory management and file management work together.
🧠 Exam Revision
Definition
Directory management is an operating-system function that manages directories, directory structures, paths, naming, searching, traversal, and relationships between files and directories.
Main directory structures
1. Single-Level 2. Two-Level 3. Tree-Structured 4. Acyclic Graph 5. General Graph
Important operations
Create Delete Open Read/List Search Rename Traverse
Important concepts
Root Directory Parent Directory Child Directory Absolute Path Relative Path Current Working Directory Hard Link Symbolic Link Mount Point Directory Entry Path Resolution Directory Permissions
Most important diagram
ROOT │ ┌──────────┼──────────┐ ↓ ↓ ↓ home etc usr │ ↓ uday │ ┌─────┼─────┐ ↓ ↓ ↓ Documents Downloads Projects │ ├── notes.txt └── os.pdf
🧠 Memory trick
“Directory = Organize → Name → Locate → Traverse → Share → Protect.”
And remember the link concepts:
Hard Link → Same inode Symbolic Link → Path reference Acyclic Graph → Sharing without cycles General Graph → Cycles possible