π What is a Directory?
A directory is a special file that contains information about other files and directories. Think of it as a table of contents for the file system.
Functions of a Directory:
- Organize files in a structured way.
- Store file attributes (name, type, size, location, creation date).
- Provide access control and navigation within the file system.
π§± Types of Directory Structures
πΈ 1. Single-Level Directory
- All files are in one directory.
- Easy to implement.
π Limitations:
- File name conflicts.
- Poor scalability.
π§ͺ Example:
/
|-- file1
|-- file2
|-- file3
πΈ 2. Two-Level Directory
- A separate directory for each user.
- Solves naming conflict between users.
π Limitations:
- Users canβt share files easily.
π§ͺ Example:
/
|-- user1/
| |-- file1
|-- user2/
| |-- file1
πΈ 3. Tree-Structured Directory
- Hierarchical structure with nested directories.
- Each directory can contain files and subdirectories.
β Advantages:
- Scalable, organized, supports grouping.
π§ͺ Example:
/
|-- home/
| |-- user/
| |-- docs/
| |-- report.txt
| |-- music/
|-- etc/
|-- bin/
πΈ 4. Acyclic Graph Directory
- Allows shared subdirectories/files via links (soft/hard links).
- Prevents duplication and saves space.
π§ͺ Example:
/home/user/docs/report.txt
/shared/docs/report.txt β points to same file
π Issues:
- Complex traversal.
- Needs care in deletion and backup.
πΈ 5. General Graph Directory
- Like acyclic graph, but allows cycles (e.g., symbolic links pointing back).
π Risks:
- Infinite loops during traversal.
- Must implement cycle detection.
βοΈ Directory Operations (Management)
The Operating System supports these directory operations:
Operation | Description |
---|---|
Create Directory | mkdir() β makes a new directory. |
Delete Directory | rmdir() β removes a directory (if empty). |
Open Directory | Opens a directory to access its contents. |
Read Directory | Reads entries in the directory (readdir() ). |
Rename Directory | Changes the name of a directory. |
Traverse | Navigates through the hierarchy. |
List Contents | Lists all files and subdirectories (ls , dir ). |
π Directory Protection
Directories also support access permissions, such as:
- Read (r) β view contents.
- Write (w) β add/delete/rename files.
- Execute (x) β enter the directory.
Unix-like systems use a combination of:
- Owner, Group, Others
- Mode bits (e.g.,
drwxr-xr--
)
π§ Behind the Scenes: Data Structures
The OS uses various internal structures like:
- Directory Tables: Store names and inode numbers.
- Inodes: Contain metadata and pointers to file blocks.
- File Allocation Table (FAT) or NTFS MFT: For locating files in Windows.
π Summary
Structure | Hierarchy | Sharing Allowed | Cycles Possible | Use Case |
---|---|---|---|---|
Single-Level | β | β | β | Simple systems, floppy disks |
Two-Level | βοΈ | β | β | Multi-user systems |
Tree | βοΈ | β | β | Unix/Linux systems |
Acyclic Graph | βοΈ | βοΈ | β | Shared files, links |
General Graph | βοΈ | βοΈ | βοΈ | Advanced linking systems |