Symbolic Links vs Hard Links: ln -s, Inodes, stat, and readlink
One inode with two names versus a file containing a path. Link counts, argument order, the -n flag that decides where a link lands, broken links, and the symlink attacks that follow from careless resolution.

Quick Summary
A hard link is a second name for the same inode. Both names are equal, there is no original, and the file's data disappears only when the last link is removed — which is why link count in ls -l matters. A symbolic link is a small file of its own whose content is a path; resolving it costs an extra lookup, works across filesystems, can point at directories, and can dangle when its target is deleted or moved.
The practical differences follow from that design. Hard links cannot cross a filesystem boundary and cannot normally be created for directories, because a directory's .. entry must have one unambiguous parent. Symlinks can point anywhere, including to files that do not exist yet, and every program that opens them has to decide whether to follow the final component. ln -s TARGET NAME puts the target first, which is the reverse of cp and the source of the "my link points at nothing" mistake.
stat, readlink, and realpath are how you see which case you are in: stat -c '%i %h %F' shows the inode number, link count, and file type; readlink -f canonicalizes a chain into an absolute path; realpath --relative-to computes a portable relative target. This guide covers link creation, resolution order, hard-link counts, broken and looping links, archive and container behaviour, and the security consequences of following a link you did not create.
Basic Syntax & Core Signatures
# Symlink: target first, then the name of the link to create
ln -s /srv/app/current/config.yml ./config.yml
ln -s ../shared/data ./data # relative target, resolved from the link's dir
# Replace an existing link (and only a link) atomically-ish
ln -sfn /srv/app/release-2 ./current # -n: do not dereference current/ as a directory
# Hard link: same inode, same filesystem
ln report.txt report-copy.txt
# Inspect
ls -li report.txt report-copy.txt # same inode number, link count 2
stat -c '%i %h %F %n' report.txt report-copy.txt
stat -c '%i %h %F %N' ./current ./config.yml
readlink ./config.yml # prints the stored target, one level only
readlink -f ./config.yml # canonical absolute path through all links
readlink -e ./config.yml # same, but empty output + non-zero if broken
readlink -m ./a/b/c # canonicalize even if components are missing
realpath --relative-to=/srv/app ./current/config.yml
# Find broken links and links in general
find . -xtype l # broken symlinks (target type differs)
find . -type l -printf '%p -> %l\n' # every link with its target
find . -samefile report.txt # every name sharing the inode
# Ownership and permissions
chown -h deploy:deploy ./config.yml # change the link, not the target
chmod 600 ./config.yml # follows the link; changes the targetResolution rules worth memorizing:
ln -s TARGET LINKNAME # create a link called LINKNAME pointing at TARGET
cp TARGET LINKNAME # copies TARGET's content to LINKNAME (opposite order)
ln -s file link -> link contains "file" (relative to link's directory)
ln -s /tmp/file link -> link contains "/tmp/file" (absolute)
rm link removes the link; rm -r link/ follows into the target directory
cp -a preserves links as links; cp -L copies the data (dereferences)
cp -P copies the link itself (GNU: -d = --no-dereference --preserve=links)Key Comparison Table
| Aspect | Hard link | Symbolic link |
|---|---|---|
| Stored as | Another directory entry for one inode | A new inode whose data is a path string |
| Crosses filesystems | No (same device only) | Yes |
| Can point to a directory | No (root-only exceptions, discouraged) | Yes |
| Survives deleting the "original" name | Yes, data stays while any link exists | No, becomes broken |
| Relative to its own location | N/A | Yes — resolved from the link's directory |
| Broken target possible | No | Yes |
ls -l shows | Same size, link count > 1 | l type and name -> target |
| Inode number | Identical to the target | Different from the target |
| Permissions | Shared with the inode | Ignored (mode 777 on Linux, not used) |
| Space cost | One directory entry | One inode plus the target string |
| Delete behaviour | Data freed when nlink reaches 0 | Target untouched; link remains, broken |
| Typical use | Deduplicated snapshots (rsync --link-dest), build caches | current pointers, version switching, config indirection |
| Command | What it reports |
|---|---|
stat -c '%h' f | Hard-link count of the inode |
stat -L -c '%F' f | Type of the resolved target |
readlink f | Raw stored target of a symlink |
readlink -f f | Absolute, fully resolved path (empty if a component is missing) |
readlink -e f | Same but fails silently on missing components |
realpath f | Canonical path, similar to readlink -f |
namei -l path | Every component with its permissions, resolving each link |
find -L . -type f | Traverse symlinks as real directories |
Practical Edge Cases & Deep Dives
1. The inode and link count, seen from the filesystem
$ echo hello > report.txt
$ ln report.txt copy.txt
$ ls -li report.txt copy.txt
1311482 -rw-r--r-- 2 user user 6 Nov 15 07:10 copy.txt
1311482 -rw-r--r-- 2 user user 6 Nov 15 07:10 report.txt
$ rm report.txt
$ cat copy.txt # hello — the data was never in the name
$ stat -c '%h' copy.txt # 1The count of 2 is the kernel's guarantee that the last name to be removed is the one that frees the blocks. Editing through either name edits the single shared inode, so a hard link is not a backup: sed -i on one name may replace the inode entirely (GNU sed writes a temp file and renames), leaving the other name pointing at the old content, which surprises people in both directions.
2. ln -sf into a directory, and the -n flag
$ ls -ld current
drwxr-xr-x 2 deploy deploy 4096 ... current # a real directory now
$ ln -sf /srv/app/release-2 current
$ ls -ld current
drwxr-xr-x ... current/ # unchanged: the link went INSIDE it
$ ls -l current/release-2 # created /srv/app/release-2 inside current/Without -n/--no-dereference, ln follows an existing directory and creates the link inside it. The fix is ln -sfn TARGET NAME, and the fully safe version replaces via rename so no window exists where current is missing:
ln -sfn /srv/app/release-2 current.new && mv -T current.new currentmv -T treats the destination as a file, so the rename replaces the directory entry atomically — the standard deploy pattern for current -> release-N.
3. Relative versus absolute targets, and why relative wins in deployments
# Absolute: breaks when the tree is mounted elsewhere, as a chroot or in a container
ln -s /srv/app/releases/7 ./current
# Relative: keeps working from the link's own directory
ln -s releases/7 ./current # ./current -> releases/7A link's relative target is interpreted from the link's directory, not from the current working directory, so ln -s ../data link inside /srv/app/x points at /srv/app/data regardless of where you run it. Absolute targets are correct for system paths (/usr/bin/python3 -> python3.11, where the directory is fixed) and fragile for anything that gets copied — a relative link survives rsync/tar into a container or a different mount point. realpath --relative-to generates the relative form without arithmetic:
realpath --relative-to=/srv/app /srv/app/releases/7
# releases/74. Broken links, loops, and how tools treat them
$ ln -s missing.txt dangling
$ cat dangling
cat: dangling: No such file or directory
$ stat dangling # succeeds: the LINK exists
$ stat -L dangling # fails: the target does not
$ find . -xtype l # lists dangling
$ ln -s loop loop # self-referential
$ cat loop
cat: loop: Too many levels of symbolic links # ELOOPTools differ by default: ls -l shows links as links, ls -L follows them, tar stores links as links unless -h/--dereference is given, and rsync -a preserves them while rsync -aL copies the data. find -type l finds the link, find -L -type f finds the target — the -xtype test is the one that compares against the target type, which is how you find links that point at the wrong kind of thing. A restore that used -h turns a current -> releases/7 pointer into a full copy, silently doubling disk usage and breaking the next deploy.
5. Symlink attacks: what "follow the link" costs
A symlink in a directory another user can write to is an attack primitive: ln -s /etc/shadow /tmp/upload turns a careless open("/tmp/upload", O_WRONLY|O_CREAT) into a write to a protected file. The kernel services that matter use O_NOFOLLOW on the final component, openat with directory file descriptors, sticky-bit directories (/tmp with 1777), and fs.protected_symlinks=1 (the systemd default), which blocks following a link in a sticky world-writable directory when the link's owner and the directory's owner differ.
Application-level rules that hold regardless of sysctl settings: never chmod/chown a path you did not create without checking lstat, write through a temp file and rename inside the final directory, and treat realpath output as the only path worth logging. For archive extraction, tar --no-overwrite-dir and extracting as a non-root user are the cheap mitigations against the ../ and absolute-link tricks in malicious archives.
6. chmod, chown, and rm on a link
$ chmod 600 link # changes the TARGET's mode, not the link's
$ chmod -h 600 link # GNU coreutils: affects the link itself (Linux ignores)
$ chown -h deploy link # changes the link's owner only
$ chown deploy link # changes the target's owner
$ rm link # removes the link, target intact
$ rm -r link/ # trailing slash: operates on the target directory
$ unlink link # same as rm, no directory checkThe trailing slash on rm -r link/ is the dangerous one, because the kernel resolves the link before the final component. find . -type l -delete is the precise way to remove a batch of links without touching what they point at.
Common Runtime & Configuration Mistakes
| Symptom | Cause | Fix |
|---|---|---|
ln: failed to create hard link: Invalid cross-device link | Target and link on different filesystems | Use a symlink, or link within one filesystem |
ln: failed to create symbolic link 'x': File exists | Name already exists | ln -sfn TARGET LINK (add -n for directories) |
| New link appears inside an existing directory | -n missing and the name is a directory | ln -sfn |
| Link points at nothing | Arguments reversed (ln -s link target) | ln -s TARGET LINK, verify with readlink |
| Relative link breaks after a move | Relative target resolved from the old directory | Absolute target, or rebuild with realpath --relative-to |
cp -r copied data instead of the link | cp dereferences by default | cp -a / cp -P / cp -d |
| Archive restore duplicated data | tar -h or rsync -L dereferenced links | Preserve links; dereference only deliberately |
chmod changed the wrong file | chmod follows links | chown -h, lstat before acting |
rm -rf dir/ deleted the target's contents | Trailing slash forces resolution | rm dir (no slash) for links |
Too many levels of symbolic links | Loop in the chain | namei -l, readlink -f to find the cycle |
| Backup tool reported a file but restore is empty | Dangling link stored and restored | Check with find -xtype l before and after |
| Link count never drops to 1 | Extra hard links in another directory | find / -samefile file |
Hard links and symlinks are both indirection, but they sit at different layers: a hard link is the filesystem naming one inode twice, and a symlink is a file whose content is a path that every consumer must resolve. Choosing correctly is mostly a question of whether the name must survive a move (hard link), cross a filesystem (symlink), or point at something that does not exist yet (symlink) — and the rest is remembering that the target comes first and that -n, -h, and a trailing slash each change which object a command acts on.
Try matching browser tools
All toolsSmall utilities that fit Linux work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Linux guides
All Linux guidesBash Indexed Arrays, Associative Maps, and Parameter Expansion
Quoting decides whether an array stays a list, and the expansion operators replace most external text tools. Includes set -u, mapfile, namerefs, and the bash 3.2 differences that break scripts on macOS.
Parsing JSON and YAML in Shell Pipelines with jq and yq
jq is a filter language, not a pretty-printer. The operators that do the work, --arg versus string interpolation, in-place YAML edits, and how to tell which yq is installed before writing a command.
Linux Memory, Swap, the OOM Killer, vmstat, and sysctl Tuning
MemAvailable versus used, the vmstat columns that prove thrashing, how the OOM killer picks its victim, and the sysctl keys that belong in /etc/sysctl.d instead of a one-off command.
Linux Firewalls: ufw, iptables, and nftables Rules and Persistence
First-match ordering, stateful filtering, and persistence: the three ideas behind every Linux firewall. Plus why Docker-published ports ignore ufw and how DOCKER-USER closes them.
More in Linux
All Linux guidesThe guides filed next to this one in Linux, in library order.