Hard and symbolic links

Updated November 1, 2026

A hard link is another name for the same inode. A symbolic link is a separate file holding a path to the target.

ln  target link        # hard: the same inode
ln -s target link     # symbolic: a pointer to a path
stat -c '%h' target   # the hard link count

Sample output

$ ls -li
total 12
2372199 -rw-r--r-- 2 student student  5 Jul 26 04:04 hard.txt
2372199 -rw-r--r-- 2 student student  5 Jul 26 04:04 original.txt
2372200 lrwxrwxrwx 1 student student 12 Jul 26 04:04 soft.txt -> original.txt
$ rm original.txt
$ cat hard.txt
data
$ cat soft.txt
cat: soft.txt: No such file or directory

How it works: what survives a delete

Delete the original and a hard link keeps working, because the data lives until the link count reaches zero. A symbolic link dangles and points at a path that is no longer there. Also: hard links cannot cross filesystems, while symbolic links can and may point at a directory.

Common mistakes

ln -s target.txt sub/link.txt builds a link that looks for target.txt inside sub, not in the directory you were standing in. ls -l sub/link.txt shows link.txt -> target.txt and looks right, but cat sub/link.txt answers cat: sub/link.txt: No such file or directory. The stored path is read from the link's own directory. Give an absolute path, write ln -s ../target.txt sub/link.txt, or let ln -sr target.txt sub/link.txt work the relative path out.

Repointing a symlink that leads to a directory needs -n. With cur -> d1, the command ln -sf d2 cur follows cur into d1 and drops the new link there: cur still points at d1, and d1 now holds a broken d2 -> d2. Not one line of error is printed. ln -sfn d2 cur replaces the link itself, and cur -> d2.

du counts the data once for all hard links. For a 10 MB file with two names, du -sh big.bin big2.bin prints a single line, 10M big.bin, and skips the second name without a word, while du -shl prints both lines at 10M. For the same reason rm big.bin frees nothing: the blocks stay until the link count in the second column of ls -l reaches zero.

Used in

You need this in the lesson Inodes and links.