Where to start First steps track · 15 stages
Linux Labs

ln — symbolic and hard links

4 min GNU coreutils

Symlinks (ln -s) and hard links: how they differ, when to use which, common mistakes with relative paths.

Cheat sheet

5 commands Printable version
  • ln -s /opt/app/current/bin/app /usr/local/bin/app symlink (order like cp)
  • ln file hardname a hard link
  • find . -xtype l find broken symlinks

Overview

A link is a second name for a file. There are two kinds:

  • Symbolic (symlink, ln -s) — a pointer to a path. It can point to a directory, to another filesystem, and can be "hanging" (broken, if the target was removed). This is the common case.
  • Hard link — another name for the same inode (the same data). Only within one filesystem, only for files. The file exists as long as at least one name remains.

Options

6

-s is the one option that makes a symlink instead of a hard link; without it ln creates a hard link. -f overwrites an existing link name — without confirmation, so it's risky with someone else's or important files. -n matters specifically when replacing a symlink that points to a directory: without it, ln -sf target dir drops the new link inside dir instead of replacing dir itself.

Option What it does Example
-s --symbolic Create a symbolic link instead of a hard one. ln -s target link
-f --force Remove an existing destination file and recreate the link. dangerous ln -sf target link
-n --no-dereference When replacing a symlink to a directory, do not follow it — replace the symlink itself. ln -sfn target linkdir
-v --verbose Print each link that is created. ln -sv target link
-r --relative Create a symbolic link with a path relative to the link's location. ln -sr target link
-T --no-target-directory Treat the destination as a normal file, even if it is a directory. ln -sT target name

Nothing found. Try a shorter query.

Recipes

2

Examples

bash
ln -s /opt/app/current/bin/app /usr/local/bin/app   # symlink (target → link name)
ln -s ../shared config                              # a relative symlink
ln -sf /new/target link                             # -f: recreate an existing link
ln -sfn /new/dir linkdir                            # -n: don't "step into" a symlinked directory when replacing it
readlink link                                       # where the symlink points
readlink -f link                                    # the absolute path after all symlinks
ls -l link                                          # shows the arrow: link -> target

# a hard link (same content, same inode):
ln file hardname
stat -c '%i %h' file hardname                       # same inode, link count 2

Argument order is like cp: target first, then the link name (ln -s target name).

Symlink vs hard

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

For configs, app versions, systemd's /etc/...wants/ — it's almost always symlinks.

Troubleshooting

3

A broken (dangling) symlink

The target was moved or deleted. ls -l shows it in red, readlink -f returns empty or an error. Find every broken one: find /path -xtype l.

A relative symlink "broke" after a move

A relative path is resolved from the link's own directory, not the current one. If you move the link, recompute the target or use an absolute path.

ln -s into an existing directory created the link INSIDE it

If name is an existing directory, ln -s target dir drops the link at dir/target. To replace the symlinked directory itself, add -n (ln -sfn target dir).

See also

  • files-basics — Navigation and files — ls, cd, cp, mv, rm, cat, less, head, tail, copy-paste in the terminal.
  • stat — Full information about a file: size, permissions, owner, timestamps (atime/mtime/ctime), inode.
  • systemd — The init system and service manager (PID 1): units, targets, dependencies, logs.
Loading…