In Unix-like operating systems, both soft (symbolic) and hard links are used to link files together. However, they behave differently and serve different purposes.
A hard link is a reference (pointer) to the physical data on a disk. Multiple hard links can refer to the same data on the disk, and deleting one hard link does not affect the others.
- A hard link points directly to the inode of the file.
- Multiple hard links share the same inode.
- If the original file is deleted, the data is still accessible through any remaining hard links.
- Hard links cannot span across different file systems (partitions).
ln source_file hard_link_name
# Create a file
echo "This is the original file." > original.txt
# Create a hard link to the original file
ln original.txt hard_link.txt
# List the inodes of the files
ls -i original.txt hard_link.txt
In this example, both original.txt
and hard_link.txt
will have the same inode number, meaning they point to the same data on disk.
Even if you delete original.txt
, the data can still be accessed through hard_link.txt
because they share the same inode.
rm original.txt
cat hard_link.txt # The content is still accessible
A soft link (also called a symbolic link or symlink) is a pointer to another file or directory. Unlike hard links, a soft link does not point directly to the inode but rather references the file name.
- A soft link points to the file name, not the inode.
- Deleting the original file breaks the symbolic link.
- Soft links can span across different file systems and partitions.
- Soft links can point to directories, unlike hard links.
ln -s source_file soft_link_name
# Create a soft link to the original file
ln -s original.txt soft_link.txt
# List the files to see the symbolic link
ls -l original.txt soft_link.txt
In this case, soft_link.txt
is a symbolic reference to original.txt
. If you delete original.txt
, the soft link will be broken, and accessing soft_link.txt
will result in an error:
rm original.txt
cat soft_link.txt # This will return an error since the link is broken
Feature | Hard Link | Soft Link |
---|---|---|
Points to | The inode (actual data) | The file name (path) |
Inode number | Same as the original file | Different from the original file |
Effect when original is deleted | The link remains valid, and data is still accessible | The link is broken, and the file becomes inaccessible |
Cross-filesystem support | No | Yes |
Can link to directories | No | Yes |
To visualize the difference between hard and soft links, you can list the inode numbers of files:
# Create a hard link
ln original.txt hard_link.txt
# Create a soft link
ln -s original.txt soft_link.txt
# List the inode numbers of all three files
ls -i original.txt hard_link.txt soft_link.txt
Output:
123456 original.txt
123456 hard_link.txt
789012 soft_link.txt
Notice that original.txt
and hard_link.txt
share the same inode number, while soft_link.txt
has a different one.
- Hard links directly reference the same data (inode), making them resilient to file deletions but restricted to the same filesystem.
- Soft links reference the file name, allowing more flexibility (like linking to directories or spanning filesystems), but they break if the original file is deleted.
Understanding the use cases for both types of links can help you manage files and storage effectively in Unix-like systems.