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 unzip Command: List and Extract ZIP Archives

Linux unzip Command: List and Extract ZIP Archives

Learn how to list and Extract ZIP Archives with the Linux unzip command, including practical examples, key options, and important precautions.

Linux cat Command: Display and Concatenate File Contents

Linux cat Command: Display and Concatenate File Contents

Learn how to use the Linux cat command to display text files, concatenate multiple files, number lines, reveal hidden characters, and avoid unsafe redirection mistakes.

Linux bg Command: Resume a Shell Job in the Background

Linux bg Command: Resume a Shell Job in the Background

Learn how to resume a Shell Job in the Background with the Linux bg command, including practical examples, key options, and important precautions.

Linux Tutorial / Install and Update Packages with APT and DNF

Linux Tutorial / Install and Update Packages with APT and DNF

Compare APT on Debian or Ubuntu with DNF on Rocky Linux for searching, installing, updating, and removing packages, including repository and transaction checks.

Linux lscpu Command: Inspect CPU Architecture and Cores

Linux lscpu Command: Inspect CPU Architecture and Cores

Learn how to inspect CPU Architecture and Cores with the Linux lscpu command, including practical examples, key options, and important precautions.

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Learn how to generate SSH Keys and Inspect Fingerprints with the Linux ssh-keygen command, including practical examples, key options, and important precautions.

Linux ln Command: Create Hard Links and Symbolic Links

Linux ln Command: Create Hard Links and Symbolic Links

Learn how to create hard links and symbolic links with Linux ln, choose relative or absolute targets, replace links safely, and inspect link behavior.

Linux kill Command: Send a Signal to a Process ID

Linux kill Command: Send a Signal to a Process ID

Learn how to send a Signal to a Process ID with the Linux kill command, including practical examples, key options, and important precautions.

Linux groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.

Linux file Command: Identify File Types and Formats

Linux file Command: Identify File Types and Formats

Learn how to use the Linux file command to identify file formats from their contents instead of filename extensions, with essential options, practical examples, output interpretation, and common troubleshooting tips.