Symbolic Link vs Hard Link in Linux: Key Differences
In the Linux operating system, links are pointers that allow users and applications to access files located elsewhere in the file system without duplicating data. This article explains the fundamental differences between symbolic links (soft links) and hard links, focusing on how each relates to filesystem inodes, their behavior when source files are deleted, their support for directories, and their ability to cross different filesystems.
Inode Association and File Identity
The primary technical difference between the two lies in how they reference an inode (an index node containing metadata about a file).
- Hard Link: A hard link points directly to the inode of the original file. It shares the exact same inode number, permissions, ownership, and data blocks as the target file. To the operating system, a hard link is essentially an identical, mirror copy of the original filename entry pointing to the same physical data on the disk.
- Symbolic Link: A symbolic link (or symlink) is an entirely independent file with its own unique inode number. Instead of pointing directly to the file data, a symlink contains a small string representing the file path of the target file.
Behavior When the Original File Is Deleted
The distinction in inode handling directly impacts what happens when the original file is removed:
- Hard Link: Deleting the original file does not delete the underlying data. In Linux, data blocks are only freed when the link count of an inode reaches zero. As long as at least one hard link remains, the data is completely accessible.
- Symbolic Link: If the original file is deleted, renamed, or moved, the symbolic link becomes broken or "dangling." Because the link only points to the path and not the inode itself, attempting to access a broken symlink results in a "File not found" error.
Cross-Filesystem Limitations
- Hard Link: Hard links cannot cross different
partitions or filesystems. Inode numbers are unique only within a
specific filesystem; therefore, an inode on partition
/dev/sda1cannot be linked to partition/dev/sdb1. - Symbolic Link: Symlinks can span across different filesystems, drives, network shares, and partitions without issue because they store only an absolute or relative directory path.
Support for Directories
- Hard Link: Standard users cannot create hard links to directories. Linux restricts this to prevent circular directory references, which could cause infinite loops for file-traversal tools and filesystem corruption.
- Symbolic Link: Symbolic links fully support directories, making them the standard tool for creating directory shortcuts across the filesystem.
Command Usage
Both types of links are created using the ln
command:
- To create a hard link:
ln target_file link_name - To create a symbolic link:
ln -s target_file link_name
In summary, use a hard link when you need resilient access to data that survives deletion of the original file entry within the same partition. Use a symbolic link when you need to link directories, point to files across different drives or partitions, or create flexible shortcuts.