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.
Understanding Symbolic Links
What Is a Symbolic Link in Linux?
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.
/opt/app/current -> /opt/app/v2.0/ index.html config.yaml bin/Symbolic Link vs Hard Link
Symbolic link Hard linkOwn inode Same inode as targetCan cross filesystems Cannot cross filesystemsCan point to directories Normally files onlyBreaks if target is removed Data survives while any link remainsls shows l and the target Looks like a regular filePractical Symlink Commands
ln -s /opt/app/v2.0 /opt/app/currentln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/mysite ls -la /opt/app/currentreadlink /opt/app/current # target as storedreadlink -f /opt/app/current # fully resolved absolute path find /etc -type l # all symlinksfind /etc -xtype l # broken symlinks rm /opt/app/current # removes the link, not the target[ -L /opt/app/current ] && echo "is a symlink"How to Switch a Symlink Atomically
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.
SEMVER="v3.0.1"ln -sfn "/opt/app/${SEMVER}" /opt/app/current.tmpmv -T /opt/app/current.tmp /opt/app/current # rename is atomic # Rollback: repoint to the previous release the same wayTroubleshooting Symlinks
| 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 MistakeCreating 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 linkcan break when the link moves.
Tip
ln -sfnwith-nstops ln from descending into an existing directory symlink and creating a nested link by accident.
RememberRemoving a symlink with
rmdeletes only the link. Add a trailing slash carefully, becauserm link/can behave differently fromrm 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.