Black Friday Starts Now: 50% Off Dedicated*Shop Now
Black Friday Starts Now: Up to 50% Off VPS* ††† Shop Now
Limited Inventory: High-Performance AMD-Powered Servers Now Available.* Shop Now
Reading Time: 3 minutes

A symbolic link, sometimes called a symlink or soft link, is a file in Linux that points to other files or directories (folders) and represents their absolute or relative path. A symlink is similar to shortcuts in Windows and is useful when you need quick access to files or folders with long paths.

A symbolic link points to the original file on your system but doesn't contain data in the target file. Consequently, removing the target file makes a symbolic link unusable, but removing the symlink does not affect the target file.

Hard and symbolic links are two distinct methods for referring to files in the hard drive. They are part of the filesystem that organizes files and where they reside. A hard link is a synced copy of a file that directly refers to the inode of a file. Inodes are databases that describe the file or directory characteristics, like metadata and the location on the hard drive. On the other hand, Symbolic links are shortcuts that refer directly to the file, which refers to the inode. 

Follow this tutorial and learn how to create a symbolic link to files and directories.

Prerequisites

  • A Linux server.
  • Access to a terminal with some command line knowledge.

Create a symbolic link using the below syntax. This command creates the symlink in the current directory.

ln -s /path/to/file to be linked symlink_name

Change symlink_name to your desired symlink name and /path/to/file to the actual file path. When you execute this command, you can access the original file path with the symlink name. For more details about the ln command, visit the ln man page or run man ln in your console.

The symbol used to signify a symbolic link is the (->) symbol. This symbol points to a file or its path if it is in a different location. Symbolic links have lrwxrwxrwx permissions, which are default permissions for symbolic links and cannot be changed. While lrwxrwxrwx permissions usually mean maximum allowable permissions in Linux, it is a dummy permission as symbolic links inherit the permissions of the file to which it links.

Symbolic links are also creatable using relative and absolute paths. Here are some sample outputs. In this example, sym1 is a symbolic link for a relative path and sym2 is for an absolute path.

[supendran@host symlinks]$ pwd
/home/supendran/symlinks/
[supendran@host symlinks]$ ln -s file.txt sym1
[supendran@host symlinks]$ ln -s /home/supendran/symlinks/file.txt sym2

[supendran@host symlinks]$ ls -l
total 4
-rw-rw-r-- 1 supendran supendran 0  July 22 18:12 file.txt
lrwxrwxrwx 1 supendran supendran 9  July 22 18:23 sym1 -> file.txt
lrwxrwxrwx 1 supendran supendran 30 July 22 18:24 sym2 -> /home/supendran/symlinks/file.txt

The cat command also outputs the contents of each file. For this example, they are the same.

[supendran@host symlinks]$ cat file.txt; cat sym1; cat sym2
This is my file.txt
This is my file.txt
This is my file.txt

Create a Symbolic Link to a Directory

Similar to creating a symbolic link to a file, use the following syntax to create a symlink to a directory. Change /path/to/folder to the directory path and symlink_name to the desired symlink name.

ln -s  /path/to/folder to be linked symlink_name

Here are example outputs after creating the symlink and listing the directory's contents.

[supendran@host symlinks]$ mkdir -p test/newdir
[supendran@host symlinks]$ ln -s  test/newdir/ newdir

[supendran@host symlinks]$ ls -l
total 4
lrwxrwxrwx 1 supendran supendran   18 July 22 18:28 newdir -> test/newdir
drwxrwxr-x 3 supendran supendran 4096 July 22 18:29  test

How to Disable a Symbolic Link

There are two different ways to disable a symbolic link on Linux depending on the web server software you use. Add the following command to the .htaccess or the apache config file to disable a symbolic link on apache servers.

Options -FollowSymlinks

On NGINX servers,  use their specific directive for disabling symlinks.

disable_symlinks on

How to Remove a Symbolic Link

Use either the unlink or rm commands to remove a symbolic link. If the command executes correctly, it displays no output.

rm /path/to/symlink
unlink /path/to/symlink

Notice the avoidance of the trailing slash (/) at the end of the symlink name.

Correct - /path/to/symlink
vs
Incorrect - /path/to/symlink/

It is safer to use the unlink command since it cannot remove directories. You may use the -i option to get prompted before removing the symlink using the rm command.

rm -i /path/to/symlink

You can find and remove a broken symlink under a given directory or sub-directories using the following command. It lists all the broken symlinks available for removal.

find /path/to/directory -xtype l

Here is the output.

/path/to/directory/sym1
/path/to/directory/subdirectory/sym2

Once you identify removable symlinks, use the unlink or rm commands to remove them.

Wrapping Up

Admins use a symlink for time saving measures when administering servers or personal systems. It allows you to access files or directories much faster. Use this tutorial to master the use of symbolic links.

Liquid Web offers many different hosting solutions, including VPS Hosting, Cloud Dedicated Servers, Dedicated Servers, and sophisticated options like VMware Private Cloud for enterprise workloads. Not sure which plan is best? Contact our sales team to help you get started today.

Avatar for Sapta Upendran

About the Author: Sapta Upendran

Sapta is a passionate Linux system engineer, a voracious reader, a dexterous cook, and a wanderlust. She is also interested in technical and non-technical writing.

Latest Articles

PHP 7 vs PHP 8: A Comparison

Read Article

How to Create a Symbolic Link

Read Article

What Is A Private VPS Parent?

Read Article

Managed Hosting vs Unmanaged VPS Hosting

Read Article

Why Is Your IP Address Blocked?

Read Article