Linux ln Command: Create Hard Links and Symbolic Links

This guide explains how to create hard links and symbolic links with Linux ln, choose relative or absolute targets, replace links safely, and inspect link behavior. It focuses on practical Linux usage, predictable automation, and the cases where a seemingly small path or option mistake can change the result.

The examples use GNU Coreutils behavior. Check ln --help, man ln, and your distribution documentation when portability or a version-specific option matters.

What Is the ln Command?

ln creates hard links by default and symbolic links with -s. A hard link is another name for the same inode on one filesystem. A symbolic link is a separate file containing a target path and can point across filesystems or to a directory.

Basic Syntax

ln [OPTION]... [-T] TARGET LINK_NAME
ln [OPTION]... TARGET... DIRECTORY

Quote paths that contain spaces. When supported, place -- before operands that begin with a hyphen so they are not interpreted as options.

Basic ln Usage

Create a hard link

Both names refer to the same inode. Editing through either path changes the same underlying file data.

ln original.txt hard-link.txt
stat -c '%i %h %n' original.txt hard-link.txt

Create a symbolic link

Use -s. The stored target string may be absolute or relative and may refer to a path that does not yet exist.

ln -s /opt/myapp/current app-current
readlink app-current

Create several links in a directory

List several targets and a final directory, or place the target directory after -t.

ln -s file1 file2 links/
ln -s -t links file1 file2

Checking the path that a symbolic link points to

ln -s usually does not display a success message. The target string recorded in the link can be checked with readlink.

printf 'demo\n' > original.txt
ln -s original.txt shortcut.txt
readlink shortcut.txt

The example output is original.txt. Relative paths are interpreted based on the directory where the link is located, so moving the link to a different location may break the link.

Key Options

Option Purpose
-s Create symbolic links instead of hard links.
-r Generate a relative symbolic target; use with -s.
-f Remove an existing link name before creating the new link.
-i Prompt before replacing an existing link name.
-n Treat a symlink-to-directory destination as the link itself.
-T Treat the link name as an exact path.
-b Back up an existing destination.
-v Report links that are created.

Long options often make reviewed scripts easier to understand. When a script must run on non-GNU systems, verify every non-POSIX option on the target platform.

Practical Examples

Create a relative symbolic link

Relative targets are resolved from the directory containing the link, not from the shell's current directory. GNU ln -sr can calculate a relative target.

ln -s ../releases/2026-09 app/current
ln -sr /srv/app/releases/2026-09 /srv/app/current

Replace a deployment link carefully

First confirm whether the existing path is a symlink or a real directory. On GNU systems, -sfnT can replace one exact link path after validation.

readlink current
ln -sfnT releases/2026-10 current
readlink current

Inspect link type and resolution

Combine ls -l, readlink, and stat to examine the stored string, final target, and inode relationship.

ls -l app-current
readlink app-current
readlink -f app-current
stat app-current

ln and Related Commands

Command or method Primary role Best fit
Hard link Another name for one inode Regular files on the same filesystem
Symbolic link A separate file storing a target path Directories, cross-filesystem targets, and pointers
cp An independent data copy Copies that should diverge
Bind mount Expose a path at another mount point Filesystem-level directory mapping

Important Cautions

Remember the operand order

The common form is ln TARGET LINK_NAME. Reversing the intent can produce a link in the wrong place.

Relative targets are based on the link location

A relative string that works from the current shell can break when the link is created elsewhere. Calculate it from the link's containing directory.

Hard links cannot cross filesystems

They share an inode and therefore stay on one filesystem. Hard links to directories are normally restricted.

FAQ

Does ln create a symbolic link by default?

No. It creates a hard link unless -s is used.

Does deleting the original name destroy a hard-linked file?

No. The data remains while another hard link refers to the inode.

How do I detect a broken symbolic link?

Use readlink to inspect the stored path and readlink -e or a suitable test to check final resolution.

Should a symbolic link use an absolute or relative target?

Relative links suit movable directory trees; absolute links suit fixed system paths. Choose based on how the link and target will move.

Related Resources

More in This Category
Linux fsck Command: Check and Repair Filesystems

Linux fsck Command: Check and Repair Filesystems

Learn how to check and Repair Filesystems with the Linux fsck command, including practical examples, key options, and important precautions.

Linux su Command: Switch User Accounts and Login Shells

Linux su Command: Switch User Accounts and Login Shells

Learn how to use the Linux su command to switch user accounts and start login or non-login shells, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Linux uniq Command: Remove or Count Adjacent Duplicate Lines

Learn how to remove or count adjacent duplicate lines with the Linux uniq command, including practical examples, key options, and important precautions.

Linux wget Command: Download Files from URLs

Linux wget Command: Download Files from URLs

Learn how to download Files from URLs with the Linux wget command, including practical examples, key options, and important precautions.

Linux gzip Command: Compress a File in gzip Format

Linux gzip Command: Compress a File in gzip Format

Learn how to compress a File in gzip Format with the Linux gzip command, including practical examples, key options, and important precautions.

Linux chgrp Command: Change a File's Group

Linux chgrp Command: Change a File's Group

Learn how to change a File's Group with the Linux chgrp command, including practical examples, key options, and important precautions.

Linux lsmod Command: List Loaded Kernel Modules

Linux lsmod Command: List Loaded Kernel Modules

Learn how to list Loaded Kernel Modules with the Linux lsmod command, including practical examples, key options, and important precautions.

Linux tracepath Command: Trace a Network Path and Path MTU

Linux tracepath Command: Trace a Network Path and Path MTU

Learn how to trace a Network Path and Path MTU with the Linux tracepath command, including practical examples, key options, and important precautions.

Linux printenv Command: Print Environment Variable Values

Linux printenv Command: Print Environment Variable Values

Learn how to use the Linux printenv command to print all environment variables or selected variable values, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Linux Tutorial / Linux Terminal and Shell Basics: Command Syntax and Finding Help

Learn the difference between a terminal and a shell, read command syntax and output, and use Bash help tools through a short hands-on exercise.