C.W.K.
Stream
Lesson 09 of 12 · published

Symbolic and Hard Links

~13 min · symlink, hard-link, ln

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Two ways one file lives in two places

Unix lets a single file appear at multiple paths. There are two mechanisms with very different semantics:

  • Hard link — a second name pointing to the same inode. Both names are equal; deleting one leaves the data alive as long as another name exists. Cannot cross filesystems.
  • Symbolic link (symlink) — a small file whose content is a path. The OS follows it transparently. If the target moves or is deleted, the symlink dangles.

Creating links

  • ln source link — hard link.
  • ln -s source link — symbolic link. You'll use this 99% of the time.

If you forget -s, you get a hard link silently. Most developers always type -s reflexively.

Recognising links

ls -l shows symlinks with an arrow: link -> /target/path. Hard links look identical to regular files; ls -i shows the inode number, and two hard links share one. stat link tells you the truth either way.

When each is the right tool

  • Symlink — the default. Cross-filesystem, points to a path, can target directories. Used by Homebrew, dotfile managers, package managers.
  • Hard link — when you want two stable references to the same data, in the same filesystem, with no "target" relationship. Time Machine uses hard links to share unchanged files between snapshots cheaply.

Code

Make and inspect both kinds·bash
echo 'hi' > original.txt
ln    original.txt hard.txt    # hard link
ln -s original.txt soft.txt    # symlink
ls -li *.txt
rm original.txt
cat hard.txt        # still works — same inode
cat soft.txt        # error — dangling symlink
Symlink a config into your home·bash
git clone git@github.com:me/dotfiles ~/dotfiles
ln -s ~/dotfiles/zshrc ~/.zshrc
ls -l ~/.zshrc

External links

Exercise

Make a file, then create both a hard and a soft link to it. Run ls -li; compare inode numbers. Delete the original; check both links. Confirm hard still works, soft is broken.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.