Understanding how to create a hard link in linux is a fundamental skill for system administrators, developers, and power users who want to manage file systems efficiently. Unlike symbolic links (symlinks), which act as shortcuts pointing to a file path, a hard link creates a direct association between a filename and the physical data on the disk. This distinction is critical because it affects how storage space is calculated, how file permissions behave, and what happens when the original file is deleted. Mastering the ln command and the underlying inode structure allows you to build solid backup strategies, save disk space, and organize data without unnecessary duplication.
Understanding the Core Concept: Inodes and Hard Links
Before diving into the syntax, it is essential to grasp why hard links work the way they do. In Linux file systems (like ext4, XFS, or Btrfs), every file is represented by an inode (index node). An inode is a data structure that stores metadata about a file—permissions, ownership, timestamps, and crucially, pointers to the actual data blocks on the storage drive.
When you create a hard link in linux, you are not copying the data. Instead, you are creating a new directory entry (a new filename) that points to the exact same inode as the original file Turns out it matters..
- Same Inode Number: Both the original file and the hard link share the identical inode number.
- Link Count Increments: The inode maintains a "link count." Creating a hard link increments this count; deleting a file decrements it.
- Data Persistence: The physical data blocks are only marked as free and overwritten when the link count drops to zero.
This mechanism makes hard links distinct from copies (which consume double the space) and symlinks (which break if the target moves).
Key Differences: Hard Links vs. Symbolic Links
To avoid confusion, compare the two linking mechanisms side-by-side:
| Feature | Hard Link | Symbolic Link (Symlink) |
|---|---|---|
| Inode | Shares the same inode as target. | Has its own unique inode. |
| Cross Filesystem | Cannot span different partitions/filesystems. | |
| Size on Disk | Zero additional data blocks used. Day to day, | Can link directories. |
| Directories | Generally cannot link directories (prevents loops). | |
| Target Deletion | Data remains accessible via the link. | Consumes a small amount of space for the path text. |
The Syntax: Using the ln Command
The standard utility for linking is ln. The basic syntax for creating a hard link is straightforward:
ln [OPTIONS] TARGET_FILE LINK_NAME
- TARGET_FILE: The existing file you want to link to.
- LINK_NAME: The name of the new hard link you are creating.
Crucial Note: Do not use the -s flag. The -s flag creates a symbolic link. Omitting -s defaults to a hard link.
Basic Example
Imagine you have a file named data.txt in your current directory and you want a hard link named data_backup.txt.
ln data.txt data_backup.txt
After running this, ls -l will show a link count of 2 for both files, and ls -i will reveal they share the same inode number.
Practical Scenarios and Advanced Usage
1. Verifying the Link Creation
Always verify your work. Use ls -l to check the link count (the number between the permissions and the owner name) and ls -i to confirm the inode match.
ls -li data.txt data_backup.txt
Output interpretation:
- First column: Inode number (should be identical).
- Third column: Link count (should be 2).
2. Creating Hard Links in Different Directories
You can place the hard link in a completely different directory path, provided it resides on the same mounted filesystem The details matter here..
ln /home/user/project/config.yaml /var/www/html/config.yaml
This is extremely useful for configuration management. You edit the file in your project directory, and the web server reads the exact same physical data instantly, without syncing or copying.
3. Using Relative vs. Absolute Paths
While symlinks store the path string you give them (relative or absolute), hard links resolve the target immediately to an inode. Because of this, the path used for the target argument only matters for locating the file at the moment of creation Practical, not theoretical..
# Using relative path
ln ../notes.txt ./link_to_notes.txt
# Using absolute path
ln /home/user/notes.txt ./link_to_notes.txt
Both commands produce the exact same result: a directory entry pointing to the inode of notes.txt Not complicated — just consistent..
4. Forcing Overwrite (-f Flag)
If LINK_NAME already exists, ln will fail with an error: File exists. To force the removal of the existing destination file and create the new link, use the -f (force) flag Simple as that..
ln -f target.txt existing_link.txt
Warning: This permanently deletes existing_link.txt (or decrements its link count) before creating the new association.
5. Interactive Mode (-i Flag)
For safety, especially in scripts or bulk operations, use -i to prompt before overwriting.
ln -i target.txt link.txt
6. Creating Multiple Links at Once
You can create multiple hard links to the same target in a single command by specifying a target directory as the last argument.
ln file1.txt file2.txt file3.txt /destination/directory/
This creates file1.txt, file2.txt, and file3.txt inside /destination/directory/, all pointing to the original inodes.
Limitations and "Gotchas"
Despite their utility, hard links have strict limitations imposed by the kernel to maintain filesystem integrity.
1. No Cross-Filesystem Linking
This is the most common error: Invalid cross-device link. You cannot hard link a file on /dev/sda1 (root) to /dev/sdb1 (a mounted USB drive or separate home partition). The inode tables are separate. Use a symlink or cp (copy) instead That's the part that actually makes a difference. Took long enough..
2. No Hard Linking Directories (Usually)
Modern Linux kernels prevent creating hard links to directories (ln -d is restricted to root and highly discouraged). Allowing directory hard links creates cycles in the filesystem tree, confusing tools like find, du, and fsck. Use mount --bind or symlinks for directory aliasing Small thing, real impact..
3. Permissions and Ownership Are Shared
Because the metadata lives in the single inode, changing permissions (chmod), ownership (chown), or timestamps (touch) on one link affects all links instantly. You cannot have different permissions for different hard links pointing to the same data Which is the point..
4. The rm Behavior
Deleting a hard link (rm link_name) only removes that specific directory entry and decrements the link count. The data remains on disk and is accessible via any remaining links. The file is truly "deleted" (data blocks freed) only when the last hard link is removed Most people skip this — try not to..
Real-World Use Cases
Efficient Backup Snapshots (Rsync --link-dest)
This is the "killer feature" of hard links. Tools like rsync can create incremental backups that appear to be full copies