Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux ln command

Reference for the Linux ln command: create hard links and symbolic links with ln -s, relative links, overwrite with -f, link counts and dangling links.

ln creates links. Without options it makes a hard link: a second name for the same file (same inode). With -s it makes a symbolic link (symlink): a tiny special file that stores a path, which the system follows when you open it.

Note the argument order, which mirrors cp: the existing thing comes first, the new link second. ln -s TARGET LINKNAME. If the link name is a directory, the link is created inside it with the target's base name.

Terminal
ln [OPTION]... TARGET LINK_NAME
ln -s [OPTION]... TARGET... DIRECTORY

Try it

Bash Hard link versus symbolic link

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Relative links, replacing and linking directories

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
ln [-sfnrTvb] [-t DIRECTORY] TARGET... LINK_NAME
Option Description
-s Create a symbolic link instead of a hard link.
-f Remove an existing destination first.
-n Treat a destination that is a symlink to a directory as a normal file (important with -f).
-r With -s, compute the target path relative to the link's directory.
-T Treat the link name as a normal file, never as a directory.
-t DIR Create the links inside DIR.
-v Print the links as they are made.
-b Back up an existing destination.
-L, -P When creating a hard link to a symlink, follow it (-L) or link the symlink itself (-P, default on Linux).

Returns: 0 on success, non-zero otherwise (for example "File exists", "Invalid cross-device link" or "hard link not allowed for directory").

Usage notes

  • Hard links share data and inode. They only work inside one filesystem, cannot point to directories, and cannot tell you which name is "the original". ls -l shows the link count in the second column.
  • Symbolic links can cross filesystems, point to directories, and even point to something that does not exist (a dangling link).
  • Relative symlink targets are interpreted relative to the link's location, not to your current directory. ln -s ../data/f bin/f works from anywhere; ln -s data/f bin/f from the parent directory creates a broken link. -r computes the right path for you.
  • Switching a directory link: ln -sfn newdir current. Without -n, if current already points to a directory, ln -sf creates the new link inside that directory.
  • Sizes: a symlink's size is the length of its stored path, and its permissions (lrwxrwxrwx) are ignored; the target's permissions apply.
  • Deleting: rm link removes only the link. Deleting the target of a symlink leaves it dangling; deleting one hard link leaves the others intact.
  • Permissions on hard links: protected hardlinks (fs.protected_hardlinks=1) stop users from linking files they do not own.
  • Finding links: find . -type l lists symlinks, find . -xtype l lists broken ones, find . -samefile f lists all hard links of f.
  • Windows and containers: some filesystems (FAT, certain shared folders) do not support links at all.