Tutorials›Operating Systems
Core CS

Directory Structures

Directory structures in operating systems organize how files are stored, retrieved, and managed on disk.

<h3>1. Single-Level Directory</h3><h2><!--StartFragment--><p>A Single-Level Directory is the simplest directory design where a single master directory hosts all files created by all users.</p><ul><li><p><b>Key Points:</b></p><ul><li><p>Only one directory exists in the entire file system.</p></li><li><p>Every file must have a globally unique name across the system.</p></li><li><p>Files sit alongside each other with no subdirectories.</p></li></ul></li><li><p><b>Significance:</b> It was the foundational model used in early single-user operating systems (such as early CP/M or simple microcomputer systems) where storage capacity and file counts were minimal.</p></li><li><p><b>Advantages:</b></p><ul><li><p>Extremely simple to design, implement, and maintain.</p></li><li><p>File searching is fast when the total number of files is small.</p></li><li><p>Minimal metadata overhead required by the OS.</p></li></ul></li><li><p><b>Disadvantages:</b></p><ul><li><p><b>Name collisions:</b> Two users cannot create a file with the same name (e.g., <code data-path-to-node="4,3,1,0,0" data-index-in-node="74">test.txt</code>).</p></li><li><p><b>No grouping:</b> Difficult to organize related files by project, type, or user.</p></li><li><p><b>Lack of privacy:</b> Not scalable or suitable for multi-user systems.</p></li></ul></li></ul></h2><h3>2. Tree-Structured Directory</h3><h2><p>A Tree-Structured Directory organizes files and folders hierarchically, starting from a single root directory and branching into subdirectories, forming an inverted tree.</p><ul><li><p><b>Key Points:</b></p><ul><li><p>Has a root directory (<code data-path-to-node="8,0,1,0,0" data-index-in-node="22">/</code> or <code data-path-to-node="8,0,1,0,0" data-index-in-node="27">C:\</code>) containing files and subdirectories.</p></li><li><p>Each file or directory has exactly one parent directory.</p></li><li><p>Files can be referenced via absolute paths (from root) or relative paths (from current working directory).</p></li></ul></li><li><p><b>Significance:</b> It is the standard directory model adopted by modern desktop and server OS file systems (like Linux, Windows, and macOS) to support multi-user isolation and project-based grouping.</p></li><li><p><b>Advantages:</b></p><ul><li><p><b>No naming collisions:</b> Files in different directories can share the same name (e.g., <code data-path-to-node="8,2,1,0,0" data-index-in-node="84">/user1/doc.txt</code> and <code data-path-to-node="8,2,1,0,0" data-index-in-node="103">/user2/doc.txt</code>).</p></li><li><p><b>Logical organization:</b> Users can nest subdirectories to categorize files systematically.</p></li><li><p><b>Easy access control:</b> Permissions can be applied at directory boundaries and inherited down the tree.</p></li></ul></li><li><p><b>Disadvantages:</b></p><ul><li><p><b>No direct sharing:</b> A file cannot naturally exist simultaneously in two different folders without duplicating the data.</p></li><li><p><b>Traversal overhead:</b> Accessing deeply nested files requires traversing multiple directory blocks, increasing disk I/O.</p></li></ul></li></ul></h2><h3>3. Acyclic-Graph Directory</h3><h2><p>An Acyclic-Graph Directory extends the tree structure by allowing directories or files to be shared among different directories via links (aliases/shortcuts), strictly prohibiting closed loops or cycles.</p><ul><li><p><b>Key Points:</b></p><ul><li><p>A file or folder can have multiple parents (indegree &gt; 1).</p></li><li><p>Direct file sharing is supported without copying the underlying data blocks.</p></li><li><p>Directed graphs are maintained such that no path leads back to the originating directory.</p></li></ul></li><li><p><b>Significance:</b> It provides a native mechanism for collaboration and joint project work in multi-user systems (e.g., UNIX hard links and symbolic links) while preventing traversal deadlock.</p></li><li><p><b>Advantages:</b></p><ul><li><p><b>True file sharing:</b> Changes made to a shared file by one user are immediately visible to others.</p></li><li><p><b>Storage efficiency:</b> Saves disk space by referencing the same physical blocks rather than creating redundant copies.</p></li><li><p><b>Flexible workflows:</b> Allows shared libraries or source code to reside in multiple user environments seamlessly.</p></li></ul></li><li><p><b>Disadvantages:</b></p><ul><li><p><b>Dangling pointers:</b> Deleting a file from one path can leave broken links in other paths unless reference counters are maintained.</p></li><li><p><b>Complex deletion logic:</b> If reference counts are used, the physical file is deleted only when the count reaches zero.</p></li><li><p><b>Cycle prevention overhead:</b> The OS must run cycle-checking algorithms every time a new link is created.</p></li></ul></li></ul></h2><h3>4. General Graph Directory</h3><h2><p>A General Graph Directory allows arbitrary links between directories and files, including cycles (a path of links can loop back to an ancestor directory).</p><ul><li><p><b>Key Points:</b></p><ul><li><p>Cycles and loops are permitted in the directory graph.</p></li><li><p>A directory can be an ancestor and a descendant of another directory simultaneously.</p></li><li><p>Requires explicit cycle detection algorithms during search and traversal.</p></li></ul></li><li><p><b>Significance:</b> Arises when an OS allows soft links/shortcuts to arbitrary directories without restricting cycle formation, prioritizing link flexibility over strict structural rules.</p></li><li><p><b>Advantages:</b></p><ul><li><p><b>Maximum flexibility:</b> Complete freedom to link files and directories without graph topology restrictions.</p></li><li><p><b>Simple link creation:</b> The file system does not need to validate every new link for cycle creation at link time.</p></li></ul></li><li><p><b>Disadvantages:</b></p><ul><li><p><b>Infinite loop risk:</b> Search, backup, or disk-usage tools can loop infinitely unless they track visited nodes.</p></li><li><p><b>Garbage collection overhead:</b> Standard reference counting fails when cycles occur (isolated loops have non-zero counts but are unreachable), requiring expensive mark-and-sweep garbage collection.</p></li><li><p><b>Complex file management:</b> Determining true file size, total directory contents, or safe deletion requires complex operating system logic.</p></li></ul></li></ul></h2>