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.
Distinguish a link from its target
Permission, deletion, and copy commands differ in whether they follow a symlink. Inspect with ls -l and readlink, and reject automated links that escape the allowed root.
A hard link is not a backup copy
Both names reference one inode, so modifying data through either name changes the same content. Removing one name leaves the data while another link remains, but it does not preserve an independent historical version.