Where to start First steps track · 15 stages
Linux Labs

Symbolic and hard links

4 min Linux

The difference between a symlink and a hard link, when to use which, broken links.

Cheat sheet

7 commands Printable version
  • ln -sfn target linkdir recreate a symlink (to a directory — needs -n!)
  • find . -xtype l broken symlinks
  • cp -a src dst archive mode: links stay as links

Overview

There are two different kinds of "link" (the command is ln):

  • Symbolic (symlink) — a pointer to a path. Like a shortcut. It can point to a directory or across filesystems; it breaks if the target is deleted or moved.
  • Hard link — a second name for the same inode (the same data). Limited to one filesystem; the file lives as long as at least one name points to it.
bash
ln -s /path/to/target linkname    # symbolic (the one you'll use most often)
ln /path/to/target hardname       # hard
ls -l                             # a symlink shows as link -> target

Recipes

2

Commands

bash
ln -s /opt/app/releases/v5 /opt/app/current     # symlink "current" to a release
ln -sfn /opt/app/releases/v6 /opt/app/current   # switch atomically (-f -n)
readlink current                  # where it points (one hop)
readlink -f current               # the real path after all symlinks
ls -l current                     # current -> /opt/app/releases/v6
find . -xtype l                   # find BROKEN symlinks (target doesn't exist)
find /path -type l -ls            # every symlink with its target
stat file                         # "Links:" — the hard-link count on the inode

-sfn matters when replacing a symlink to a directory: without -n, ln drops the new link INSIDE the existing target directory (see ln).

Symlink vs hard

Symlink (-s) Hard
Points to a directory yes no
Across filesystems yes no
Target deleted "broken" data survives as long as any name remains
Shown in ls -l -> target looks like a regular file
Own inode yes no (shares the file's)

Use cases: symlink — a "current → release" deploy pattern, switching configs, shared libraries. hard link — deduplication within a filesystem (snapshot backups, see rsync --link-dest).

Troubleshooting

4

A broken symlink (points "nowhere")

The target was deleted or moved. Find it with find . -xtype l. Recreate it pointing at the right target (ln -sf). ls highlights broken ones in red.

You copied something and got the link itself (or the other way around)

cp follows a symlink by default (it copies the target). cp -P keeps the link itself; cp -a/-d do it archive-style (links stay as links). tar stores symlinks as-is; tar -h dereferences them.

du/df disagree because of a hard link

Hard links share the same blocks — du counts them once (which is confusing). That's normal; du --count-links counts every name.

rsync "bloated" the backup

Without -H, rsync doesn't preserve hard links (it copies the data again for each name). For deduplicated snapshots, use --link-dest (see rsync).

Terms

2
symbolic link
A separate file that stores a path to another file. If the target is removed or moved, the link becomes dangling. Created with ln -s. Also appears in: pwd, Capabilities: root split into parts, Boot targets: default, multi-user, rescue and emergency, sed and awk and 1 more
hard link
An additional directory entry for the same inode. The file's data is freed once all names are removed and no process holds it open. Hard links work only within one filesystem. Also appears in: ln, ls, namei, sed and awk and 1 more

See also

  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
  • permissions — The Unix permission model — read/write/execute, owner/group/other, special bits.
  • disk-usage — Where the space and inodes went — df, du, finding large files, gotchas.
Loading…