This article explains how to create and manage hard links in Linux, highlighting their differences from soft links and their practical applications.
In this lesson, we will explore how Linux manages hard links and understand the differences between hard links and soft links. Before diving into hard links, it is important to grasp a few fundamental concepts about Linux file systems.Imagine a Linux computer shared between two users, Aaron Lockhart and Jane. Each user logs in with their unique credentials—which means they have separate desktops, program settings, and personal directories.Suppose Aaron takes a picture of the family dog and saves it at: /home/aaron/pictures/family_dog.jpgWe can simulate creating a file at that path by storing the text “Picture of Milo the Dog” in it. Linux offers the stat command to display detailed information about files and directories, including the inode number. In file systems such as XFS or ext4, inodes contain data block locations and metadata (like permissions and timestamps).When a file (e.g., family_dog.jpg) is created, Linux assigns it an inode (e.g., 52946177) to hold its data and metadata. Initially, the file gets one hard link pointing to that inode. Instead of referencing the inode directly, the system uses a file name that points to the inode holding all of the file’s necessary information.The stat command output lists the number of hard links associated with the inode. Consider the following example:
When you access the file (for example, by reading family_dog.jpg), Linux refers to inode 52946177 to retrieve the data. The “Links” count indicates how many file names point to that inode.
Suppose Jane has her own pictures folder at /home/jane/pictures, and Aaron wants to efficiently share his family photograph with her. One basic approach would be to copy the file:
However, if there are thousands of pictures, copying can duplicate massive amounts of data. To avoid redundancy, you can create a hard link. With a hard link, the file data is stored only once on the disk while multiple file names (even in different directories) point to the same inode and data.The syntax for creating a hard link is: ln [path to target file] [path to link file]For example, to create a hard link for family_dog.jpg in Jane’s directory, run:
This confirms that either /home/aaron/Pictures/family_dog.jpg or /home/jane/Pictures/family_dog.jpg will retrieve the same underlying data. If Aaron later deletes his version with:
Copy
Ask AI
$ rm /home/aaron/Pictures/family_dog.jpg
Jane still has access to the file because at least one hard link remains. Only when the link count reaches zero is the data removed from the disk.
For clarity, here is a consolidated example of creating a hard link and observing the resulting link count:
Copy
Ask AI
$ echo "Picture of Milo the Dog" > /home/aaron/Pictures/family_dog.jpg$ ln /home/aaron/Pictures/family_dog.jpg /home/jane/Pictures/family_dog.jpg$ stat /home/aaron/Pictures/family_dog.jpg
Once the link count is verified as 2, if both users delete their hard links:
Hard links are subject to certain restrictions and best practices:
Consideration
Details
File Type
Hard links can only be created for files, not directories.
File System
Hard links must reside on the same file system. For example, you cannot create a hard link from an SSD (/home/aaron) to an external drive (/mnt/backups).
Write Permissions
Proper write permissions are required in the destination directory (e.g., /home/jane/pictures).
Group Access
To share a file between users, consider adding them to a common group (e.g., “family”) and adjust permissions accordingly.
For instance, if Aaron and Jane need to share access to the file, you might execute:
Copy
Ask AI
$ usermod -a -G family aaron$ usermod -a -G family jane$ chmod 660 /home/aaron/Pictures/family_dog.jpg
This ensures that modifying permissions on any hard link updates the inode, so all links reflect the change.
Hard links are a robust feature of Linux file systems, enabling efficient file sharing and storage optimization. By storing a file’s data only once while making it accessible via multiple file names, hard links conserve disk space and simplify access management. Data is only removed when all links pointing to the inode are deleted.In this lesson, we examined the concept, usage, and limitations of hard links. For more insights into Linux file system management, continue exploring the additional lessons in this series.