On this page
- What is the difference between a symbolic and a hard link?
- How do you create a symbolic link?
- Should a symlink use an absolute or a relative path?
- How do you switch a link to a new target in one step?
- How do you create and inspect hard links?
- How do you remove a link without touching the target?
- How do you find links and broken links?
- How do links interact with copying, archives and permissions?
- Why does my link not work?
- Questions
What is the difference between a symbolic and a hard link?
A symbolic link (symlink) is a tiny file that stores a path; opening it opens whatever sits at that path. A hard link is another directory entry pointing at the same inode, so both names are equally real and the data stays until the last name is removed.
- Symlinks can point to directories, to other filesystems and to paths that do not exist yet. If the target moves, the link breaks.
- Hard links work only for files (not directories) and only within one filesystem. Moving or deleting the original name does not affect the other.
- Permissions shown for a symlink are always
lrwxrwxrwx; the target file's own permissions are what count.
Day to day on a server, symlinks are by far the more common tool.
How do you create a symbolic link?
Give ln -s the target first and the link name second. A handy way to remember: the order is the same as cp, existing thing first.
ln -s /var/log/nginx ~/nginx-logs
sudo ln -s /etc/nginx/sites-available/app.example.net /etc/nginx/sites-enabled/
ls -l ~/nginx-logs
readlink ~/nginx-logs
readlink -f ~/nginx-logsWhen the link name is an existing directory, ln places the link inside it with the target's name, as in the second line. readlink prints the stored path; readlink -f resolves every step to the final real path. On Rocky and Alma, Nginx uses /etc/nginx/conf.d instead of the sites-enabled pattern, so symlinked configs are mostly a Debian family habit.
Should a symlink use an absolute or a relative path?
Use an absolute path when the link and target live in unrelated places, and a relative path when they move together, for example inside a project folder you copy between servers.
cd /srv/app
ln -s releases/v2 current # relative, stored as "releases/v2"
ln -sr /srv/app/shared/.env /srv/app/releases/v2/.env # -r computes the relative path
ls -l /srv/app/releases/v2/.envA relative target is resolved from the directory where the link lives, not from where you ran ln. The -r flag of GNU ln saves you from counting ../ steps.
How do you switch a link to a new target in one step?
Use ln -sfn: -f replaces the old link, and -n treats an existing link to a directory as a file, so ln does not create a new link inside the old target directory.
ln -sfn /srv/app/releases/v3 /srv/app/current
# fully atomic swap, no moment without a link
ln -s /srv/app/releases/v3 /srv/app/current.tmp
mv -T /srv/app/current.tmp /srv/app/current
readlink /srv/app/currentln -sfn removes and recreates the link, which leaves a split second with no link. The rename with mv -T is a single atomic operation, which matters when a web server is reading the path under load. This release-folder pattern makes rollbacks one command: point current back at the previous release. See mv for more on renames.
How do you create and inspect hard links?
Run ln without -s. Both names then share an inode number and a link count.
echo "shared data" > ~/original.txt
ln ~/original.txt ~/second-name.txt
ls -li ~/original.txt ~/second-name.txt
stat -c '%i %h %n' ~/original.txt
find ~ -samefile ~/original.txtThe first column of ls -li is the inode, and the number after the permissions is the link count. Editing through either name changes the same data. Hard links are useful for space-saving snapshot schemes, such as rsync --link-dest backups (covered in the rsync guide), where unchanged files cost no extra disk.
How do you remove a link without touching the target?
Delete the link name with rm or unlink. For a symlink to a directory, never add a trailing slash, or tools may act on the directory contents instead of the link.
rm ~/nginx-logs # removes only the link
unlink /srv/app/current # same, one file at a time
# dangerous: rm -r ~/nginx-logs/ would delete files inside /var/log/nginxRemoving a hard link name simply lowers the link count; the data stays as long as one name (or an open file handle) remains.
How do you find links and broken links?
Use find with -type l for symlinks, and -xtype l for symlinks whose target is missing.
find /etc -maxdepth 2 -type l -ls
sudo find /etc /srv -xtype l
sudo find /srv -xtype l -delete # after reviewing the list
find / -xdev -type f -links +1 2>/dev/null | headThe last command lists regular files with more than one hard link. More search options are in find and locate.
How do links interact with copying, archives and permissions?
Tools treat symlinks differently, so check the flags when backing up or copying.
cp -aandrsync -acopy symlinks as links;cp -Landrsync -Lcopy the target content instead.tarstores links as links unless you pass-h.chownfollows a link unless you add-h; see ownership with chown.- The kernel setting
fs.protected_symlinks(on by default) stops processes from following symlinks in world-writable sticky folders such as/tmpwhen the link owner differs. That blocks a classic attack, so leave it on.
A web server also needs execute permission on every directory along the real target path, not just on the link.
Why does my link not work?
- "No such file or directory" on a link that exists: the target is missing or the relative path resolves from the wrong folder. Compare
readlinkwithreadlink -f. - "Invalid cross-device link": you tried a hard link across filesystems. Use a symlink.
- Link appeared inside the old folder: you replaced a directory link without
-n. Remove the stray link and repeat withln -sfn. - Too many levels of symbolic links: two links point at each other. Fix one of them.
- Permission denied through the link: fix permissions on the target, not the link.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
| Plan | Fits because | Size | Monthly | Order from |
|---|---|---|---|---|
| S1-2 | release folders and links need very little space | 1 vCPU, 2 GB RAM, 55 GB SSD | $6.00/mo | $72.00 for 12 months |
Questions about Symbolic and hard links
Can I make a hard link to a directory?
No. Linux forbids it to keep the directory tree free of loops. Use a symlink or a bind mount instead.
Does deleting the target delete the symlink?
No. The link stays and becomes broken (dangling) until the target returns or you remove the link.
How do I see where /usr/bin/python3 really points?
Run readlink -f /usr/bin/python3. On Debian systems many commands also pass through /etc/alternatives, which this reveals.
Do symlinks use disk space?
Barely. Short targets are stored inside the inode itself, so most symlinks use no data blocks at all.
Is a symlink or a bind mount better for an app data folder?
A symlink is simpler and fine for most apps. A bind mount helps when software refuses to follow links or runs in a restricted service sandbox.
More in Linux command line
- Copy files and directories with cp
- Move and rename with mv
- Create directories and empty files
- List files with ls, find your place with pwd
- Read files with cat, less and tail
- Find files with find and locate
- Search text with grep
- Edit and report text with sed and awk
- echo, tee and wc
- Edit files with nano
- Edit files with Vim
- Read manual pages
- Users and groups
- Ownership with chown and chgrp
- Pipes, redirection and xargs