OS Notes
Understanding directory structures — single-level, two-level, tree-structured, acyclic graph, and general graph directories with their advantages and disadvantages.
Introduction
Imagine having a thousand files with no folders — just a flat list of names. Finding anything would be a nightmare, and name conflicts would be constant (you cannot have two files called "report.txt"). Directory structures solve this by organizing files into a hierarchy, much like a physical filing cabinet has drawers, folders, and sub-folders.
A directory (or folder) is a special file that contains entries mapping filenames to their file information (inode numbers in Unix, MFT entries in NTFS). The structure of how directories relate to each other determines how easily users can organize, find, and share files.
Single-Level Directory
The simplest structure: all files in one directory. Every file must have a unique name.
Advantages: Simple to implement, fast file lookup (just scan one list) Disadvantages: Name conflicts when multiple users share the system, no organization, does not scale beyond a few dozen files.
Two-Level Directory
Each user gets their own directory. Solves the name conflict problem but provides no further organization within a user's space.
| ├── /user1/ | [file1.txt] [report.pdf] [data.csv] |
| ├── /user2/ | [file1.txt] [photo.jpg] [notes.txt] |
| └── /user3/ | [report.pdf] [code.c] [Makefile] |
Advantages: Isolates users, allows same filenames for different users Disadvantages: No sub-directory organization within user space, sharing files between users is difficult.
Tree-Structured Directory (Hierarchical)
The standard approach used by all modern OS. Directories can contain both files AND other directories, creating an arbitrary-depth hierarchy.
Advantages: Natural organization, unlimited depth, easy to manage permissions at directory level, efficient searching (navigate to relevant subtree). Disadvantages: A file can only exist in one directory (no sharing without links), deeper hierarchies mean longer paths.
Path Navigation
# Absolute path (from root)
/home/alice/documents/report.pdf
# Relative path (from current directory)
cd /home/alice
cat documents/report.pdf # Relative to /home/alice
# Special directories
. → Current directory
.. → Parent directory
~ → Home directory (shell expansion)Acyclic Graph Directory
Extends tree structure by allowing shared files/directories through links. A file can appear in multiple directories without being duplicated.
Types of Links
Hard link: Multiple directory entries point to the same inode. The file is deleted only when ALL links are removed.
Symbolic link (soft link): A special file containing the path to another file. Like a shortcut. If the target is deleted, the link becomes "dangling."
# Hard link
ln /home/alice/file.txt /home/bob/file.txt
# Both entries point to same inode — same physical data
# Symbolic link
ln -s /home/alice/file.txt /home/bob/link.txt
# link.txt contains the path "/home/alice/file.txt"Challenge: Deletion is complex. If you delete a shared file, do other links become invalid? Hard links solve this with reference counting. Symbolic links can dangle.
General Graph Directory
Allows cycles (directory A contains B which contains A). This is problematic because traversal algorithms can loop infinitely and garbage collection becomes necessary to reclaim space.
Most file systems PREVENT cycles through restrictions:
- Only allow symbolic links (not hard links) to directories
- Check for cycles when creating links
- Garbage collection to find unreachable files (if cycles exist)
Directory Operations
Common operations the OS provides:
- Create directory:
mkdir /home/alice/new_folder - Delete directory:
rmdir /home/alice/empty_folder - List contents:
ls /home/alice/ordir - Rename:
mv old_name new_name - Search:
find / -name "*.pdf" - Change current directory:
cd /home/alice
Real-World Analogy
Directory structures are like address systems. A single-level directory is a small village where everyone has a unique first name. A two-level directory is apartments with unit numbers (Building A, Unit 3). A tree-structured directory is a full mailing address (Country/State/City/Street/Building/Apartment). An acyclic graph is when the same person receives mail at two addresses (home and office). The hierarchy helps postal workers (the OS) efficiently route mail (find files) without checking every address in the country.
Key Takeaways
- Directory structures organize files hierarchically for easy management and retrieval
- Tree-structured (hierarchical) directories are the universal standard in modern OS
- Acyclic graphs extend trees with file sharing through hard and symbolic links
- Cycles are generally prevented because they complicate traversal and deletion
- Paths can be absolute (from root) or relative (from current directory)
- Hard links share the same inode; symbolic links store a path string
- Good directory organization improves both human usability and system performance
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Directory Structure.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, file, system, directory, structure
Related Operating Systems Topics