| Tested on | Ubuntu 25.04 (Plucky Puffin) |
|---|---|
| Package | uutils-coreutilsln (uutils coreutils) 0.2.2 |
| Applies to | Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux, SUSE, openSUSE, Alpine |
| Privilege | sudo or root |
| Man page | ln(1) |
| Scope | The ln command creates links in the filesystem. With -s it makes symbolic (soft) links — path shortcuts to files or directories — without duplicating data. |
| Related guides | find Linux commands |
ln — quick reference
Symbolic links (-s)
Create a symlink — a small file that stores a path to another file or directory.
| When to use | Command |
|---|---|
| Create a symbolic link with a custom name | ln -s TARGET LINK_NAME |
| Create a symlink in the current directory (link name = basename of target) | ln -s /path/to/file |
| Create a symlink inside a directory (name = basename of target) | ln -s /path/to/file /path/to/dir/ |
Place the link in a directory (-t) |
ln -s -t /path/to/dir TARGET |
Replace an existing symlink (-f) |
ln -sf TARGET LINK_NAME |
Create a relative symlink (-r) |
ln -sr ../file LINK_NAME |
Print each link created (-v) |
ln -sv TARGET LINK_NAME |
| Symlink to a directory | ln -s /path/to/dir LINK_NAME |
Inspect and manage links
| When to use | Command |
|---|---|
| List links and targets | ls -l LINK |
| Show the stored target path | readlink LINK |
| Resolve to absolute path (follow symlinks) | readlink -f LINK |
| Find symlinks under a tree | find DIR -type l |
| Remove a symlink (target is kept) | rm LINK or unlink LINK |
Hard links (no -s)
Same inode, another name — files only, same filesystem. Omitted from most symlink workflows but listed for contrast.
| When to use | Command |
|---|---|
| Create a hard link to a regular file | ln TARGET LINK_NAME |
| List inode and link count | ls -li TARGET LINK_NAME |
Help and version
| When to use | Command |
|---|---|
| Show brief usage | ln --help |
| Show package version | ln --version |
ln — command syntax
Synopsis from ln --help on Ubuntu 25.04 (ln uutils coreutils 0.2.2):
ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET
ln [OPTION]... TARGET... DIRECTORY
ln [OPTION]... -t DIRECTORY TARGET...Add -s for symbolic links. Without -s, ln creates hard links to the same inode. Symbolic links may hold any path text; relative targets resolve from the link's parent directory.
ln — command examples
Essential Create a symbolic link with ln -s
A symbolic link is a separate file that points at a path. Use it when you want a shortcut — for example exposing a config under /etc while keeping the real file elsewhere.
Run the command:
echo 'original content' > /tmp/ln-lab/dir1/src_file.txt
ln -s /tmp/ln-lab/dir1/src_file.txt /tmp/ln-lab/mylinkVerify with ls -l:
ls -l /tmp/ln-lab/mylinkSample output:
lrwxrwxrwx 1 root root 29 Jul 1 17:50 /tmp/ln-lab/mylink -> /tmp/ln-lab/dir1/src_file.txtThe leading l marks a symlink; the arrow shows the stored target path.
Essential Drop a symlink into another directory
When the second argument is a directory, ln creates a link inside that directory using the basename of the target.
Run the command:
mkdir -p /tmp/ln-lab/dir1 /tmp/ln-lab/dir3
echo 'original content' > /tmp/ln-lab/dir1/src_file.txt
ln -s /tmp/ln-lab/dir1/src_file.txt /tmp/ln-lab/dir3/Verify:
ls -l /tmp/ln-lab/dir3/Sample output:
total 0
lrwxrwxrwx 1 root root 29 Jul 1 17:50 src_file.txt -> /tmp/ln-lab/dir1/src_file.txtThe link name src_file.txt came from the target basename, not from a third argument.
Common Portable relative symlink with -r
Absolute symlinks break when the tree moves. ln -sr builds a relative path from the link location to the target — common in web roots and reproducible package layouts.
Run the command:
mkdir -p /tmp/ln-lab/dir1/dir2
echo 'original content' > /tmp/ln-lab/dir1/src_file.txt
cd /tmp/ln-lab/dir1/dir2
ln -sr ../src_file.txt rel_linkCheck the stored path:
readlink rel_link
ls -l rel_linkSample output:
../src_file.txt
lrwxrwxrwx 1 root root 15 Jul 1 17:50 rel_link -> ../src_file.txtIf you move the whole /tmp/ln-lab tree, a relative link often keeps working; an absolute /tmp/... link would not.
Common Symlink to a directory
Symbolic links can point at directories — unlike hard links, which cannot directory-link on Linux. Accessing the link behaves like accessing the target path.
Run the command:
mkdir -p /tmp/ln-lab/dir1
ln -s /tmp/ln-lab/dir1 /tmp/ln-lab/dir_linkVerify:
ls -ld /tmp/ln-lab/dir_link
ls /tmp/ln-lab/dir_linkSample output:
lrwxrwxrwx 1 root root 16 Jul 1 17:50 /tmp/ln-lab/dir_link -> /tmp/ln-lab/dir1ls through the link lists the same entries as /tmp/ln-lab/dir1.
Common Read and resolve symlink targets
readlink prints the path stored in the symlink. readlink -f walks relative and chained links to a canonical absolute path when the target exists.
Run the commands:
readlink /tmp/ln-lab/mylink
readlink -f /tmp/ln-lab/mylinkSample output:
/tmp/ln-lab/dir1/src_file.txt
/tmp/ln-lab/dir1/src_file.txtUse readlink in scripts when you need the literal target string; use readlink -f when you need the resolved file path.
Common Replace an existing symlink with -f
ln refuses to overwrite by default. -f removes an existing destination link before creating the new one.
Run the command:
ln -sf /tmp/ln-lab/dir1/src_file.txt /tmp/ln-lab/mylink
ls -l /tmp/ln-lab/mylinkSample output:
lrwxrwxrwx 1 root root 29 Jul 1 17:50 /tmp/ln-lab/mylink -> /tmp/ln-lab/dir1/src_file.txtThe target file is untouched — only the symlink inode is replaced.
Advanced Find symbolic links under a path
Audits and cleanups often start with locating symlinks. find -type l lists link paths without following them.
Run the command:
find /tmp/ln-lab -type lSample output:
/tmp/ln-lab/mylink
/tmp/ln-lab/dir_link
/tmp/ln-lab/dir1/dir2/rel_linkBroken links still appear in find output; ls --color may show them in red when the target is missing.
Advanced Remove a symlink without deleting the target
Deleting a symlink removes only the link file. The original target data stays on disk.
Run the command:
rm /tmp/ln-lab/mylink
ls -l /tmp/ln-lab/dir1/src_file.txtSample output:
-rw-r--r-- 1 root root 17 Jul 1 17:50 /tmp/ln-lab/dir1/src_file.txtDo not append / and traverse into the target when removing — rm /path/to/link/ can delete files inside the target directory. Remove the link path itself.
ln — when to use / when not
| Use ln -s when | Use something else when |
|---|---|
|
|
ln — symbolic link vs hard link
Symbolic link (ln -s) |
Hard link (ln) |
|
|---|---|---|
| Inode | New inode storing a path | Same inode as target |
| Directories | Allowed | Not allowed on Linux |
| Cross filesystem | Yes | No |
| Survives target delete | No — link breaks | Yes while any name remains |
| Looks like | l in ls -l, shows -> |
Regular file line, higher link count |
This page focuses on symbolic links. Hard links are a separate pattern for duplicate names to one file body. When you want an independent second copy instead of another name for the same data, reach for cp as shown in create, copy, move and delete files, which also covers how cp -a and mv treat existing links.
ln — interview corner
What is a symbolic link in Linux?
A symbolic link (soft link) is a small filesystem object that stores a path string. When you open the link, the kernel follows that path to the target file or directory — like a shortcut.
ln -s /etc/nginx/nginx.conf /tmp/nginx.conf.link
ls -l /tmp/nginx.conf.linkSample output:
lrwxrwxrwx 1 root root 22 Jul 1 18:00 /tmp/nginx.conf.link -> /etc/nginx/nginx.confSymlinks have their own inode and permissions (lrwxrwxrwx is normal). They can break if the target path is removed.
A strong answer is:
"A symlink is a separate file that holds a path to another file or dir — I create them with ln -s and verify with ls -l or readlink."
What is the difference between a soft link and a hard link?
Soft (symbolic): new inode, stores a path, can point at directories and other filesystems, breaks when the target path disappears.
Hard: another directory entry for the same inode — no stored path, cannot directory-link on Linux, data survives if one name is deleted while another hard link remains.
ls -li original hardlink # same inode number for hard link
ls -l symlink # l bit and -> for soft linkA strong answer is:
"Soft links are path shortcuts with their own inode; hard links are extra names for the same inode. I use -s for symlinks and plain ln only for same-fs file aliases."
Should symlink targets be absolute or relative?
Absolute paths (/var/www/app/config.yml) are clear and work from any current directory when the mount layout is fixed.
Relative paths (../../config.yml) survive when the whole project tree moves — common with ln -sr in repos and containers.
Rule: prefer relative inside relocatable trees; prefer absolute for system paths like /usr/bin/python.
A strong answer is:
"Absolute for stable system locations; relative with ln -sr when the whole directory might move — relative targets resolve from the link's parent dir."
How do you remove a symbolic link without deleting the target?
Remove the link path itself — not the target:
rm /path/to/link
# or
unlink /path/to/linkDo not use rm -r link/ if link points at a directory — that can delete contents inside the target. rm on the symlink path only drops the link inode.
A strong answer is:
"rm or unlink on the symlink path — never traverse into a directory symlink with rm -r unless I mean to delete the target tree."
What is a broken symbolic link?
A broken (dangling) symlink still exists but its stored path no longer resolves — usually because someone deleted or moved the target.
readlink /path/to/broken
ls -l /path/to/broken # may show red in color terminals
find /var -type l ! -exec test -e {} \; -print # locate dangling linksFix by updating the link (ln -sf newtarget link) or restoring the target file.
A strong answer is:
"The link file remains but the target path is gone — I detect with readlink and find, then ln -sf or restore the missing target."
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
File exists |
Destination path already present | Use ln -sf to replace a symlink, or pick a new link name |
No such file or directory when creating hard link |
Hard links require an existing target file | Use ln -s for symlinks; create the target first for hard links |
Invalid cross-device link |
Hard link across filesystems | Use ln -s instead |
| Link works from one cwd only | Relative target computed from wrong directory | Recreate with ln -sr from the link's parent directory |
Too many levels of symbolic links |
Circular symlink chain | Remove the loop with readlink / find -type l |
rm -r link/ deleted data |
Removed through a directory symlink | Remove link without trailing slash; use rm link |

