How to Create Symbolic Link (Symlink) in Linux

December 4, 2023

Introduction

A symbolic link (symlink) references a file or a directory in Linux and other UNIX-like operating systems. Symlinks have many applications, from organizing the OS's tree structure to increasing the accessibility of files on the filesystem.

In this guide, learn how to create symbolic links in Linux using the ln command.

How to Create Symbolic Link (Symlink) in Linux

Prerequisites

  • A system running a Linux distribution.
  • Command-line access.
  • Sudo privileges to access protected files and directories.

What Is a Symlink (Symbolic Link)?

Symlink is a file that stores a path to an existing target (file or directory) on any local or external volume. When a user, application, or system call attempts to access the symlink, the OS automatically interprets the stored path and serves the contents of the target.

Note: Symlinks are conceptually similar to Windows shortcuts. However, the system does not resolve shortcuts automatically, making them readable only by specific programs (Windows Shell, file browsers, etc.).

Soft Links vs. Hard Links

Linux supports two link types: soft links (i.e., symlinks) and hard links. The table below provides the essential points of comparison between the two.

Soft LinkHard Link
Reference TypeFollows a path to a file or a directory.Points to the actual file data on the storage volume.
Compatible TargetsUses files and directories on local and external volumes.Can be created only locally as it references a physical location on the volume
Target DependenceDoes not work after its target file is moved or deleted.Works after the target file is moved or deleted.
UsageOffers quick access to a frequently-used file.Provides flexibility when organizing a filesystem.

As seen above, the differences between soft and hard links stem from how they link to their targets. While a soft link references the path to the target, a hard link references the target's data.

Note: In Linux, each file is a hard link to the physical location of its data.

Hard-linking results in two separate files that reference the same data on the volume. This property of hard links has two consequences:

  • Changes you make on one reflect on all the hard links.
  • Deleting the target file does not affect the other copies.

How to Create Symbolic Link in Linux (ln Command)

Use the ln command to create links to files and directories in Linux. The sections below provide more information about the procedure, alongside some examples.

Create Symlink for File

To create a symbolic link to a file, open a terminal window and enter the command below:

ln -s [target] [symlink]

The command consists of the following elements:

  • The -s option instructs ln to create a symlink. With no options specified, the command creates a hard link.
  • [target] is the file the link references.
  • [symlink] is the location to save the link. If this element is omitted, the command places the symlink in the current working directory.

The example below creates a symbolic link (link-file.txt) that points to the target file (target-file.txt) located in the test directory.

ln -s test/target-file.txt link-file.txt

The command provides no output. However, the ls command shows link-file.txt was created:

A new symlink in the working directory.

We add the -l option to the ls command to see the properties of link-file.txt:

ls -l link-file.txt

The letter l at the beginning of the permissions block shows that the file is a symlink. The output also contains the path to the target file.

Inspecting the symlink with the ls command.

Create Symlink for Directory

A symbolic link can point to a directory's absolute or relative path. Use the following syntax to create a symbolic link to a directory in Linux:

ln -s [target-directory] [symlink]

The example below creates a symbolic link named test-link in the home (~/) directory. The link references a directory on a mounted CD.

ln -s /media/marko/VBox_GAs_6.1.38/cert ~/test-link

The ls command shows the newly created symlink in the home directory.

Linking to a directory in an external volume with the ln command.

The ls -l output shows the l symbol in the permissions block and the path to the target directory.

Viewing the properties of the directory symlink.

Finally, the test-link directory's content corresponds to the cert directory's content on the CD.

Showing the contents of the linked directory.

Note: If the system has a connection to another computer, such as a corporate network or a remote server, symlinks can reference resources on those remote systems.

Overwrite Symbolic Links

When creating a symbolic link, you might receive the following error message:

ln: failed to create symbolic link '[filename]': File exists
Error output when creating a symlink and the file already exists.

The error message means that there is already a file with the same name in the destination. To force the system to overwrite the destination link, use the -f option:

ln -sf [target] [destination]

Warning: Using the -f option permanently deletes the existing file.

Find Broken Symbolic Links

The symlink becomes unusable if the original file is moved, deleted, or unavailable (e.g., when a server goes offline). However, the system does not automatically remove broken symbolic links.

To perform the search and locate the links that do not work, use the following find command:

find [directory] -type l ! -exec test -e {} \; -print

Replace [directory] with the path to the directory where you want to look for the broken links. For example, use the (~) symbol to search the home directory:

find ~ -type l ! -exec test -e {} \; -print
How to find broken symbolic links in Linux.

Remove Symbolic Links

If a symlink is broken or you do not need it anymore, remove it with the unlink command:

unlink [symlink]

Replace [symlink] with the path to the link. Alternatively, use the rm command, as you would with any other file:

rm [symlink]

Both commands provide no output if the operation is successful.

Conclusion

After reading this article, you should understand hard and symbolic (soft) links and know how to work with them. The article provided the syntax for the ln command and offered ways to inspect, overwrite, and remove symlinks on your system.

Next, improve your Linux skills by reading about Linux Commands All Users Should Know.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Use the sudo Command in Linux
August 18, 2020

sudo stands for SuperUser DO, and it's used to temporarily elevate privileges in Linux. This guide will show...
Read more
19 Crucial Linux ls Commands to Know
July 30, 2020

The ls command (short for list) lists information about directories and any type of files in the working...
Read more
How to Use Linux Cat Command (With Examples)
July 13, 2020

The cat command, short for concatenate, is used to display the contents of one or more files, without having...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more