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
Make a symbolic link: ln -s
The target comes first, then the link’s name. ls -l shows where it points.
ln -s notes.txt latest.txt && ls -l latest.txtlrwxrwxrwx 1 learner learner 9 Jan 12 09:01 latest.txt -> notes.txt
Link to a folder
A symlink can point to a folder anywhere, which makes a handy shortcut.
ln -s /var/log logs && ls logsapt auth.log syslog
Make a hard link
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.txt131211 -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
Change where a link points: ln -sf
-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.txtreport.txt
Find and remove links
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, --symbolic | make a symbolic link instead of a hard link |
|---|---|
| -f, --force | replace a file that already has the link’s name |
| -n, --no-dereference | treat an existing link to a folder as a file (use with -f to replace it) |
| -v, --verbose | print the name of each link made |
More examples
ln -s notes.txt latest.txtln -s /var/log logsln notes.txt notes-hard.txtln -sf report.txt latest.txtQuestions 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
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.