Course Info / Syllabus

Labs

Lecture Topics

Exam Reviews

Source Code & Other Resources


Operating Systems Concepts & Design

Symbolic and Hard Links

Since we’ve already discussed how inodes and data blocks work, understanding the difference between hard and symbolic (soft) links is much easier. Both are methods for making a single file appear in multiple locations, but they operate at different layers of the file system.


A hard link is essentially an additional name for an existing inode. When you create a hard link, you aren’t pointing to the file; you are pointing directly to the inode number where the data lives.


A symbolic link is a special type of file that contains a text string: the path to another file or directory. It is the Linux equivalent of a “Shortcut” in Windows.


3. Visual Comparison: The “Library Card” Analogy

Imagine a library (the file system):


The ln (link) command handles both.

Task Command Identification
Create Hard Link ln target linkname ls -i (Look for identical Inode numbers)
Create Symlink ln -s target linkname ls -l (Look for l at the start of permissions and an arrow ->)

5. Practical Summary Table

Feature Hard Link Symbolic Link (Soft)
Inode Shared with the target Unique to the link
Cross-Filesystem No Yes
Link to Directory No Yes
If Target is Moved Still works Breaks
Space Used Zero extra (just a name entry) Minimal (stores path string)

A Real-World “Pro” Tip

In your work with containers or web servers, you’ll often see symlinks used for versioning. For example, a folder named current might be a symlink to v1.2.0. When you’re ready to upgrade, you just update the symlink to point to v1.3.0. This allows for zero-downtime updates!


Directories and Hard Links

Directories use links in a very specific, structural way to maintain the “tree” hierarchy of the Linux filesystem. While you generally cannot create your own hard links to directories as a user, the system creates them automatically to handle navigation.


Every time you create a directory, the filesystem automatically creates two hidden hard links inside it. These are essential for the OS to understand where it is and how to go back.

Why this matters for Inode Counts: If you look at the “Links” column in ls -l, a brand-new, empty directory will show a link count of 2.

  1. One link from the parent directory (the name of the folder).
  2. One link from the . inside the folder itself.

If you create a sub-directory inside it, the parent’s link count jumps to 3 because the new sub-directory’s .. now points back to it.


Linux (and most Unix-like systems) strictly forbids users from creating their own hard links to directories.


Because hard links are restricted, Symbolic Links (Symlinks) are the standard way to “shortcut” into directories.


You can actually calculate how many sub-directories a folder has just by looking at its hard link count:

  1. Run ls -ld /var.
  2. Look at the second column (the link count).
  3. The Formula: $\text{Subdirectories} = \text{Link Count} - 2$.
    • (Subtract 2 for the folder’s own name and its . link).

Link Type Used for Directories? Purpose
Hard Link System Only Created as . and .. to build the tree.
Symbolic Link User & System Used for shortcuts, versioning, and cross-disk access.