How to Create a Symbolic Link

Posted on by Matthew Stevens | Updated:
Reading Time: 3 minutes

A symbolic link, also known as a soft link or symlink, is a special file pointing to another file or directory using an absolute or relative path. Symbolic links are similar to shortcuts in Windows and are useful when you need quick access to files or folders with long paths.

Symbolic links don't contain data in the target file since the links only point to different files in the file system, but removing the target file will make symbolic links unusable.

Follow the below tutorial to learn how to create a symbolic link to both files and directories. 

Create a Symbolic link to a File

Create a symbolic link using the below syntax.

ln -s /path/to/file symlink_name

I created two symbolic links using relative and absolute paths.

  • Absolute Path: A path that starts from the root directory (beginning with /) down to the file’s location. All of the information that is needed to find the specific file is in the path string.
  • Relative Path: A path from the current location. This path starts with a period representing the current directory or two periods for the directory above the current location.

The symbol used to signify a symbolic link is the -> symbol. This symbol points to a file or a file’s path if it is in a different location. Symbolic links have lrwxrwxrwx permissions, which are default permissions for symbolic links and cannot be changed.

Output the contents of the current folder using the ls command, which shows the two symbolic links I created. The symbolic link sym1 and sym2 are both pointing to the same file.

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

[mstevens@host symlinks]$ ls -l
total 4
-rw-rw-r-- 1 mstevens mstevens  7 May 23 12:23 file.txt
lrwxrwxrwx 1 mstevens mstevens  8 May 23 12:23 sym1 -> file.txt
lrwxrwxrwx 1 mstevens mstevens 32 May 23 12:24 sym2 -> /home/mstevens/symlinks/file.txt

Here we can use the cat command to output the contents of each file. As shown below, the contents are the same.

[mstevens@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

To create symbolic links to a directory, we can use the same command to create symbolic links to a file.

[mstevens@host symlinks]$ mkdir -p folder/123/abc
[mstevens@host symlinks]$ ln -s folder/123/abc/ abc

[mstevens@host symlinks]$ ls -l
total 4
lrwxrwxrwx 1 mstevens mstevens   15 May 23 12:53 abc -> folder/123/abc/
drwxrwxr-x 3 mstevens mstevens 4096 May 23 12:55 folder

We can then access the abc folder through the folder/123/abc/ path and the symlink abc.

In the example below, we created a file inside the abc folder. When checking the abc folder, we can use the pwd command and see that the path is different when checking the current directory.

[mstevens@host symlinks]$ touch folder/123/abc/File1

//accessing through folder
[mstevens@host symlinks]$ cd folder/123/abc/

[mstevens@host abc]$ ls -l
total 0
-rw-rw-r-- 1 mstevens mstevens 0 May 23 12:58 File1

[mstevens@host abc]$ pwd
/home/mstevens/symlinks/folder/123/abc

//accessing through symlink
[mstevens@host symlinks]$ cd abc/

[mstevens@host abc]$ ls -l
total 0
-rw-rw-r-- 1 mstevens mstevens 0 May 23 12:58 File1

[mstevens@host abc]$ pwd
/home/mstevens/symlinks/abc

Hackers usually exploit symlink functionality to access parts of the server that are not accessible to regular users. Disabling symlinks on apache servers can be done by adding the following command in .htaccess or the apache config file.

Options -FollowSymlinks

Nginx has its own directive for disabling symlinks.

disable_symlinks on

CloudLinux has its way of disabling symlinks to the files/folders with different owners.

fs.enforce_symlinksifowner=1

To remove symbolic links, we can use one of two commands: rm or unlink.

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

Both commands will remove symbolic links but unlink is the safer command. Use caution when using the rm -r command as the folder’s contents where the symlink is pointing will be removed. 

Below is the proper way to remove a symlink.

[mstevens@host symlinks]$ ll
total 4
lrwxrwxrwx 1 mstevens mstevens   15 May 23 12:53 abc -> folder/123/abc/
drwxrwxr-x 3 mstevens mstevens 4096 May 23 12:55 folder
[mstevens@host symlinks]$ unlink abc
[mstevens@host symlinks]$ ls -l
total 4
drwxrwxr-x 3 mstevens mstevens 4096 May 23 12:55 folder

Avoid removing a symlink improperly, as shown below.

//Contents of abc folder
[mstevens@host symlinks]$ ls -l abc/
total 0
-rw-rw-r-- 1 mstevens mstevens 0 May 24 16:30 File1

[mstevens@host symlinks]$ rm -r abc/
rm: cannot remove 'abc/': Not a directory

//checking contents again
[mstevens@host symlinks]$ ls -l abc/
total 0
[mstevens@host symlinks]$

Conclusion

Symbolic links allow you to access specific files or directories from your current location, which is similar to how we use desktop shortcuts. If a symbolic link is no longer required, it can be removed but be mindful of the command being used to avoid deleted important files accidentally.

Liquid Web offers many different hosting solutions. Not sure which plan is best? Contact our Sales team to help you get started today!

About the Author: Matthew Stevens

I'm a system administrator, developer, and I'm constantly improving and learning new skills. In my spare time I keep myself in shape with dancing... breakdancing!

Have Some Questions?

Our Sales and Support teams are available 24 hours by phone or e-mail to assist.

1.800.580.4985
1.517.322.0434

Latest Articles

5 Alternatives to GitHub

Read Article

5 Steps to Install and Configure osquery on CentOS 7 & 8

Read Article

Five Steps to Create a Robots.txt File for Your Website

Read Article

Premium Business Email Pricing FAQ

Read Article

Microsoft Exchange Server Security Update

Read Article