File Systems: The Interface

a hard link

Imagine a person who genuinely goes by two names, both are real legal names for the same human being, not a nickname pointing at a real name. A hard link is like that. It is a second (or third) directory entry that names the very same file, the exact same underlying data and metadata, so the file truly has multiple equal names, and no one of them is more "the real one" than the others.

This works because of how directories and metadata are separated. The file's real identity lives in a metadata record (in Unix systems, the inode), and a directory entry is just a name paired with a pointer to that inode. A hard link is simply another directory entry pointing at the SAME inode. The inode keeps a reference count of how many directory entries point to it. Creating a hard link adds an entry and bumps the count; deleting a name removes that entry and decrements the count. The actual data is freed only when the count hits zero, that is, when the LAST name is gone. So deleting one name leaves the file fully intact under its other names.

Why it matters: hard links let several places in the tree share one file with zero duplication, edits through any name are seen through all of them, because there is only one file. But they come with honest limits. Because all the names are peers pointing at one inode, hard links normally cannot cross from one file system to another (inode numbers are local to a file system), and most systems forbid hard-linking directories to prevent loops. This is the key contrast with a symbolic link: a hard link is another true name for the data; a symbolic link is merely a path that points at a name and can break.

Create file a.txt, then make a hard link b.txt to it. The inode's reference count is now 2. Editing through a.txt changes what you see through b.txt, they are one file. Delete a.txt and b.txt still works perfectly; the data is freed only when b.txt is also removed.

Two equal names for one inode; the file lives until the last name is gone.

A hard link is a true peer name, not a pointer to a name, so deleting one name never breaks the others; the data survives until the reference count reaches zero. Hard links usually cannot cross file systems, and directories are normally not hard-linkable.

Also called
hard link硬鏈結