Examples
Argument order is like
cp: target first, then the link name (ln -s target name).
Symlinks (ln -s) and hard links: how they differ, when to use which, common mistakes with relative paths.
ln -s /opt/app/current/bin/app /usr/local/bin/app
symlink (order like cp)
ln -sf /new/target link
recreate it
readlink -f link
the real path
ln file hardname
a hard link
find . -xtype l
find broken symlinks
A link is a second name for a file. There are two kinds:
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.-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.
Argument order is like
cp: target first, then the link name (ln -s target name).
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.
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 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 itIf 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).
Found an inaccuracy? Report an error in the article