What a hard link is
The command ln src dst (no flags) creates a hard link, a new entry in
the directory that points to the same inode as src. After that:
srcanddsthave the same inode number (ls -ishows it)staton both reportsLinks: 2(the number of names went up)- changes made through either name are visible through the other; it is one file
rm srcleaves the file intact: the blocks are freed only when the last name is gone
Limits
A hard link can only be made within one filesystem, since an inode is
local to a specific FS. And only to regular files: a hard link to a directory
is forbidden (it would break the acyclic structure of the file tree; . and
.. are the exception).
For cross-filesystem references and links to directories, use symbolic-link.
Where you meet it in practice
- Snapshot backups (rsync
--link-dest): between backups, unchanged files keep hard links to each other, which saves a huge amount of space byte for byte - Deduplication in
/usr/share/docand Docker layer images (on ext4) - Atomic publishing:
ln new stagingfollowed bymv -T staging oldguarantees that at any momentoldpoints to either the old or the new content, with no in-between state