Hard Link vs. Symlink
Last updated
Last updated
Hard links point to inode and symlinks point to the file. Visually:
The inode is a database that describes the file/directory attributes such as metadata and the physical location on the hard drive.
A hard link is a direct reference to a file via its inode. By using a hardlink, you can change the original file’s contents or location and the hardlink will still point to the original file because its inode is still pointing to that file. There is no referencing to the original file.
For example, we create a hard link of the file fun
and name it fun-hard
:
The original file fun
and the hard link fun-hard
share the same inode number, so we can confirm that they are the same file:
Hardlinks have two important limitations:
A hard link cannot reference a file outside its own file system. This means a link cannot reference a file that is not on the same disk partition as the link itself.
A hard link may not reference a directory.
Symbolic links (symlinks) were created to overcome the limitations of hard links. Symlinks are essentially shortcuts that reference to a file instead of its inode value. If the original file is deleted, then its symlink becomes broken link or dangling link.
For example, we create a symlink of the file fun
and name it fun-sym
:
The symlink is represented by the l bit, its size is small, and Linux indicates the symlink using an arrow:
If we delete fun, then fun-sym becomes a broken link. Linux marks a broken link as red: