We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

Symlinks in Linux: How to Create and Use Them

Boot.dev Team
Boot.dev TeamProgramming course authors and video producers

Last published

Table of Contents

A symlink lets one file live at two paths: the real one, and a lightweight pointer that forwards to it. It's one of those Linux features you can ignore for years, then suddenly use every day once you've seen what it's for (dotfiles, versioned tool installs, and log directories, to name a few). Here's what symlinks are, how to create them with ln -s, and how they differ from copies.

All the content from our Boot.dev courses are available for free here on the blog. This one is the "Local CLI" chapter of Learn Linux. If you want to try the far more immersive version of the course, do check it out!

A symbolic link, usually called a symlink for short, is a special kind of file that points to another file or directory.

It's a lot like a shortcut in a GUI. The symlink has its own path, but when you use it, the operating system follows the link to the target.

The ln command creates links. To create a symbolic link, use the -s flag:

ln -s target_path link_path

Note, the path to the target comes first, then the path where the symlink will be created. For example:

ln -s documents/important.txt important.txt

That creates a symlink in the current directory named important.txt, which points to documents/important.txt.

Directories work exactly the same way. For example, you can keep a stable path that always points at the latest version of something:

ln -s releases/v2 current

Now cd current drops you into releases/v2, and when v3 ships you just update the link instead of every script that references it.

You can use ls -l to see where a symlink points:

important.txt -> documents/important.txt

A relative target is resolved from the symlink's location, not from wherever you run ln. You can use either a relative or an absolute path for the target, and both options have tradeoffs. If you use an absolute path, you can usually move the symlink without a problem, but moving the target will break the symlink. If you use a relative path, the symlink will continue to work as long as its position relative to the target stays the same. For example, you could move a parent directory containing both the symlink and the target.

A symlink is not the same as a copy: it doesn't duplicate the file's contents. It just creates another path that points to the original. That means:

  • If the target file changes, the symlink shows the new contents.
  • If you delete the symlink, the target file still exists.
  • If you delete the target file, the symlink breaks.

That last one is worth remembering: a symlink whose target is gone is called a dangling (or broken) symlink. It still exists as a file, but following it leads nowhere.

To remove a symlink, just rm the link itself — the target is untouched:

rm important.txt

Symlinks show up everywhere once you know to look: your shell configuration dotfiles are commonly symlinked from a Git repo, and package managers use symlinks to wire up installed tools. If you want to practice creating (and breaking, and fixing) them yourself, the Learn Linux course has a whole section on it.

Frequently Asked Questions

Is a symlink the same as a copy of a file?

No. A symlink does not duplicate the file's contents, it just creates another path that points to the original. If the target changes, the symlink shows the new contents; if you delete the target, the symlink breaks; and if you delete the symlink, the original file is untouched.

How do I create a symlink in Linux?

Use the ln command with the -s flag: ln -s target_path link_path. The target comes first, then the path where the symlink should be created. It works the same way for files and directories.

How do I remove a symlink?

Just rm the symlink itself, like any other file. Removing the link does not touch the target file or directory it points to.

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

A symlink is a separate file that points to a path, so it breaks if the target moves or is deleted. A hard link is another directory entry for the same underlying data, so the data survives as long as at least one hard link remains. ln creates hard links by default; the -s flag makes symbolic links, which are what you want most of the time.

What is a dangling symlink?

A dangling (or broken) symlink is one whose target no longer exists, usually because the target was moved or deleted. The link file still exists, but following it leads nowhere until you recreate the target or update the link.

Related Articles