Don’t miss Amazon Black Friday See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux Symbolic Links: Create One with ln

To create a symbolic link in Linux, run ln -s TARGET LINK_NAME, replacing TARGET with the file or directory the link should point to and LINK_NAME with the name you want to create. For example, ln -s /var/www/site ~/site creates a link named site in your home directory that refers to /var/www/site.

What a Symbolic Link Does

A symbolic link, often called a symlink or soft link, is a special filesystem object that stores a path to another file or directory. When a program accesses the link, Linux follows that path to reach the target. The link is not a duplicate of the target: changing the target’s contents changes what you see through the link, and the link itself uses very little space.

Symlinks are useful when you want one file or directory to be available from more than one location. You might link a project folder into a workspace, point a configuration filename at a versioned file, or make a convenient command-line shortcut to a deeply nested directory. They can also help software find files in expected locations, provided the target remains available and permissions allow access.

A symlink is different from a hard link. A hard link is another directory entry for the same underlying file, while a symlink contains a path that is followed to find its target. That distinction affects behavior when a target is moved or removed, and is one reason the -s option matters.

The Basic ln Command

The general form is:

ln -s TARGET LINK_NAME

The first path is the target; the second is the new link. For example, if /srv/projects/atlas is a directory and you want to reach it as ~/atlas, use:

ln -s /srv/projects/atlas ~/atlas

The command normally prints nothing when it succeeds. Check the result with ls -l ~/atlas. The output begins with an l to indicate a link and shows its destination, similar to atlas -> /srv/projects/atlas.

Use ln -s for both files and directories; there is no separate directory-link option. The command creates a link, not its parent directories, so the directory containing LINK_NAME must already exist. If a path contains spaces or shell characters, quote it so the shell passes it as one argument:

ln -s "/mnt/shared/Design Files" "$HOME/Design Files"

Choose an Absolute or Relative Target

An absolute target starts from the root directory, such as /srv/projects/atlas. It is easy to understand and continues to identify the same location when you run commands from different working directories. It is often a good choice for links in a stable system location.

A relative target is interpreted from the directory containing the symlink, not from the directory where you happen to run ln. Suppose you are in /home/lee/work and run:

ln -s ../shared/assets assets

The link /home/lee/work/assets points to /home/lee/shared/assets. Relative paths can make a directory tree portable: if the link and target move together while keeping the same layout, the link can continue to work. But a relative link may break if either is moved independently. When diagnosing one, calculate its target from the link’s parent directory.

There is no universally best choice. Prefer an absolute path when the target’s location is fixed and clarity matters. Consider a relative path when a project or bundle may be relocated as a unit. In either case, verify the path before relying on the link.

Creating Links to Files and Directories

To give a configuration file a shorter or more convenient name, create a file symlink:

ln -s /etc/example/app.conf ~/app.conf

To make a directory available elsewhere, use the same syntax:

ln -s /opt/tools/current ~/tools

Afterward, cd ~/tools takes you into the target directory, and commands that read ~/tools generally access the target’s contents. The link remains a distinct filesystem object, even though many everyday commands follow it when they access files.

You can also ask ln to put a link inside an existing destination directory. For example, ln -s /srv/data /home/lee creates a link named data inside /home/lee, assuming that destination directory exists and the name is available. When the destination is a directory, this behavior can be less obvious than naming the link explicitly. Using a full link path such as /home/lee/data makes your intent clearer.

Check, Read, and Remove a Symlink

Use ls -l LINK_NAME to see whether a path is a symlink and what path it stores. For scripting or inspection, readlink LINK_NAME prints the stored target path. These checks answer slightly different questions: readlink reports the link text, while opening the link follows it to the target.

A link can exist even when its target does not. This is called a dangling or broken symlink. In a long listing it is often shown in a different color, depending on your terminal settings, and attempts to read through it fail. A target may have been moved, deleted, or become inaccessible. To find the problem, inspect the stored path and check whether it resolves from the link’s directory.

To remove the link itself, use rm LINK_NAME or unlink LINK_NAME. Removing a symlink does not remove the target. Take care with commands that operate on a path after following it, and confirm the exact name before using recursive deletion. If you want the target gone too, remove it separately and only after checking that it is the intended file or directory.

Replace an Existing Link Safely

ln -s will not normally replace an existing file or link. It reports an error so that you do not accidentally overwrite something. First inspect the existing path with ls -l; it may be a real file or directory rather than an old symlink.

If you have confirmed that the existing path is a symlink and want to replace it, remove that link and create the new one:

rm ~/current-tool
ln -s /opt/tools/v2 ~/current-tool

There can be a short interval between those commands when the link does not exist. On GNU/Linux systems using GNU Coreutils, ln -sfnT TARGET LINK_NAME can replace a destination symlink without treating a symlink to a directory as a destination directory. The flags mean symbolic, force, no-dereference, and treat the destination as a normal file. Because -T is a GNU extension and forceful replacement deserves care, check your system’s ln manual and verify the destination before using it. Do not apply a force option to a path you have not inspected.

Symlinks Compared with Hard Links

Both hard links and symlinks can make a file reachable through multiple names, but they work differently. A hard link refers directly to the file’s underlying inode. It generally cannot cross filesystem boundaries and ordinarily cannot be made to a directory by an unprivileged user. Removing one hard-link name does not remove the file’s data while another name remains.

A symlink stores a pathname instead. It can point to a directory, can refer to a location on another mounted filesystem, and can point to a target that has not been created yet. If the target is renamed or removed, the symlink does not automatically update; it may become dangling. Use a symlink when you need a path-based reference or a link to a directory. Use a hard link only when you specifically want another name for the same file on the same filesystem and understand the consequences.

A symlink is also not the same as copying a file. A copy has its own contents and can diverge from the original. A symlink continues to refer to its target, so changes made through either path affect the same underlying target.

Permissions and Safety Considerations

On Linux, access through a symlink is generally governed by permissions on the target and on the directories leading to it. The permissions displayed for the symlink itself are usually not the controls that decide whether you can read or modify the target. A link does not grant access that you otherwise lack.

Be cautious when creating links in shared or privileged locations. A symlink can point somewhere unexpected, and programs or scripts that trust a path without checking it may act on the target instead. Before using a symlink in an automated cleanup, installation, or deployment process, establish whether that process follows links and whether the target can be changed by another user. Avoid using elevated privileges just to create a link in a directory you do not own unless you understand why they are needed.

Symlinks can cross filesystem boundaries because they store paths rather than direct inode references. However, a link that points into a temporarily unmounted drive, network share, or removable disk will not be useful while that target is unavailable. The link can remain present even when the destination cannot be reached.

Common Problems and How to Fix Them

  • “File exists”: The requested link name is already occupied. Inspect it with ls -l, then decide whether to keep it or deliberately remove or replace it.
  • The link points to the wrong place: Check whether you used an absolute or relative target. For a relative target, resolve it from the directory containing the link, not your current shell directory.
  • The link is broken: Confirm that the target exists and that every directory in its path is accessible. A symlink can be created before its target exists, so successful creation alone does not prove that it works.
  • “Too many levels of symbolic links”: The link may lead back to itself, or two or more symlinks may form a loop. Inspect each link in the chain and change one of the paths to break the cycle.
  • Permission denied: Check permissions on the target and its parent directories, as well as the directory where you are creating the link. Creating the link requires permission to write in its parent directory.
  • A command acts on the target: Many programs follow symlinks when opening paths. Confirm how that particular command handles links before using it for deletion, copying, or a privileged operation.

Frequently Asked Questions

What is the exact command to create a symbolic link in Linux?
Use ln -s TARGET LINK_NAME. For example, ln -s /var/log/app.log ~/app-log creates ~/app-log as a symlink to the log file.

Can I create a symbolic link to a directory?
Yes. Use the same ln -s command as for a file. For example, ln -s /srv/media ~/media creates a directory symlink named media in your home directory.

How do I tell whether a path is a symlink?
Run ls -l PATH and look for a leading l and an arrow showing the destination. You can run readlink PATH to print the path stored in the link.

Does deleting a symlink delete its target?
No. Removing the symlink with rm LINK_NAME removes that link, not the file or directory it points to. Check the path carefully before using recursive commands.

Can a symbolic link point to a file that does not exist yet?
Yes. Linux allows a symlink to store a path even if no target currently exists. The link will be dangling until that path resolves to an accessible file or directory.

Why use a relative symlink instead of an absolute one?
A relative link can keep working when a directory tree is moved as a unit, as long as the layout between the link and target stays the same. An absolute link is often clearer for a target at a fixed location.

Linux Symbolic Links: Create One with ln
Scroll to top