Learn how Linux file systems use hard links to let multiple directory entries point to the same underlying data. You’ll see how to create, inspect, and remove hard links without wasting disk space.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Files, Inodes, and Link Counts
Every file on a Linux filesystem (ext4, XFS, etc.) is represented by an inode. The inode stores metadata—permissions, timestamps, and pointers to data blocks on disk. The “Links” count in thestat output shows how many directory entries (hard links) reference that inode.
Let’s simulate Aaron saving a photo of his dog:
Why Use Hard Links?
Copying files duplicates data and consumes extra space:| Comparison | Copy (cp) | Hard Link (ln) |
|---|---|---|
| Disk Usage | Duplicated per copy | Single copy on disk |
| Data Consistency | Separate files | Always the same content |
| Link Count Effect | Independent inodes | Inode “Links” count increases by 1 |
Hard links only work on regular files within the same filesystem. See limitations below.
Inspecting Link Counts
After creating the hard link, check the link count again:Links: 2 confirms two directory entries point to the same inode.
Removing Hard Links
-
If Aaron removes his entry:
-
Only when all links are removed does the filesystem reclaim the inode and free the data blocks:
Limitations of Hard Links
You cannot:- Link directories (to prevent cycles in the filesystem tree).
- Cross filesystem boundaries (e.g., SSD → external drive).

Attempting to
ln a file across different mounts will fail silently or return an error: