What Is a Symlink and How Do I Create One?

On this page
  1. Contents
  2. Types of Links
  3. Hard Links
  4. Symbolic Links (Soft Links)
  5. Using the ln Command
  6. Creating a Symlink to a File
  7. Example
  8. Verifying the Symlink
  9. Creating a Symlink to a Directory
  10. Example
  11. Overwriting an Existing Symlink
  12. Removing a Symlink
  13. Using unlink
  14. Using rm
  15. Important Note
  16. Broken Symlinks
  17. Common Use Cases
  18. Create a Shortcut
  19. Share a Directory Between Applications
  20. Switch Application Versions Easily
  21. Summary

A symbolic link, commonly called a symlink, is a special type of file that points to another file or directory. Instead of containing data itself, a symlink acts as a reference to another location on the filesystem.

Symlinks are commonly used to create shortcuts, share files between locations, or make files and directories accessible from multiple paths without duplicating data.

Contents

  • Types of Links
  • Using the ln Command
  • Creating a Symlink to a File
  • Creating a Symlink to a Directory
  • Overwriting an Existing Symlink
  • Removing a Symlink
  • Summary

Linux and UNIX systems support two types of links:

A hard link is an additional name for an existing file.

Characteristics:

  • Multiple filenames can point to the same file data.
  • Changes made through one link are visible through all other links.
  • Hard links cannot point to directories.
  • Hard links cannot span different filesystems or partitions.

A symbolic link is similar to a shortcut in Windows.

Characteristics:

  • Can point to files or directories.
  • Can point to locations on different filesystems or partitions.
  • Contains a reference to the target path rather than the actual data.

Using the ln Command

The standard Linux command used to create links is:

ln

By default, ln creates hard links.

To create a symbolic link, use the -s option:

ln -s [OPTIONS] TARGET LINK_NAME

Where:

  • TARGET is the existing file or directory.
  • LINK_NAME is the name of the symbolic link to create.

If the command completes successfully, no output is displayed.

To create a symbolic link to a file:

ln -s source-file symbolic-link

Example

ln -s my-file.txt my-link.txt

This creates a symbolic link named:

my-link.txt

that points to:

my-file.txt

Use:

ls -l my-link.txt

Example output:

lrwxrwxrwx 1 username users 11 Dec 2 23:03 my-link.txt -> my-file.txt

The leading:

l

indicates that the file is a symbolic link.

The arrow:

->

shows the destination to which the link points.

Creating a symbolic link to a directory uses exactly the same syntax.

Example

ln -s /images ~/pictures

This creates:

~/pictures

as a symbolic link pointing to:

/ images

Any access to the pictures directory will automatically redirect to the images directory.

If a symbolic link with the same name already exists, the command will fail:

ln -s my-file.txt my-link.txt

Output:

ln: failed to create symbolic link 'my-link.txt': File exists

To overwrite an existing symbolic link, use the -f (force) option:

ln -sf my-file.txt my-link.txt

This replaces the existing symlink with the new target.

You can remove symbolic links using either the unlink or rm command.

unlink symlink-name

Example:

unlink my-link.txt

Using rm

rm my-link.txt

Both commands remove only the symbolic link and do not affect the original file or directory.

Important Note

Do not add a trailing slash (/) when removing a symbolic link to a directory.

Incorrect:

rm my-link/

Correct:

rm my-link

If the original file or directory is moved or deleted, the symbolic link remains in place but no longer points to a valid location.

Such a link is called a broken symlink.

Example:

rm my-file.txt

The symlink:

my-link.txt

still exists but no longer works because its target has been removed.

Common Use Cases

Create a Shortcut

ln -s /home/user/projects/project1 ~/project1

Share a Directory Between Applications

ln -s /storage/uploads /var/www/html/uploads

Switch Application Versions Easily

ln -s php83 php

Updating the symlink later allows you to switch versions without changing application configurations.

Summary

To create a symbolic link in Linux, use:

ln -s TARGET LINK_NAME

Useful commands:

Create a symlink:

ln -s source-file link-name

Create a directory symlink:

ln -s /source-directory /target-link

Overwrite an existing symlink:

ln -sf source-file link-name

Remove a symlink:

unlink link-name

or

rm link-name

Symbolic links are a powerful way to simplify file management, create shortcuts, and organize applications without duplicating files or directories.

Didn't find an answer?

Our support team is here for you 24/7 – write to us and we will help you sort it out.