🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

ln Command

Intermediate File Management man(1)

Create hard and symbolic links between files

👁 13 views 📅 Updated: Mar 15, 2026
SYNTAX
ln [OPTION]... TARGET LINK_NAME

What Does ln Do?

The ln command creates links between files in Linux. There are two types: hard links and symbolic (soft) links. Hard links create an additional directory entry pointing to the same inode, while symbolic links are special files that point to the target path.

Hard links share the same data blocks on disk, so changes to either link affect the same content. They cannot span filesystems or link to directories. Symbolic links are more flexible — they can link across filesystems, to directories, and even to non-existent targets.

Links are essential for system administration, enabling file sharing without duplication, creating convenient shortcuts, managing library versions, and implementing various deployment strategies. Most system commands like /usr/bin utilities are symlinked to their actual locations.

Options & Flags

OptionDescriptionExample
-s Create a symbolic (soft) link instead of a hard link ln -s /etc/nginx/nginx.conf nginx.conf
-f Force — remove existing destination files ln -sf /new/target symlink
-i Interactive — prompt before removing existing files ln -si source link
-n Do not dereference symlink that is a directory ln -sfn /new/dir existing_link
-v Verbose — print name of each linked file ln -sv /usr/bin/python3 /usr/local/bin/python
-r Create relative symbolic links ln -sr ../shared/config.yml config.yml
-b Make a backup of each existing destination file ln -sb newfile oldlink
-t Specify target directory ln -st /usr/local/bin /opt/app/bin/*

Practical Examples

#1 Create a symbolic link

Creates a symbolic link in your home directory pointing to the web root.
$ ln -s /var/www/html/site /home/user/site

#2 Create a hard link

Creates a hard link — both names point to the same file data on disk.
$ ln data.csv data_backup.csv

#3 Update an existing symlink

Atomically updates a symlink to point to a new release directory. Common in deployment workflows.
$ ln -sfn /var/www/releases/v2.1 /var/www/current

#4 Link a config file

Standard Nginx pattern for enabling sites using symlinks.
$ ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/

#5 Create relative symlink

Creates a relative symlink, which works better when moving directory trees.
$ ln -sr ../shared/.env .env

#6 Verify link details

Shows where a symlink points to with the -> arrow notation.
$ ls -la /usr/bin/python
Output: lrwxrwxrwx 1 root root 7 Mar 14 /usr/bin/python -> python3

Tips & Best Practices

Broken symlinks: If the target of a symlink is moved or deleted, the link becomes broken. Use find -L /path -type l to find broken symlinks.
Deployment with symlinks: Use ln -sfn for zero-downtime deployments: keep releases in versioned directories and symlink "current" to the active release.
Hard link limitations: Hard links cannot cross filesystem boundaries, cannot link to directories, and all links share the same inode number (verify with ls -i).

Frequently Asked Questions

What is the difference between hard and symbolic links?
Hard links point directly to the file's inode (data), so deleting the original still leaves the data accessible. Symbolic links point to the file path, so they break if the target is moved or deleted. Hard links cannot span filesystems.
How do I remove a symbolic link without deleting the target?
Use rm or unlink on the symlink name: rm /path/to/symlink. Do NOT add a trailing slash or you might affect the target directory.
Can I create a symlink to a directory?
Yes, symbolic links can point to directories. Use ln -s /target/dir /link/name. To update an existing directory symlink, use ln -sfn to avoid creating a link inside the existing target.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →