Hard Link vs. Symlink

Hard links point to inode and symlinks point to the file. Visually:

Inode

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:

ln fun 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:

  1. 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.

  2. 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:

ln -s fun 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:

Reference

Last updated