Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 66 ยท Files and Storage

Directory Structures and Path Names

Compare single-level, two-level, tree, acyclic-graph and general-graph directories, learn absolute and relative paths, links and mounting, and resolve paths in Python.

A disk can hold millions of files. Without organization you could never find anything, and two files could never share a name. A directory solves this: it is a special file whose contents are a table of names, each pointing at a file (or at another directory). The way directories are arranged is the directory structure, and it decides how easy it is to name, share, group and protect files.

This lesson walks through the five classic structures, from the simplest to the most flexible, then shows how a path like /home/ana/docs/a.txt is resolved one step at a time.

What a directory has to support

Whatever the structure, a directory must let you:

  • search for a file by name,
  • create and delete entries,
  • list the entries,
  • rename a file,
  • traverse the whole structure (for backups and searches).

The five structures

1. Single-level directory

Every file on the volume lives in one directory. It is trivial to implement, but two users cannot both have notes.txt, and with thousands of files the list becomes unmanageable. Early floppy-disk systems worked this way.

2. Two-level directory

A master file directory has one entry per user, and each user has a private user file directory. Different users can reuse names, and searches stay short. The weakness is that users are isolated: sharing a file requires a special rule, and a user cannot group their own files.

3. Tree-structured directory

Directories can contain directories, forming a tree with a single root. Every file has a unique path from the root. Users create subdirectories to group related files, and each process has a current (working) directory. This is what almost every modern OS uses. Deleting a non-empty directory needs a policy: refuse (rmdir) or delete the whole subtree (rm -r).

4. Acyclic-graph directory

A tree forces each file to have one parent. If two projects need the same file, you want two names for one file without copying. An acyclic graph allows that sharing but forbids cycles. Two mechanisms provide it:

  • A hard link is a second directory entry pointing at the same inode. The file is freed only when its link count drops to zero.
  • A symbolic (soft) link is a tiny file that stores a path. It can dangle if the target disappears, and it can point to directories or to other file systems.

5. General-graph directory

If links may point anywhere, cycles appear (a directory that contains a link back to its own ancestor). Cycles make traversal loop forever and confuse reference counting: a cycle's link counts never reach zero even when nothing outside can reach it. Systems avoid this by forbidding hard links to directories, limiting link-following depth, or running a garbage collector.

flowchart TD
    R["/"] --> H["home"]
    R --> E["etc"]
    H --> A["ana"]
    H --> B["bo"]
    A --> D["docs"]
    D --> F["plan.txt"]
    B --> S["shared"]
    S -.->|"hard link"| F

A tree plus one extra link: plan.txt now has two names, and the structure is an acyclic graph.

Path names

A path names a file by the directories you pass through.

  • An absolute path starts at the root: /home/ana/docs/plan.txt (Windows: C:\Users\ana\docs\plan.txt). It means the same thing everywhere.
  • A relative path starts at the current working directory: docs/plan.txt. The special names . (this directory) and .. (the parent) help: ../bo/shared/plan.txt.

Resolving a path is a loop: begin at the root (or the working directory), look up each component in the current directory's table, and move to what it points at. If a component is a symbolic link, substitute its text and continue. If a component is missing or lacks execute (search) permission, the lookup fails.

Python Resolving paths in a toy file system

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The symbolic-link case shows why ls on a path can print a different place from where you typed: the OS replaced latest with its target while walking.

The next demo makes hard-link counting visible. Deleting a name decrements the count. The data is released only when the count hits zero.

Python Hard links and the link count

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Mounting

Several storage devices can share one tree. Mounting attaches the root of another file system at an empty directory in the existing tree, called the mount point. After mount /dev/sdb1 /mnt/usb, a path like /mnt/usb/photo.jpg crosses from the main volume into the USB volume without the program noticing. Windows instead gives each volume a drive letter. Unmounting flushes pending writes so the device can be removed safely.

Choosing a structure

Structure Sharing Naming Used today?
Single-level none one name space, collisions No
Two-level none (needs special rule) per-user Rarely
Tree by path only unique path Core of every OS
Acyclic graph links many names per file Yes, via links
General graph unrestricted cycles possible Avoided

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main limitation of a single-level directory?

    With one directory for everything, two users cannot reuse a name and large collections become unmanageable.
  2. 2. What makes a directory structure an acyclic graph rather than a plain tree?

    A tree gives each file one parent. Links allow sharing while cycles remain forbidden.
  3. 3. A file has two hard links and one is deleted. What happens to the data?

    Data is released only when the link count reaches zero and no process has the file open.
  4. 4. Why are cycles a problem in a general-graph directory?

    A cycle keeps itself alive in link counts and makes naive searches recurse endlessly.

Summary

  • A directory maps names to files or other directories; its structure controls naming, sharing and grouping.
  • Single-level and two-level directories are simple but limited. Tree directories are the standard. Acyclic graphs add sharing through hard and symbolic links; general graphs allow cycles and need extra care.
  • Absolute paths start at the root, relative paths at the working directory; . and .. navigate.
  • Hard links share an inode and use a link count; symbolic links store a path and can dangle.
  • Mounting grafts another file system into the tree at a mount point.

Next: File Allocation Methods