ln
Create hard or symbolic links between files
By CMD Script Team · 5 min read · Last updated
ln [OPTIONS] TARGET LINK_NAMEOptions
| Flag | Description |
|---|---|
-s | Create a symbolic (soft) link instead of a hard link |
-f | Force the link creation, removing an existing destination file first |
-v | Print a message for each link created |
-n | Treat a symbolic link destination that is a directory as a normal file (don't follow it) |
-i | Prompt before removing an existing destination file |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
ln creates links between files. By default it creates a hard link — an additional
directory entry pointing to the exact same underlying inode/data as the original file.
With -s, it instead creates a symbolic (soft) link — a small special file that stores
a path pointing at another file or directory by name.
Beginner examples
ln -s /path/to/original linkname— create a symbolic link (the common, everyday case)ln original.txt hardlink.txt— create a hard link to a file (no-s)ls -l linkname— inspect a symlink; it showslas the file type and displayslinkname -> /path/to/originalln -sf newtarget linkname— force-replace an existing symlink to point somewhere new
ln -s /opt/app/current/config.yaml /etc/app/config.yaml
Advanced examples
- Symlink an entire directory so multiple paths resolve to the same tree, commonly used
for "current version" pointers:
ln -sfn /opt/releases/v2.3 /opt/releases/current(-nensures it replaces the symlink itself rather than following it into the old target directory). - Use hard links to deduplicate identical files on the same filesystem without using
extra disk space (each hard link shares the same inode/data blocks):
ln original.iso backup.iso. - Inspect a file's link count to see how many hard links reference the same inode:
ls -l file.txt(the number after the permissions is the hard link count; anything above 1 for a regular file means other hard links exist). - Chain symlinks to build flexible "latest version" deployment pointers that a running service can reference via a stable path even as the underlying target changes.
ln -sfn /opt/releases/v2.3 /opt/releases/current
Common mistakes
- Trying to create a hard link across filesystems (e.g. from
/to a separately mounted/mnt/data) and getting "Invalid cross-device link" — hard links require target and link to share the same filesystem/inode table; use a symlink instead. - Deleting a symlink's target and being surprised the symlink still "exists" but is now
broken (dangling) —
ls -lwill show it in a different color or flag it, and accessing it returns "No such file or directory." - Assuming removing the original file also breaks a hard link — it doesn't; the data persists as long as at least one hard link (the link count) still references it.
- Forgetting
-nwhen replacing a symlink that points to a directory — without it,ln -sfcan end up creating the new link inside the old target directory instead of replacing the symlink itself.
Tips
- Use
ls -lito see inode numbers directly — files sharing the same inode number (on the same filesystem) are hard links to the same data. - Prefer symbolic links for pointing across filesystems, to directories, or when you
want the link to visibly show its target path in
ls -l. - Use
ln -sfn(force, no-dereference) when updating a "current"/"latest" symlink that points at a directory, to avoid accidentally nesting the new link inside the old target.
Best practices
- Use symbolic links for "current version" or configuration pointers in deployment
setups (e.g.
current -> releases/v2.3) since they're easy to inspect, retarget, and work across filesystems. - Reserve hard links for same-filesystem deduplication scenarios where you specifically want two names to be indistinguishable and equally authoritative — understand that there's no "original" once created.
- Always use
-fand-ntogether when scripting symlink updates to a directory target, to avoid the common trap of nesting instead of replacing.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Symlinking a versioned release directory to a stable
currentpath so a running service always points at the active deployment, and cutovers/rollbacks are just a symlink swap:ln -sfn /opt/releases/v2.4 /opt/releases/current. - Linking a single shared configuration file into multiple locations that different tools expect to find it at, without duplicating the file's content.
- Using hard links for space-efficient backups (as tools like
rsync --link-destand some backup utilities do) — unchanged files between backups share the same inode rather than being duplicated.
Common interview questions
- What's the core difference between a hard link and a symbolic link? A hard link is another directory entry pointing to the same inode/data as the original, with no concept of an "original" once created; a symbolic link is a separate small file storing a path/pointer to the target by name, which breaks if that path stops resolving.
- Why can't hard links cross filesystems, while symlinks can? Hard links reference inode numbers, which are only unique within a single filesystem; symlinks just store a text path, which works regardless of which filesystem the target is on.
- What happens to a hard-linked file's data if you delete the original filename? Nothing is actually freed — the underlying data persists as long as the link count (number of hard links referencing that inode) is greater than zero; only deleting the last remaining hard link frees the data.
Frequently Asked Questions
What's the fundamental difference between a hard link and a symbolic link?
A hard link is another directory entry pointing to the exact same inode (the same underlying data on disk) as the original — there's no 'original' vs 'copy' distinction; both names are equally real. A symbolic link is a separate small file that simply stores a path/pointer to another file by name; if that target path is removed or renamed, the symlink breaks (dangles).
Why can't I create a hard link across filesystems, but I can create a symlink?
Hard links work by referencing an inode number, and inode numbers are only unique within a single filesystem — they can't reach across mount points. A symbolic link just stores a text path, which works regardless of which filesystem the target lives on, so it has no such restriction.
Why does deleting the original file not break a hard link, but does break a symlink?
A hard link doesn't point to a 'file' as a name at all — it points to the same inode/data as the original. The data isn't actually freed until every hard link (the link count) referencing that inode is removed. A symlink only stores the original's path/name, so once that path no longer resolves, the symlink is left dangling.
Can you create a hard link to a directory?
Generally no — most Unix-like systems disallow hard links to directories (with the exception of the automatic . and .. entries) to prevent filesystem loops and inconsistencies. Symbolic links to directories are fully supported and commonly used instead.
Cheat sheet
Download a quick-reference cheat sheet for ln.