Course contentsShow
← Linux
Lesson 278 of 1,8193. The Filesystem Hierarchy and POSIX StandardsFree lesson

Hard Links and Inode Referencing

Distinguish between file content persistence and directory entries using hard links.

In this lesson, you will learn how hard links allow multiple directory entries to point to the exact same underlying file data on your disk.

Understanding Inodes and Data

As we covered in our exploration of the Virtual Filesystem (VFS), a file in Linux is split into two parts: the metadata/data blocks (stored in an inode) and the filename (stored in a directory entry).

Normally, a directory entry points to an inode, and that inode tracks how many directory entries are currently pointing to it—this is called the link count.

What is a Hard Link?

A hard link is simply a new directory entry that points to an existing inode. Unlike a copy of a file, which creates a new inode and replicates the data, a hard link creates no new data. It is literally just an additional "name" for the same piece of information.

Consider this scenario:

# Create a file
echo "Hello World" > original.txt

# Create a hard link
ln original.txt link.txt

# Inspect the link count
ls -li

In the output of ls -li, you will notice that both original.txt and link.txt share the exact same inode number (the first column). Because they share the same inode, any changes made to the content of link.txt will immediately appear in original.txt. They are not "linked" to each other; they are both equally valid entry points to the same storage blocks.

Persistence and Deletion

This distinction is crucial for file persistence. If you delete original.txt, the data does not disappear from your drive. The system simply decrements the link count for that inode. As long as the link count is greater than zero, the file content remains persistent. The data is only truly marked for deletion by the filesystem once the last hard link is removed and the link count hits zero.

Because of this mechanism, you can safely move or rename hard links without breaking the connection, as the inode remains the constant anchor for your data.

Key Takeaway: A hard link is not a shortcut or a copy; it is a directory entry that shares the same inode as the original, ensuring data persists as long as at least one link remains.