# How to Symlink a Directory in Linux

Symlink, also known as a symbolic link in Linux, creates a link to a file or a directory for easier access. To put it in another way, symlinks are links that points to another file or folder in your system, quite similar to the shortcuts in Windows. Some users refer to symlinks as soft-links. Before moving forward, let’s elaborate soft-links and hard-links.

Hard-links: Hard-links are the links that mirror or copy the original file. Hard-links have the same inode numbers.

Soft-links: Soft-links are simple links that points to the original file. You can access the original file through soft links. Soft-links can point to a file or folder in any partition and have different inode numbers.

Learning about creating symlink in Linux is a great way to improve your grip on the Linux terminal. So, let’s learn the steps involved in making the soft-links in Linux.

## How to Create Symlink (soft-link) in Linux

To make symlink or soft link, we use the “**ln**” command. The syntax to follow to create symlink is mentioned below:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ln%C2%A0-s%C2%A0%5Bpath-of-the"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ln</span> <span class="re5">-s</span> <span class="br0">\[</span>path of the target file<span class="sy0">/</span>directory<span class="br0">\]</span> <span class="br0">\[</span>symbolic name<span class="br0">\]</span></div></div>In the first argument after the “-s” option, you will be giving the path of the file of a folder you want to create the symlink of. While in the second argument, pass the name you want to give that symlink. To check the created links, use the following command:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ls%C2%A0-l"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ls</span> <span class="re5">-l</span></div></div>To check inode numbers, use the command mentioned below:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ls%C2%A0-i"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ls</span> <span class="re5">-i</span></div></div>## How to Create a Symlink (soft link) to a File

Creating a soft link to a file is simple; use the syntax mentioned below:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ln%C2%A0-s%C2%A0%5Bpath-of-the-1"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ln</span> <span class="re5">-s</span> <span class="br0">\[</span>path of the target <span class="kw2">file</span><span class="br0">\]</span> <span class="br0">\[</span>symbolic name<span class="br0">\]</span></div></div>Important to note that if you do not specify the “\[symbolic name\]”, then the command will create a symlink by the original file’s name. Let’s understand it through an example.

I have created a directory “my\_folder” that contains a text file “my\_doc.txt”. Now, to create symlink to “my\_doc.txt” file, I will use:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ln%C2%A0-s%C2%A0my_folder%2Fmy"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ln</span> <span class="re5">-s</span> my_folder<span class="sy0">/</span>my_doc.txt my_document</div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image1-34.png)

To verify it, use:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ls%C2%A0-l-1"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ls</span> <span class="re5">-l</span></div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image3-34.png)

As it can be seen in the above output, “**my\_document**” is pointing to “**my\_folder/my\_doc.txt**” file. Both the symlink and the original file would have different inode number. To check inode numbers used:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ls%C2%A0-i-1"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ls</span> <span class="re5">-i</span></div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image2-34.png)

Hard links will always have same inode numbers. To verify, I created a hard link of “**my\_doc.txt**” file and name it “**my\_document\_2**”:

![](https://linuxhint.com/wp-content/uploads/2021/06/image5-30.png)

It can be seen in the output that the original file and the hard link have same inode numbers.

## How to Create a Symlink (Soft Link) of the Folder/Directory

To create a soft-link or symlink to a directory is quite similar to creating a symlink to a file. For instance, I am creating the symlink of the “**my\_folder**” directory using:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ln%C2%A0-s%C2%A0my_folder-my"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ln</span> <span class="re5">-s</span> my_folder my_doc_folder</div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image4-33.png)

The above command will create a symlinked folder in the current directory. To verify it, use:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ls%C2%A0-l-2"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ls</span> <span class="re5">-l</span></div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image7-20.png)

Now, check inode numbers:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ls%C2%A0-i-2"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ls</span> <span class="re5">-i</span></div></div>## How to Overwrite the Symlink (Soft Link) in Linux:

If you try to update a symlink with the same name that already exist, then you will get an error:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ln%C2%A0-s%C2%A0my_folder_2%2F"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ln</span> <span class="re5">-s</span> my_folder_2<span class="sy0">/</span>my_doc_2.txt my_document</div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image9-15.png)

We will have to use the force flag “**-f**” to overwrite the new path to the existing symlink.

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0ln%C2%A0-sf%C2%A0my_folder_2"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">ln</span> <span class="re5">-sf</span> my_folder_2<span class="sy0">/</span>my_doc_2.txt my_document</div></div>## ![](https://linuxhint.com/wp-content/uploads/2021/06/image8-17.png)

## How to Remove Symlink (Soft Link) in Linux:

In many situation, you need to remove the unnecessary symlinks from your system. To delete symlink, we use the “**unlink**” command, and the syntax is given below:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0unlink%C2%A0%5Bsymlink-na"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">unlink</span> <span class="br0">\[</span>symlink name<span class="br0">\]</span></div></div>Let’s remove the symlinks we created in the above examples. To unlink a symlink of a file, use:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0unlink%C2%A0my_document"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">unlink</span> my_document</div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image12-6.png)

And to unlink the symlink of a directory:

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0unlink%C2%A0my_doc_fold"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">unlink</span> my_doc_folder</div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image10-9.png)

We can also use the “**rm**” command to remove symlinks.

<div class="codecolorer-container bash blackboard" id="bkmrk-%24%C2%A0rm%C2%A0my_document-my_"><div class="bash codecolorer"><span class="co4">$ </span><span class="kw2">rm</span> my_document my_doc_folder</div></div>![](https://linuxhint.com/wp-content/uploads/2021/06/image11-6.png)

The advantage of “**rm**” over “**unlink**” is that you can remove multiple symlinks with the “**rm**” command, which is not possible with the “**unlink**” command as shown in the following image:

![](https://linuxhint.com/wp-content/uploads/2021/06/image13-6.png)

Note that whether you use the “**unlink**” or “**rm**” command, do not use trailing slash “**/**” even if it is a directory.