100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What are Directory Structure Types in an OS?

Single-level, two-level, tree, and acyclic-graph directory structures compared, with OS interview questions and examples.

mediumQ111 of 224 in Operating Systems Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

Directory structure types describe how a file system organizes the mapping from file names to files, and the main forms are single-level, two-level, tree-structured, and acyclic-graph directories, each offering progressively more flexibility for namespace organization and sharing at the cost of added complexity.

A single-level directory places every file in one flat namespace, which is simple but forces every file name in the whole system to be unique and offers no grouping. A two-level directory gives each user their own directory, solving the naming-collision problem between users but still not allowing users to organize their own files into subgroups. A tree-structured directory lets any directory contain subdirectories to arbitrary depth, which is what almost every modern OS uses, letting users organize files hierarchically and reuse names in different branches, though a file still exists at exactly one location. An acyclic-graph directory extends the tree to allow shared subdirectories or files reachable via multiple paths — implemented with hard or symbolic links — which supports sharing without duplication but requires care to avoid cycles, since a true cycle would break traversal and reference counting. Most production file systems are trees for user-facing directories with symbolic and hard links layered on top to approximate an acyclic graph where sharing is needed.

  • Explains why every OS you have used organizes files hierarchically
  • Clarifies the difference between hard links and symbolic links conceptually
  • Shows the naming-collision problem that motivated multi-level directories
  • Connects directory structure to file sharing and reference counting

AI Mentor Explanation

A single-level directory is like one giant kit bag shared by an entire club where every player’s gear must have a unique name tag to avoid mix-ups. A two-level directory is like giving each player their own kit bag, so two players can both own a bat named the same without collision, but neither bag can be organized further. A tree-structured directory is like each player’s bag having labeled compartments for pads, gloves, and spikes, nested as deep as needed. An acyclic-graph directory is like a shared team first-aid kit reachable from every player’s bag without being duplicated in each one.

Step-by-Step Explanation

  1. Step 1

    Single-level

    One flat namespace for all files; every file name in the system must be globally unique.

  2. Step 2

    Two-level

    Each user gets a private directory, solving cross-user naming collisions but still with no internal grouping.

  3. Step 3

    Tree-structured

    Directories can nest subdirectories to arbitrary depth; the standard model in nearly all modern operating systems.

  4. Step 4

    Acyclic-graph

    Files or subdirectories can be reachable via multiple paths through links, enabling sharing without duplication, while cycles are avoided.

What Interviewer Expects

  • Ability to name and order single-level, two-level, tree, and acyclic-graph structures
  • Understanding of why two-level directories solve naming collisions
  • Knowledge that trees are what real operating systems use for the user-visible namespace
  • Awareness that hard/symbolic links approximate an acyclic graph and why cycles are dangerous

Common Mistakes

  • Saying a tree-structured directory allows a file to exist at multiple locations (that is acyclic-graph)
  • Confusing hard links with symbolic links when explaining sharing
  • Forgetting that single-level directories force globally unique file names
  • Not mentioning the cycle-avoidance problem in acyclic-graph directories

Best Answer (HR Friendly)

Directory structure types describe how an operating system organizes where files live and how they are named. It goes from a single flat list where every name has to be unique, to per-user folders, to the nested folder-within-folder tree structure we all use daily, and finally to structures that let the same file be reachable from more than one folder through links, which is how sharing works without copying files.

Code Example

Walking a tree-structured directory recursively
#include <dirent.h>
#include <stdio.h>

void walk_directory(const char *path, int depth) {
    DIR *dir = opendir(path);
    struct dirent *entry;
    if (!dir) return;

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_name[0] == '.') continue;   /* skip . and .. */
        for (int i = 0; i < depth; i++) printf("  ");
        printf("%s\n", entry->d_name);

        if (entry->d_type == DT_DIR) {
            char child[512];
            snprintf(child, sizeof(child), "%s/%s", path, entry->d_name);
            walk_directory(child, depth + 1);    /* recurse into subdirectory */
        }
    }
    closedir(dir);
}

Follow-up Questions

  • What is the difference between a hard link and a symbolic link?
  • Why do most operating systems avoid true cyclic directory graphs?
  • How does reference counting work for a file with multiple hard links?
  • How does a two-level directory differ from a tree-structured one in practice?

MCQ Practice

1. Which directory structure requires every file name in the system to be globally unique?

A single-level directory has one flat namespace, so every file name across the whole system must be unique.

2. What problem does a two-level directory solve compared to single-level?

Giving each user a private directory means two users can reuse the same file name without colliding.

3. What distinguishes an acyclic-graph directory from a pure tree?

Acyclic-graph directories allow shared files or subdirectories to be reached through multiple paths via links, unlike a strict tree.

Flash Cards

Name the four directory structure types.Single-level, two-level, tree-structured, and acyclic-graph.

Which structure does nearly every modern OS use?Tree-structured directories, with links approximating an acyclic graph.

What problem does two-level solve over single-level?Cross-user file name collisions.

What risk must acyclic-graph directories avoid?True cycles, which break traversal and reference counting.

1 / 4

Continue Learning