Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ln Command Guide for Creating Links Between Files

This guide helps you understand and use the ln command, which creates links — pointers from one file path to another.


What is ln, and Why Use It?

The ln command creates a link: a new directory entry that points at an existing file or directory. There are two kinds. A symbolic link (symlink, created with -s) is a standalone file that stores the path of its target, like a shortcut. A hard link (the default) is another name for the same data on disk, indistinguishable from the original.

Why Learn ln?

  • Reference shared files once: Update software in one place and every symlink sees the change.
  • Shorter paths: ln -s /var/www/site/releases/current public keeps URLs stable across deployments.
  • Version management: Symlink current to the newest release so rollbacks are one ln away.
  • Understand the system: Package managers and /etc/alternatives rely heavily on symlinks.

ln is part of the base system on Linux and macOS. There is nothing to install.


ln -s target.txt link.txt
  • -s makes a symbolic link named link.txt that points at target.txt.

Example:

ln -s /etc/nginx/nginx.conf ~/nginx.conf
ls -l ~/nginx.conf
lrwxrwxrwx 1 alice alice 22 Dec  4 10:00 /home/alice/nginx.conf -> /etc/nginx/nginx.conf
  • The l at the start of the permissions and the -> arrow confirm it is a symlink.

ln target.txt hardlink.txt
  • Without -s, ln creates a hard link: a second name for the same inode.
ls -i target.txt hardlink.txt
  • -i prints the inode number; both files show the same number, proving they share the data.

ln -s ~/projects/app ~/current-app
  • When the second argument is an existing directory, the link is created inside it with the target’s basename.
ln -s /opt/app/releases/v2 /opt/app/current
  • The classic release-symlink pattern: point current at v2, and update it on each deploy.

ln -s ~/Documents/*.pdf ~/pdf-links/
  • A symlink is created for each matching file inside the target directory.

ln -sf new-target.txt link.txt
  • -f (force) removes an existing link.txt before creating the new link.
ln -sfn /opt/app/releases/v3 /opt/app/current
  • -n treats an existing symlink-to-directory as a normal file, so the link itself is replaced instead of creating a link inside the old target. This combination is the safe way to repoint a current symlink.

ls -l link.txt
  • Shows what the link points to.
readlink -f link.txt
  • Resolves the link to its final absolute target, following chains.
rm link.txt
  • Removes the link itself, never the target. (Never use rm -r on a symlinked directory unless you intend to follow it.)

Common Pitfalls

  • Relative vs absolute targets. ln -s ../shared/config.yml link stores the path as written. If you move the link, relative targets can break. Absolute targets are more robust.
  • Deleting a symlink does not delete the target. rm link is safe; the original file stays.
  • Hard links have limits. They cannot cross filesystems and, on most systems, cannot point at directories.
  • Overwriting a symlink to a directory. Without -n, ln -sf may create the new link inside the old target directory. Always use ln -sfn when repointing.
  • Broken links are visible. ls shows a symlink in red (or with a ? in ls -l) when its target is missing.

CommandDescription
cpCopy data, which symlinks do not
rmRemove a link without touching its target
readlinkPrint what a symlink points to
statShow inode and link-count details
chmodSymlink permissions are always rwxrwxrwx; the target’s permissions govern access

ln Cheatsheet

CommandDescription
ln -s target linkCreate a symbolic link
ln target hardlinkCreate a hard link
ln -s dir/ linkCreate the link inside dir/
ln -sf target linkReplace an existing link
ln -sfn newdir linkRepoint a symlink-to-directory safely
ln -s a b c dir/Link multiple targets into a directory
readlink linkShow the link target
readlink -f linkResolve to the final absolute path
ls -l linkInspect link type and target
ls -i fileShow the inode number (hard links share it)