Skip to content

The ln command

The ln command makes links: ln -s target linkname makes a symbolic link…

The ln command makes links: ln -s target linkname makes a symbolic link (a shortcut to a path), and ln without -s makes a hard link (a second name for the same data).

Taught in Intermediate, lesson 18

A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.

Usage

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

Make links between files. Gives a file a second name. With -s it makes a symbolic link (a symlink, or soft link): a small file that points to a path, like a shortcut. It can point to a folder or to another disk, and it breaks if the target is moved. Without -s it makes a hard link: another name for the same data, which keeps working when the first name is deleted, but only for files on the same disk. The target comes first and the new name second, the same order as cp.

How to use ln

The target comes first, then the link’s name. ls -l shows where it points.

ln -s notes.txt latest.txt && ls -l latest.txt
lrwxrwxrwx 1 learner learner 9 Jan 12 09:01 latest.txt -> notes.txt

A symlink can point to a folder anywhere, which makes a handy shortcut.

ln -s /var/log logs && ls logs
apt  auth.log  syslog

A hard link is another name for the same data, with the same inode number. Deleting one name leaves the other working.

ln notes.txt notes-hard.txt && ls -li notes.txt notes-hard.txt
131211 -rw-r--r-- 2 learner learner 361 Jan 12 09:00 notes-hard.txt
131211 -rw-r--r-- 2 learner learner 361 Jan 12 09:00 notes.txt

-f replaces an existing link instead of stopping with "File exists".

ln -s notes.txt latest.txt && ln -sf report.txt latest.txt && readlink latest.txt
report.txt

find -type l lists symbolic links. Removing a link with rm leaves the file it points to alone.

ln -s notes.txt latest.txt && find . -type l && rm latest.txt
./latest.txt

Options

-s, --symbolicmake a symbolic link instead of a hard link
-f, --forcereplace a file that already has the link’s name
-n, --no-dereferencetreat an existing link to a folder as a file (use with -f to replace it)
-v, --verboseprint the name of each link made

More examples

ln -s notes.txt latest.txt
A symbolic link called latest.txt that points to notes.txt
ln -s /var/log logs
A shortcut to a folder
ln notes.txt notes-hard.txt
A hard link: a second name for the same data
ln -sf report.txt latest.txt
Make the link point somewhere else, replacing it

Questions about ln

What is the difference between a hard link and a symbolic link?

A symbolic link stores a path and points to it, so it breaks if the target moves, but it can point to folders and other disks. A hard link is a second name for the same data on the same disk: both names are equal, and the data stays until the last name is deleted.

What order do the arguments go in?

The existing file first, then the new link: ln -s existing-file new-link, the same order as cp.

How do I remove a symbolic link?

Use rm linkname (or unlink linkname). It removes the link, not the file it points to. For a link to a folder, don’t add a slash at the end.

Learn it step by step

Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.