Skip to main content

Symbolic Link

A symbolic link (symlink) is a special file that stores the path of another file or directory. Opening the symlink transparently opens its target. Unlike a hard link, it can cross filesystems and point to directories.

A symlink is a shortcut. It has its own inode, and its content is just a path string. The classic production use is a current link that points at the active release directory, so switching versions means repointing one link instead of editing every config.

◈ DIAGRAM
/opt/app/current -> /opt/app/v2.0/
index.html config.yaml bin/
TEXT
Symbolic link Hard link
Own inode Same inode as target
Can cross filesystems Cannot cross filesystems
Can point to directories Normally files only
Breaks if target is removed Data survives while any link remains
ls shows l and the target Looks like a regular file
Bash
ln -s /opt/app/v2.0 /opt/app/current
ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite
ls -la /opt/app/current
readlink /opt/app/current # target as stored
readlink -f /opt/app/current # fully resolved absolute path
find /etc -type l # all symlinks
find /etc -xtype l # broken symlinks
rm /opt/app/current # removes the link, not the target
[ -L /opt/app/current ] && echo "is a symlink"

ln -sf replaces a link by removing and recreating it, which leaves a tiny window with no link. For a truly atomic swap, create a temporary link and rename it over the old one.

Bash
SEMVER="v3.0.1"
ln -sfn "/opt/app/${SEMVER}" /opt/app/current.tmp
mv -T /opt/app/current.tmp /opt/app/current # rename is atomic
# Rollback: repoint to the previous release the same way
Symptom Command What to Look For
Link not working readlink -f link Target missing
Loop readlink -f link Too many levels of links
Dangling links find /etc -xtype l Broken targets
Odd size in ls stat link Size is path length
Common Mistake

Creating a relative symlink from the wrong directory. A relative target resolves from the link's own location, not your current directory, so ln -s ../shared/config.yaml link can break when the link moves.

Tip

ln -sfn with -n stops ln from descending into an existing directory symlink and creating a nested link by accident.

Remember

Removing a symlink with rm deletes only the link. Add a trailing slash carefully, because rm link/ can behave differently from rm link.

Frequently Asked Questions

What happens if I delete the file a symlink points to?

The symlink remains but dangles. Following it returns "No such file or directory". A hard link behaves differently because the data is kept until every hard link is removed.

When should I use a symlink instead of a hard link?

Use a symlink to link across filesystems, to link directories, or for versioned release layouts such as `current -> releases/v1.2.3`. Use a hard link when you want a second name for the same file data on one filesystem.

Are symlink swaps safe for zero-downtime deploys?

Swapping with a temporary link and `mv -T` is atomic, so readers see either the old or the new target. A plain `ln -sf` can briefly leave no link.

Found a mistake, an outdated step or a broken link?