Skip to main content

Hard and Soft Links

A link is a reference from one path in the filesystem to another file or directory. Linux supports two kinds: hard links and symbolic (soft) links. Both are created with the ln command but behave very differently.

Inodes

Every file on a Linux filesystem is identified by an inode, a number that points to the actual data on disk. A filename is just a label that maps to an inode, and multiple filenames can point to the same inode.

ls -i file.txt # Show the inode number of a file

A hard link creates an additional filename for the same inode. Both names refer to exactly the same file on disk. Neither is "the original". The file's data is only released when the last hard link to it is removed.

ln target.txt linkname.txt

Properties

  • Both names share the same inode
  • Removing one name does not affect the other
  • Cannot span filesystems, both names must live on the same partition
  • Cannot link directories (with rare exceptions reserved for the system)
  • Cannot link to a file that does not exist

Example

echo "hello" > original.txt
ln original.txt hardlink.txt
ls -li original.txt hardlink.txt
# 12345 -rw-r--r-- 2 user user 6 Apr 25 10:00 original.txt
# 12345 -rw-r--r-- 2 user user 6 Apr 25 10:00 hardlink.txt
rm original.txt
cat hardlink.txt # still prints "hello"

A symbolic link is a small special file that stores the path to another file or directory. It has its own inode. If the target is moved or deleted, the symlink becomes "dangling" and broken.

ln -s target.txt linkname.txt

Properties

  • Has its own inode separate from the target
  • Can span filesystems freely
  • Can link directories
  • Can point to a non-existent target (broken/dangling link)
  • Can use absolute or relative paths as the target

Example

echo "hello" > original.txt
ln -s original.txt softlink.txt
ls -li original.txt softlink.txt
# 12345 -rw-r--r-- 1 user user 6 Apr 25 10:00 original.txt
# 12346 lrwxrwxrwx 1 user user 12 Apr 25 10:00 softlink.txt -> original.txt
rm original.txt
cat softlink.txt # error: No such file or directory
PropertyHard LinkSoft Link
Points toinodepath
Own inode?No (same as target)Yes
Can cross filesystems?NoYes
Can link directories?NoYes
Can target a missing file?NoYes
Survives target deletion?YesNo
Created withlnln -s

Common ln Options

CommandDescription
ln target nameCreate a hard link
ln -s target nameCreate a symbolic (soft) link
ln -f target nameRemove (overwrite) an existing destination file
ln -i target namePrompt before overwriting an existing destination
ln -b target nameBack up an existing destination before replacing it (appends ~ to the filename)
ln -v target namePrint the name of each linked file (verbose)
ln -sf target nameForce-replace an existing link of the same name
readlink nameShow the path a symbolic link points to
readlink -f nameResolve all symlinks to the canonical absolute path

When to Use Which

  • Hard links are useful for keeping multiple stable names for the same file (for example, backup snapshots that share unchanged content) without using extra disk space.
  • Soft links are the more common choice for shortcuts. For example switching between versions of a tool (e.g. /usr/bin/python => python3.12), or referencing files across mount points.