LearnWithShobhan

Symbolic links in Linux: ln -s explained (soft links vs copies)

Shobhan Biswas··4 min read
>_From the course CLI Mastery: 57 lessons, lifetime access.

A symbolic link (symlink, or soft link) is a file that does not hold any data. It holds a path to somewhere else. Open it, and the system quietly follows the path and opens the real thing.

If you come from Windows, think of it as a shortcut, except that it works at the file system level, so every program and every command treats it like the real file.

Why you would want one

Say a program keeps its logs in a deep folder, but you want to find them at a short, predictable place. Instead of moving the logs (and breaking the program), you leave them where they are and create a link in the place you want:

  • You want ~/Desktop/config to open the file that really lives in ~/Desktop/directory1/config.
  • A tool expects /usr/local/bin/node, but the version you want is installed somewhere else. Version managers use symlinks for exactly this.
  • You keep your dotfiles in one Git folder, and link them where the system looks for them: ~/.zshrc points into your repo.

Create one: ln -s

The command is ln (link) with the -s flag (symbolic). The order is the one people forget:

ln -s <where the real file is> <name of the link>
bash
$ ls directory1
config
$ ln -s directory1/config config
$ ls -l
total 8
lrwxr-xr-x 1 shobhan staff 17 Nov 12 10:02 config -> directory1/config
drwxr-xr-x 3 shobhan staff 96 Nov 12 09:58 directory1

Read the ls -l line. The first letter is l (for link, where a normal file shows - and a directory d), and the name is followed by an arrow showing exactly where the link points. A link can point to a file or to a whole directory.

Use it like the real thing

bash
$ nano directory1/config # edit the original
$ cat config # read through the link
debug=true
$ cat directory1/config
debug=true

Both commands print the same text, because there is only one file. Edit through the link and you change the original. You can even rename the link with mv, and it keeps working, because renaming a link changes the link, not the file it points to.

A link is a pointer, not a copy

This is the core idea, and the thing that confuses beginners. Delete the original and the link stays, but it now points at nothing:

Diagram: a symlink on the desktop pointing at a real file works while the original exists and fails with No such file once it is deleted
The link only stores a path. Delete the file at the end of the path and the link is broken (a dangling link).
bash
$ rm directory1/config
$ cat config
cat: config: No such file or directory
$ ls -l config
lrwxr-xr-x 1 shobhan staff 17 Nov 12 10:02 config -> directory1/config

The link is still listed, still with its arrow, but it is dangling. Nothing was copied, so nothing survived. The reverse is also safe: rm config removes only the link and leaves the original untouched.

Soft links vs hard links

ln without -s makes a hard link: a second name for the same data, which keeps working even if the first name is deleted. Hard links cannot point to directories or across disks, and they are rarely what you need. In day-to-day work, 99% of the time you want ln -s.

Symbolic (soft) linkHard link
Commandln -s target linkln target link
StoresA pathAnother name for the same data
Works on directoriesYesNo
If the original is deletedLink breaksData is still there

The mistake everyone makes: relative paths

A relative path in a symlink is read from the folder where the link lives, not from where you ran the command.

bash
$ cd ~
$ ln -s directory1/config ~/Desktop/config
$ cat ~/Desktop/config
cat: /Users/shobhan/Desktop/config: No such file or directory

The link sits in ~/Desktop, so it looks for ~/Desktop/directory1/config, which does not exist. The fix is to use the full path as the first argument:

ln -s ~/directory1/config ~/Desktop/config

If you are unsure how the two path styles differ, read absolute vs relative paths in Linux, and this will never bite you again.

Key takeaways

  • ✓A symlink stores a path, not data: ln -s <real file> <link name>.
  • ✓ls -l shows a link with a leading l and an arrow to its target.
  • ✓Edits through the link change the original, because there is only one file.
  • ✓If the original is deleted the link dangles. rm on the link never touches the original.
  • ✓Use the full path for the target, or the link breaks when it lives in another folder.

Learn the whole terminal, not just one command

Links are a small piece of the Unix file system. CLI Mastery teaches the rest in order: paths, files, permissions, processes, pipes, shell scripts, and finally connecting to a real server with SSH. 57 lessons, lifetime access. Try the free lesson on absolute and relative paths.

Free download

Linux command line cheat sheet

One printable page: navigation, files, permissions, pipes, processes, scripts and SSH.

Occasional emails with new tutorials and courses. Unsubscribe any time.

Keep reading