Linux / OS Fundamentals Series

🔗 Understanding the Difference Between Hard Links and Symbolic Links From a Top 1% Perspective — the Inode, Another Entity Tying a Filename to Its Data

Introduction

This article is part of the Top 1% Series Complete Article Guide, and article 4.1 in the Linux/OS Fundamentals Series.

Prerequisite Knowledge

Getting the Big Picture

Many people think a “filename” is the file’s substance, but this isn’t accurate. In a Linux filesystem, a file’s actual data and attributes (owner, permissions, size, pointers to data blocks, and so on) are stored inside an entity managed by number, called an inode. And the “filename” you see inside a directory is nothing more than a “label” (a directory entry) pointing at that inode number. Starting from this premise, you can see that hard links and symbolic links — two kinds of “links” — are entirely different mechanisms.

graph TB
    subgraph Hard["Hard Link"]
        NameA["Name A (report.txt)"] --> Inode1["inode #1234<br/>(the actual data)"]
        NameB["Name B (backup.txt)"] --> Inode1
    end
    subgraph Symlink["Symbolic Link"]
        NameC["Name C (link.txt)<br/>content: the string 'report.txt'"] -.->|follows the path string| NameA2["Name A (report.txt)"]
        NameA2 --> Inode2["inode #1234<br/>(the actual data)"]
    end

Deep Dive Into the Fundamentals

A hard link (the ln command, with no options) creates a new directory entry pointing at the same inode number as an existing file. Between the two names created, there’s no distinction at all between “the original name” and “the link” — they’re two perfectly equal entry points into the exact same file. An inode keeps track of the number of directory entries pointing at it (a link count), and deleting either name with the rm command only decrements that link count by one — the inode itself and the data inside it never get deleted until the link count reaches zero.

The Real Identity of "Deleting a Log File That Somehow Never Frees Up Disk Space"

Here’s a phenomenon you’ve probably hit in real-world work: you delete a huge log file with rm, but checking with the df command shows no increase in free disk space at all. The cause is usually a process still running with that log file still open (still holding a file descriptor for it). The rm command only deletes the directory entry (the name) and decrements the link count by one — as long as a process’s file descriptor, a separate path to the inode distinct from the name, still references it, the link count never reaches zero, and the actual data blocks never get freed. The fix is to restart that process, or identify the deleted-but-still-open file with a command like lsof | grep deleted and restart whatever process is holding it.

A symbolic link (the ln -s command) works through an entirely different mechanism from a hard link. A symbolic link is itself a completely independent file with its own inode, whose content is nothing more than “a string representing the path to the target.” When you access a symbolic link, the kernel performs one extra layer of indirect processing: it first reads that file’s content (the path string), then follows that path again to actually reach the real target file. As a result, if the real target gets deleted or moved, the symbolic link itself keeps existing, but becomes a “dead link” (a dangling link), pointing at nothing at all — a behavior that contrasts directly with a hard link.

What a Pro Sees Here (Top 1% Understanding)

A hard link can only ever be created within the same filesystem (partition). This is because inode numbers are only managed uniquely within each individual filesystem. Trying to create a hard link to a different filesystem fails, since there’s no corresponding inode number to reference at all. Also, most implementations prohibit hard links to directories. This is because allowing a hard link to a directory would create a cycle inside the directory tree, risking an infinite loop in a tree-traversal command like find. A symbolic link, being an independent file whose only content is a path string, never carries any of these constraints — it can freely be created across different filesystems, and can freely point at a directory, both at once. The usrmerge covered in /etc and Linux’s Directory Structure, turning /bin into something pointing at /usr/bin, is implemented with a symbolic link rather than a hard link, and this difference in constraints is one of the reasons.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. You deleted a file with rm, but free disk space never increased: Use lsof | grep deleted to identify a process still holding a deleted-but-open file.
  2. Following a symbolic link gives a “file not found” error: Check whether the real target got deleted or moved, turning it into a dead link (a dangling link).
  3. Creating a hard link to a different partition fails: Since a hard link can only be created within the same filesystem, use a symbolic link instead, or actually copy the file.

Summary

Takeaways to Apply Today

  1. When you hit the issue “I deleted a file but disk space didn’t go down,” check whether any process is still holding that file open.
  2. When choosing between a hard link and a symbolic link, first check whether you need to cross filesystems, or need to point at a directory.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).