Navigation and files

Command ln

Create a hard or symbolic link

Linux · Bash examples · commands run on the selected server

When to use ln

Use ln to give data another filesystem name. A hard link refers to the same inode, while a symbolic link stores a path that is resolved when opened.

Syntax

ln [PARAMETERS] TARGET LINK_NAME

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-s
Create a symbolic link instead of a hard link.
-i
Ask before replacing an existing destination.
-v
Show created link.

Usage examples

Create a directory link

ln -s ./releases/current ./current-release

The target path is interpreted relative to the link directory. Check that this release link resolves to the intended directory.

Create a hard link

ln ./report.txt ./report-copy.txt

Both names refer to the same file data; editing through either name affects that same data.

View link target

ls -l ./current-release

Inspect the arrow target and permissions to check that the symbolic link points where intended.

Notes and limitations

  • The relative target of a symbolic link is measured from the directory of the link itself.
  • Hard links generally cannot be created between different file systems or to directories.

Common questions and troubleshooting

Why is my symbolic link broken?

Its stored path may no longer exist or may be wrong relative to the link directory. Inspect it with ls and resolve the intended target.

Can a hard link cross filesystems?

Ordinary hard links cannot cross filesystem boundaries and usually cannot target directories. Use a symbolic link when a path across mounts is required.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man ln.