Skip to main content

Command Palette

Search for a command to run...

How Does a Linux Hard Link Link to Another File

Updated
6 min read

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.

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.

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.

It’s common to confuse hard links with symbolic (soft) links, but they work differently.

FeatureHard LinkSymbolic Link
Points toSame inode (file data)Path to another file
Works across filesystemsNoYes
Can link to directoriesUsually no (restricted)Yes
Effect if original file deletedFile remains accessibleLink becomes broken
Link count affects file deletionYesNo

Hard links are more like duplicate names for the same file, while symbolic links are shortcuts pointing to the file’s path.

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.txt as a hard link to original_file.txt.
  • Both files share the same inode and data.
  • You can verify this by running ls -li to 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.

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.

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.

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.

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.

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.

ActionEffect on Hard Links
Create hard linkAdds new directory entry, increments link count
Modify file via any linkChanges visible through all hard links
Delete one hard linkRemoves one name, decrements link count
Delete last hard linkDeletes file data from disk
Move or rename hard linkChanges 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

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.

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.

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.

Most common Linux file systems like ext4 support hard links, but some file systems may have restrictions or not support them at all.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts