Hard Links in Linux — Understanding Inodes and File Linking¶
A Hard Link is another name for the same file in the filesystem. Unlike a symbolic (soft) link, a hard link points directly to the file's inode, not its pathname. This means all hard links are equal references to the same underlying data. Understanding hard links is essential for Linux administrators, DevOps engineers, and anyone working with Linux filesystems.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand what a hard link is
- Learn how Linux stores files
- Understand inodes
- Create hard links
- Compare hard links and symbolic links
- View inode numbers
- Identify hard links
- Use hard links in production
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Command Line Essentials
- Module 3 – Text Processing
- Module 4 Lesson 1 – File Types
Why Learn Hard Links?¶
Imagine this situation:
You have a file:
Another application needs the same file with a different name:
Should you:
- Copy the file?
- Create a symbolic link?
- Create a hard link?
Understanding hard links helps you save disk space and understand how Linux filesystems work internally.
How Linux Stores Files¶
Linux separates:
The filename is not the file itself.
Instead:
- Filename → points to an inode.
- Inode → points to the actual file data.
What is an Inode?¶
An inode stores metadata about a file.
It contains:
- File owner
- Group
- Permissions
- File size
- Timestamps
- Link count
- Data block locations
It does not store the filename.
Viewing Inodes¶
Create a file.
Display inode.
Example:
Here:
is the inode number.
Creating a Hard Link¶
Syntax:
Example:
Display:
Output:
524288 -rw-r--r-- 2 basha users 15 Jan 10 notes.txt
524288 -rw-r--r-- 2 basha users 15 Jan 10 backup.txt
Notice:
- Same inode
- Same file size
- Link count is now 2
Understanding Link Count¶
Example:
Both names reference the same inode.
The link count indicates how many directory entries point to that inode.
Modifying a Hard Link¶
Edit:
Read:
Output:
Both filenames display the same content because they reference the same data.
Deleting One Hard Link¶
Remove:
Check:
The file still exists.
Only one directory entry was removed.
When is Data Deleted?¶
A file's data is removed only when:
- The link count becomes 0
- No process is keeping the file open
Visual Representation¶
Before:
After creating a hard link:
Hard Link vs Copy¶
Copy:
Results:
- Different inode
- Separate file
- Changes are independent
Hard link:
Results:
- Same inode
- Same data
- Changes appear in both names
Verify Using ls¶
Example:
Find Files with Multiple Hard Links¶
or
Limitations of Hard Links¶
Hard links:
Limitation
Cannot span different filesystems.
Limitation
Cannot normally link directories.
These restrictions help prevent filesystem corruption and directory loops.
Common Commands¶
Create:
Display inode.
Find same inode.
Count links.
View Link Count¶
Example:
Hard Links vs Symbolic Links¶
| Hard Link | Symbolic Link |
|---|---|
| Same inode | Different inode |
| Points to file data | Points to pathname |
| Survives original filename deletion | Breaks if target is deleted |
| Cannot span filesystems | Can span filesystems |
| Usually cannot link directories | Can link directories |
Real Production Examples¶
Package managers use hard links to reduce duplicate storage.
Backup software uses hard links for incremental backups.
Log rotation tools may use hard links internally.
Administrators use hard links to avoid duplicate copies of large files.
Production Perspective¶
Hard links are useful for:
- Incremental backups
- Saving disk space
- File deduplication
- Package management
- Filesystem understanding
- Advanced troubleshooting
Although many administrators use symbolic links more often, understanding hard links explains how Linux filesystems manage files internally.
Hands-on Lab¶
Task 1¶
Create a file.
Task 2¶
Display inode.
Task 3¶
Create a hard link.
Task 4¶
Display inode numbers.
Verify both files share the same inode.
Task 5¶
Append data.
Task 6¶
Read the original file.
Notice the appended content.
Task 7¶
Delete the hard link.
Verify the original file still exists.
Task 8¶
Display metadata.
Observe the link count.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
ln | Create hard link | Backups |
ls -li | Display inode numbers | Filesystem analysis |
stat | Show metadata | Troubleshooting |
find -samefile | Find hard links | File audits |
Production Troubleshooting Scenario¶
Scenario
A storage administrator notices that deleting a filename does not free disk space.
Investigation:
- Check the inode.
- Verify the link count.
- Find other hard links.
- Remove unnecessary links.
Commands:
The investigation reveals that multiple hard links still reference the same inode, so the data remains allocated.
Best Practices¶
- Use hard links only within the same filesystem.
- Avoid creating hard links for directories.
- Use
ls -liandstatto understand link counts. - Use hard links for efficient backup strategies.
- Prefer symbolic links when linking across filesystems.
Common Mistakes¶
❌ Assuming a hard link is a shortcut.
✅ It is another directory entry for the same file, not a reference like a symbolic link.
❌ Expecting hard links to work across different filesystems.
✅ They cannot.
❌ Believing deleting the original filename deletes the file.
✅ The file remains until the last hard link is removed and no process has it open.
Interview Questions¶
Beginner¶
- What is a hard link?
- What is an inode?
- Which command creates a hard link?
- How do you view inode numbers?
Intermediate¶
- Why do hard links share the same inode?
- Why can't hard links usually be created for directories?
- What happens when one hard link is deleted?
- How do you find all hard links to a file?
Architect Level¶
- Why are hard links useful for incremental backups?
- Explain how Linux deletes a file internally.
- When would you choose a hard link instead of a symbolic link?
Summary¶
In this lesson, you learned:
- What hard links are
- How Linux stores files using inodes
- Creating and managing hard links
- Viewing inode numbers
- Understanding link counts
- Differences between hard and symbolic links
- Production use cases
Hard links provide multiple names for the same file without duplicating data. Understanding them is essential for mastering Linux filesystems and advanced file management.
Key Takeaways¶
- A hard link is another name for the same file.
- Hard-linked files share the same inode.
- Deleting one hard link does not delete the underlying data.
- Data is removed only after the last hard link is deleted and no process is using the file.
- Hard links cannot normally span filesystems or link directories.
What's Next?¶
Symbolic (Soft) Links in Linux — Creating Flexible File References
In the next lesson, you'll learn:
- Creating symbolic links
- Relative vs absolute links
- Broken symbolic links
- Symlink management
- Real-world DevOps deployment strategies