ln Command in Linux: Create Symbolic Links (Soft Links) on Ubuntu

Tested on Ubuntu 25.04 (Plucky Puffin)
Package uutils-coreutils
ln (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

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
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

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):

text
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

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:

bash
ln -sf /tmp/ln-lab/dir1/src_file.txt /tmp/ln-lab/mylink
ls -l /tmp/ln-lab/mylink

Sample output:

output
lrwxrwxrwx 1 root root 29 Jul  1 17:50 /tmp/ln-lab/mylink -> /tmp/ln-lab/dir1/src_file.txt

The target file is untouched — only the symlink inode is replaced.


ln — when to use / when not

Use ln -s when Use something else when
  • You need a shortcut path to a file or directory
  • The link may cross filesystems or point at directories
  • You want a portable tree with ln -sr relative paths
  • Config management expects a symlink (nginx sites-enabled, systemd wants, …)
  • You need another name for the same inode without indirection → hard link (ln without -s)
  • You are mirroring entire directory trees with permissions → cp -a or rsync
  • You need a view of a path without storing a link file → bind mount (mount --bind)
  • The target may be deleted and you cannot tolerate broken links → copy or hard link instead

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 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.

bash
ls -li original hardlink    # same inode number for hard link
ls -l symlink               # l bit and -> for soft link

A 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."


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

References

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)