How to Create Symbolic Links (Symlinks) in Linux Using the ln Command

In Linux, links act as shortcuts to files or directories located elsewhere in the file system. The most common and useful type of link is the symbolic link, commonly referred to as a “symlink” or a “soft link.”

Unlike a “hard link,” which points directly to the physical data on the hard drive, a symlink simply contains a text string pointing to the original file’s path. This means you can create symlinks across different hard drives, different file systems, and even point them at directories. If you delete the symlink, the original file remains perfectly safe. (However, if you delete the original file, the symlink becomes “broken” and points to nothing).

This guide explains how to create symbolic links using the ln command in the Linux terminal.

The Basic Syntax for Symlinks

To create a link, you must use the ln (link) command. To ensure you create a symbolic (soft) link rather than a hard link, you must pass the -s flag.

The standard syntax is always: ln -s [Target] [Link_Name]

  • Target: The absolute path to the existing, original file you want to link to.
  • Link_Name: The path and name of the new shortcut you are creating.

How to Create a Symlink to a File

Suppose you have a configuration file buried deep in the system at /var/www/html/website/config/settings.php, and you want a quick shortcut to it right on your user’s desktop.

You would execute the following command:

ln -s /var/www/html/website/config/settings.php /home/user/Desktop/settings-shortcut.php

Now, if you open settings-shortcut.php on your desktop, you are actually editing the live configuration file on the server. Always use absolute paths (starting with /) for the Target to ensure the link works regardless of your current directory.

How to Create a Symlink to a Directory

Creating a link to an entire folder is identical to linking a file. This is incredibly useful for providing quick access to deep log directories.

For example, to link the Apache server logs directory to your home folder:

ln -s /var/log/apache2/ /home/user/apache-logs

You can now simply type cd ~/apache-logs to instantly jump to the server logs.

How to Verify or Delete a Symlink

If you want to check where a symlink is pointing, use the standard list command with the -l flag:

ls -l /home/user/Desktop/settings-shortcut.php

The terminal will output the file details, ending with a visual arrow showing the exact path it points to: settings-shortcut.php -> /var/www/html/website/config/settings.php

To remove a symlink:

You can delete a symlink using the standard rm (remove) command or the unlink command. This will only delete the shortcut; the original file or directory is completely untouched.

rm /home/user/apache-logs

Warning: When removing a symlink to a directory, do NOT append a trailing slash (e.g., do not type rm /home/user/apache-logs/). Doing so instructs the system to enter the directory and attempt to delete its contents, which could destroy the original files if you have sufficient permissions. Always remove the link without the trailing slash.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.