How Does A Linux Hardlink Link To Another File

8 min read

Understanding how a Linux hard link connects to another file requires a shift in perspective: stop thinking of files as names and start thinking of them as data structures on a disk. Now, unlike a symbolic link, which acts as a shortcut or a pointer to a filename, a hard link is a direct association between a directory entry and the physical data stored on the storage device. This fundamental difference defines how Linux manages file identity, disk space, and data integrity Small thing, real impact..

This is where a lot of people lose the thread.

The Core Concept: Inodes and Directory Entries

To grasp the mechanism of a hard link, you must understand the inode (index node). In Linux filesystems like ext4, XFS, or Btrfs, every file consists of two distinct parts:

  1. The Inode: This is the actual file object. It stores metadata—permissions, ownership, timestamps (access, modification, change), and critically, pointers to the data blocks on the disk where the file content lives. The inode has a unique identification number (the inode number) within a specific filesystem partition.
  2. The Directory Entry (Dentry): This is the "name" you see when you run ls. A directory is essentially a special file containing a table that maps human-readable filenames to inode numbers.

When you create a standard file, you create one inode and one directory entry pointing to it. When you create a hard link, you create a second directory entry (a new name, potentially in a different directory) that points to the exact same inode number.

How the Link Is Established: The System Call Level

The creation of a hard link is handled by the kernel via the link() system call (exposed to userspace via the ln command without the -s flag). The process looks like this:

  1. Verification: The kernel checks if the source path exists and resolves it to an inode.
  2. Filesystem Check: It verifies that the target location (the new link's path) resides on the same filesystem (same mount point). Hard links cannot cross filesystem boundaries because inode numbers are only unique within a single filesystem.
  3. Directory Update: The kernel adds a new entry in the target directory mapping the new filename to the source file's inode number.
  4. Link Count Increment: The kernel increments the i_nlink (link count) field inside the inode itself.

At this precise moment, there is no "original" file and "linked" file. There are simply two directory entries referencing a single inode. Practically speaking, both names are functionally identical peers. Reading, writing, or modifying the file through either name manipulates the exact same data blocks and metadata Simple, but easy to overlook..

The Link Count: The Heart of Persistence

The link count (i_nlink) is the mechanism that governs the lifecycle of the data. You can view this count using ls -l (the number between the permissions and the owner name) or stat.

  • Creation: A new file starts with a link count of 1.
  • Hard Linking: Each new hard link increments the count (1 → 2 → 3...).
  • Deletion (unlink/rm): Deleting a filename calls the unlink() system call. This removes the directory entry and decrements the link count.

Crucial Rule: The actual data blocks on the disk are only marked as free and returned to the filesystem's available pool when the link count drops to zero and no running processes have the file open.

This behavior enables a powerful pattern: you can delete the "original" file you used to create the link, and the data remains perfectly accessible through the remaining hard link(s). The file persists until the very last name pointing to that inode is removed Turns out it matters..

Hard Links vs. Symbolic Links: Structural Differences

The distinction is often a source of confusion. A symbolic link (symlink) is a special file type that contains a text string—the path to the target file. The kernel reads the symlink, sees the path, and redirects the operation to that path.

A hard link is not a special file type. It is a standard directory entry. The kernel does not "follow" a pointer; it simply looks up the inode number in the directory table and accesses the inode directly.

Feature Hard Link Symbolic Link (Symlink)
Inode Shares the exact same inode as target. But Has its own unique inode. But
Cross Filesystem **Impossible. ** Must stay on same partition. Day to day, **Possible. Worth adding: ** Can link across partitions/drives. Even so,
Target Deletion Data survives if link count > 0. Link breaks (dangling pointer).
Directory Linking Generally forbidden for directories (prevents loops). Allowed for directories.
Size Overhead Zero (just a directory entry). Small (stores the path string).

Why Directories Cannot Be Hard Linked (Usually)

You might wonder why ln refuses to hard link directories (throwing "Operation not permitted" even as root on modern kernels). The answer lies in filesystem integrity and the .. (parent directory) entry And that's really what it comes down to..

Every directory contains .Plus, (points to itself) and .. That's why (points to parent). Which means if hard links to directories were allowed:

  1. A directory could have multiple parents. Worth adding: 2. The ..So entry would become ambiguous—which parent is the "real" one? Think about it: 3. This creates cycles in the directory tree, turning the hierarchical tree structure into a general graph.
  2. Tools like find, du, and fsck (filesystem check) rely on a strict tree structure (Directed Acyclic Graph). Cycles would cause infinite loops and corruption during traversal or repair.

While the kernel technically could support it (and very old Unix versions did), it is explicitly disabled in modern Linux (VFS layer) to preserve filesystem sanity. Symlinks are the correct tool for referencing directories Small thing, real impact..

Practical Implications and Use Cases

Understanding the "sameness" of hard links unlocks specific administrative superpowers.

1. Space-Efficient Backups and Snapshots

This is the classic use case for tools like rsync --link-dest or backup utilities like rsnapshot and BackupPC Less friction, more output..

  • Run a full backup (creates files, link count = 1).
  • Run incremental backup: rsync compares source to previous backup. Unchanged files are hard linked to the previous backup directory instead of copied.
  • Result: Ten "full" backups consume only slightly more space than one, because identical files share inodes. Deleting the oldest backup simply unlinks those names; files changed in later backups retain link counts > 0 and keep their data.

2. Atomic File Replacement

Applications often need to update a configuration file or binary without the risk of a reader seeing a half-written file.

  • Write new content to a temporary file (e.g., config.new).
  • Use rename() (atomic on POSIX) to move config.new over config.conf.
  • If config.conf had hard links elsewhere, those other names still point to the old inode (old version). The new name config.conf points to the new inode.
  • This allows seamless versioning or rollback capabilities.

3. File Deduplication

If you have multiple copies of a large dataset (e.g., ISO images, VM templates) in different project folders, you can replace duplicates with hard links using tools like fdupes -L or rdfind. The files appear in all locations, edits

...to one linked file instantly reflect across all directory entries pointing to that inode, saving significant storage without breaking application paths Which is the point..

4. Preserving File Identity Across Moves

Because a hard link is the file (via the inode), moving a hard-linked file within the same filesystem (mv) does not break other links to it. The inode number remains constant. This contrasts sharply with symlinks, which break if the target is moved, and copies, which create distinct, diverging data Most people skip this — try not to..

The unlink Reality: When Data Actually Dies

The rm command is a misnomer; it does not "remove" files. It calls the unlink() system call, which performs two atomic operations:

  1. Removes the directory entry (the name).
  2. Decrements the inode's link count (st_nlink).

Data blocks are only freed (returned to the free pool) when the link count hits zero and no process holds the file open. This explains the classic "disk full but df shows space" scenario: a process holds a deleted log file open. The directory entry is gone (invisible to ls/du), the link count is 0, but the data persists because the file descriptor keeps the inode alive. Restarting the process (or truncating via /proc/<pid>/fd/) finally releases the blocks.

Limitations to Respect

Hard links are not a universal solution. They cannot cross filesystem boundaries (different mount points = different inode tables). They offer no protection against accidental modification—vim editing a hard-linked file typically writes to a new inode (breaking the link) unless configured otherwise, while sed -i or truncate modifies the shared inode in place, affecting all linked names simultaneously. They also complicate backup exclusion logic; excluding one path does not exclude the data if another hard link to the same inode is included Worth keeping that in mind..

Conclusion

Hard links expose the fundamental truth of Unix filesystems: data is identity (inode), names are merely references. By decoupling the namespace from the storage allocation, they enable atomic operations, space-efficient versioning, and structural flexibility impossible with simple copies or symlinks. Mastering them requires shifting mental models from "files as containers" to "inodes as objects with multiple handles." Used wisely, they are a scalpel for storage efficiency; used carelessly, they create a maze of implicit dependencies. The rule remains simple: use hard links for multiple names for the exact same data state on the same filesystem; use symlinks for pointers, cross-filesystem references, or directory aliases.

Freshly Posted

Fresh from the Writer

Handpicked

Related Corners of the Blog

Thank you for reading about How Does A Linux Hardlink Link To Another File. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home