ln Command Guide for Creating Links Between Files
This guide helps you understand and use the ln command, which creates links — pointers from one file path to another.
What is ln, and Why Use It?
The ln command creates a link: a new directory entry that points at an existing file or directory. There are two kinds. A symbolic link (symlink, created with -s) is a standalone file that stores the path of its target, like a shortcut. A hard link (the default) is another name for the same data on disk, indistinguishable from the original.
Why Learn ln?
- Reference shared files once: Update software in one place and every symlink sees the change.
- Shorter paths:
ln -s /var/www/site/releases/current publickeeps URLs stable across deployments. - Version management: Symlink
currentto the newest release so rollbacks are onelnaway. - Understand the system: Package managers and
/etc/alternativesrely heavily on symlinks.
lnis part of the base system on Linux and macOS. There is nothing to install.
1. Create a Symbolic (Soft) Link
ln -s target.txt link.txt
-smakes a symbolic link namedlink.txtthat points attarget.txt.
Example:
ln -s /etc/nginx/nginx.conf ~/nginx.conf
ls -l ~/nginx.conf
lrwxrwxrwx 1 alice alice 22 Dec 4 10:00 /home/alice/nginx.conf -> /etc/nginx/nginx.conf
- The
lat the start of the permissions and the->arrow confirm it is a symlink.
2. Create a Hard Link
ln target.txt hardlink.txt
- Without
-s,lncreates a hard link: a second name for the same inode.
ls -i target.txt hardlink.txt
-iprints the inode number; both files show the same number, proving they share the data.
3. Link Into a Directory
ln -s ~/projects/app ~/current-app
- When the second argument is an existing directory, the link is created inside it with the target’s basename.
ln -s /opt/app/releases/v2 /opt/app/current
- The classic release-symlink pattern: point
currentatv2, and update it on each deploy.
4. Create Links for Multiple Targets
ln -s ~/Documents/*.pdf ~/pdf-links/
- A symlink is created for each matching file inside the target directory.
5. Force-Replace an Existing Link
ln -sf new-target.txt link.txt
-f(force) removes an existinglink.txtbefore creating the new link.
ln -sfn /opt/app/releases/v3 /opt/app/current
-ntreats an existing symlink-to-directory as a normal file, so the link itself is replaced instead of creating a link inside the old target. This combination is the safe way to repoint acurrentsymlink.
6. Inspect and Remove Links
ls -l link.txt
- Shows what the link points to.
readlink -f link.txt
- Resolves the link to its final absolute target, following chains.
rm link.txt
- Removes the link itself, never the target. (Never use
rm -ron a symlinked directory unless you intend to follow it.)
Common Pitfalls
- Relative vs absolute targets.
ln -s ../shared/config.yml linkstores the path as written. If you move the link, relative targets can break. Absolute targets are more robust. - Deleting a symlink does not delete the target.
rm linkis safe; the original file stays. - Hard links have limits. They cannot cross filesystems and, on most systems, cannot point at directories.
- Overwriting a symlink to a directory. Without
-n,ln -sfmay create the new link inside the old target directory. Always useln -sfnwhen repointing. - Broken links are visible.
lsshows a symlink in red (or with a?inls -l) when its target is missing.
Related Commands
| Command | Description |
|---|---|
cp | Copy data, which symlinks do not |
rm | Remove a link without touching its target |
readlink | Print what a symlink points to |
stat | Show inode and link-count details |
chmod | Symlink permissions are always rwxrwxrwx; the target’s permissions govern access |
ln Cheatsheet
| Command | Description |
|---|---|
ln -s target link | Create a symbolic link |
ln target hardlink | Create a hard link |
ln -s dir/ link | Create the link inside dir/ |
ln -sf target link | Replace an existing link |
ln -sfn newdir link | Repoint a symlink-to-directory safely |
ln -s a b c dir/ | Link multiple targets into a directory |
readlink link | Show the link target |
readlink -f link | Resolve to the final absolute path |
ls -l link | Inspect link type and target |
ls -i file | Show the inode number (hard links share it) |