Skip to content

directory structures and management

πŸ“‚ 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:

OperationDescription
Create Directorymkdir() β€” makes a new directory.
Delete Directoryrmdir() β€” removes a directory (if empty).
Open DirectoryOpens a directory to access its contents.
Read DirectoryReads entries in the directory (readdir()).
Rename DirectoryChanges the name of a directory.
TraverseNavigates through the hierarchy.
List ContentsLists 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

StructureHierarchySharing AllowedCycles PossibleUse 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