In the Linux file system, every file is composed of two distinct parts: the actual data stored on the storage device, and a directory entry (filename) that points to that data through an internal identifier called an inode. Understanding this separation is key to understanding hard links.
When you create a hard link, you are creating a second, completely independent filename that points directly to the exact same data blocks on the disk. Unlike a symbolic link (symlink), which is essentially a shortcut pointing to another filename, a hard link is an equally valid reference to the underlying data. If you delete the original filename, the data remains safely accessible through the hard link because the file system does not remove the actual data until all hard links pointing to it have been deleted.
How Hard Links Differ from Symbolic Links
Before creating hard links, it is important to understand how they compare to symbolic links:
| Feature | Hard Link | Symbolic Link (Symlink) |
|---|---|---|
| Points to | The inode (data directly) | The filename (path) |
| Survives original deletion | Yes — data persists | No — becomes a broken link |
| Works across file systems | No | Yes |
| Can link to directories | No (restricted by kernel) | Yes |
| File size | Same as original | Tiny (stores only the path) |
| Permissions | Shared with original | Independent from target |
How to Create a Hard Link
The ln (link) command creates hard links by default when used without any flags.
The basic syntax is:
ln [original_file] [new_hard_link_name]
- Open your terminal emulator.
- Navigate to the directory containing the file you want to link, or use absolute paths.
- Type the command. For example, to create a hard link named
backup.txtthat points to the same data asoriginal.txt:ln original.txt backup.txt - Press Enter.
You now have two files in your directory: original.txt and backup.txt. Both point to the exact same data blocks on your storage device. Editing either file changes the content visible through both filenames, because they are the same file underneath.
How to Verify a Hard Link
You can verify that two files are hard-linked by checking their inode numbers. An inode is the internal system identifier assigned to the data on the storage device.
- In the terminal, use the
lscommand with the-i(inode) and-l(long format) flags:ls -il - Look at the first column of numbers in the output. This is the inode number.
- Compare the inode numbers for
original.txtandbackup.txt. If they are identical, the files are hard-linked.
You will also notice that the second column (the link count) shows 2 for both files, indicating that two directory entries point to the same inode.
Alternatively, you can use the stat command for more detailed information:
stat original.txt backup.txt
This displays the inode number, link count, file size, and timestamps for each file.
Practical Use Cases for Hard Links
- Backup systems: Many backup tools (including
rsnapshotand Time Machine on macOS) use hard links to create space-efficient incremental backups. Files that have not changed between backup runs share the same data blocks, dramatically reducing storage consumption. - Organising files without duplication: If you need the same file accessible from multiple directory locations without duplicating it, hard links allow you to maintain a single copy of the data whilst having multiple “access points.”
- Data safety: Because hard links are independent references to the data, accidentally deleting one filename does not destroy the file. The data persists as long as at least one hard link exists.
- Package management: Linux package managers sometimes use hard links internally to manage shared library files efficiently.
How to Check the Link Count of a File
Every file has a link count that indicates how many hard links point to its data.
stat -c '%h' filename.txt
This outputs just the link count number. A regular file with no additional hard links has a count of 1. After creating one hard link, both files will show a count of 2.
How to Find All Hard Links to a File
If you want to locate all hard links pointing to the same data:
- First, find the inode number of the file:
ls -i filename.txt - Then search for all files with that inode number:
find / -inum [inode_number] 2>/dev/null
Replace [inode_number] with the actual number from step 1. The 2>/dev/null portion suppresses permission-denied error messages for directories you cannot access.
Important Limitations of Hard Links
Hard links have two strict limitations enforced by the Linux kernel:
- You cannot hard link directories: Creating hard links to folders could create infinite recursive loops that would break file system integrity checks and tools like
find. The kernel explicitly prevents this. Use symbolic links if you need to create shortcuts to directories. - You cannot hard link across different file systems or partitions: Both the original file and the hard link must reside on the exact same partition, because inode numbers are only unique within a single file system. Attempting to create a hard link across partitions will produce an “Invalid cross-device link” error. Use symbolic links for cross-partition references.
How to Remove a Hard Link
Removing a hard link is identical to deleting any file:
rm backup.txt
This removes the backup.txt directory entry and decrements the link count by one. The actual data on the storage device is only deleted when the link count reaches zero — meaning all filenames pointing to that data have been removed.