How Does a Linux Hard Link Link to Another File
Introduction
If you’re working with Linux, you might have heard about hard links but wondered how exactly they link to another file. Understanding this can help you manage your files better and avoid confusion when dealing with file storage and organization.
In this article, I’ll explain how a Linux hard link works, how it connects to the original file, and why it’s different from other types of links. You’ll get clear examples and practical tips to use hard links effectively.
What Is a Linux Hard Link?
A hard link is a way to create another name or reference for an existing file on your Linux system. Unlike shortcuts or symbolic links, hard links point directly to the file’s data on the disk.
- It’s like having two or more names for the same file.
- Both names share the same content and inode (a unique identifier for files).
- Changes made through one link affect the file accessed by the other links.
Hard links are useful when you want multiple paths to the same file without duplicating the data.
How Does a Hard Link Work at the System Level?
To understand how a hard link links to another file, you need to know about inodes. In Linux, every file has an inode, which stores metadata and points to the actual data blocks on the disk.
- A hard link is essentially an additional directory entry that points to the same inode.
- When you create a hard link, you’re adding a new name in the file system that references the same inode number.
- The file system keeps track of how many links point to that inode using a link count.
This means the file’s data remains on the disk as long as at least one hard link exists.
Differences Between Hard Links and Symbolic Links
It’s common to confuse hard links with symbolic (soft) links, but they work differently.
| Feature | Hard Link | Symbolic Link |
| Points to | Same inode (file data) | Path to another file |
| Works across filesystems | No | Yes |
| Can link to directories | Usually no (restricted) | Yes |
| Effect if original file deleted | File remains accessible | Link becomes broken |
| Link count affects file deletion | Yes | No |
Hard links are more like duplicate names for the same file, while symbolic links are shortcuts pointing to the file’s path.
Creating a Hard Link in Linux
You can create a hard link using the ln command in the terminal. Here’s how:
ln original_file.txt hard_link.txt
- This command creates
hard_link.txtas a hard link tooriginal_file.txt. - Both files share the same inode and data.
- You can verify this by running
ls -lito see the inode numbers.
For example:
$ ls -li original_file.txt hard_link.txt
123456 -rw-r--r-- 2 user user 1024 Apr 27 12:00 original_file.txt
123456 -rw-r--r-- 2 user user 1024 Apr 27 12:00 hard_link.txt
Notice both files have the same inode number 123456 and a link count of 2.
How Does the File System Handle Hard Links?
When you create a hard link, the file system updates the directory structure to include the new name pointing to the same inode.
- The inode’s link count increases by one.
- The file’s data blocks remain unchanged.
- Deleting one hard link only removes that directory entry and decreases the link count.
- The file’s data is deleted only when the last hard link is removed.
This mechanism ensures data integrity and efficient storage.
Limitations of Hard Links
While hard links are powerful, they have some limitations:
- You cannot create hard links across different file systems or partitions.
- Hard links to directories are generally not allowed to avoid file system loops.
- Some file systems may restrict or not support hard links.
- Managing many hard links can be confusing if you don’t keep track of all names.
Understanding these limits helps you use hard links wisely.
Practical Uses of Hard Links
Hard links are useful in various scenarios:
- Backup and versioning: Create hard links to save space when backing up files that haven’t changed.
- File organization: Have multiple directory entries for the same file without duplication.
- Software development: Manage files that need to appear in different locations but share content.
- System administration: Use hard links to maintain consistency across configuration files.
By using hard links, you save disk space and maintain file consistency.
How to Check If Two Files Are Hard Links
You can check if two files are hard links to the same inode by comparing their inode numbers.
Use the command:
ls -li file1 file2
If both files have the same inode number, they are hard links to the same file.
Alternatively, use:
stat file1 file2
Look for the Inode field in the output.
What Happens When You Delete a Hard Link?
Deleting a hard link removes one directory entry pointing to the inode.
- The inode’s link count decreases by one.
- If the link count reaches zero, the file system frees the inode and data blocks.
- Other hard links remain valid and can access the file data.
This behavior means deleting one hard link does not delete the file if other links exist.
Summary Table: Hard Link Behavior
| Action | Effect on Hard Links |
| Create hard link | Adds new directory entry, increments link count |
| Modify file via any link | Changes visible through all hard links |
| Delete one hard link | Removes one name, decrements link count |
| Delete last hard link | Deletes file data from disk |
| Move or rename hard link | Changes directory entry, inode unchanged |
Conclusion
Now you know how a Linux hard link links to another file by sharing the same inode and data blocks. Hard links create multiple names for the same file without duplicating data, making them efficient for file management.
By understanding how hard links work, you can use them to save space, organize files better, and maintain consistency. Just remember their limitations and how they differ from symbolic links to avoid common pitfalls.
FAQs
What is the difference between a hard link and a symbolic link?
A hard link points directly to the file’s inode, sharing the same data, while a symbolic link points to the file’s path. Hard links cannot cross file systems, but symbolic links can.
Can I create a hard link to a directory?
Generally, no. Linux restricts creating hard links to directories to prevent file system loops and maintain stability.
How do I find the inode number of a file?
Use the command ls -li filename or stat filename to see the inode number associated with the file.
What happens if I delete the original file but keep the hard link?
The file data remains accessible through the hard link because it points to the same inode. The file is only deleted when all hard links are removed.
Are hard links supported on all Linux file systems?
Most common Linux file systems like ext4 support hard links, but some file systems may have restrictions or not support them at all.
