Linux Commands

How to create a soft link in Linux?

You may have heard about the symbolic link in Linux. It is a document that serves as a shortcut to some folder or document. The symbolic link is also known as a soft link. These connections are commonly used to connect libraries. Symbolic links allow multiple clones of the same document to be stored in different destinations whilst also pointing to the same document. The address or path of the original document is referred to via a soft link or symbolic link. It works analogously to a hyperlink on the web. So, if you want to create a soft link in Linux, you should look at this article from start to end. Thus, Let’s get started.

Soft Link for Directory:

We will be using the Ubuntu 20.04 shell terminal via “Ctrl+Alt+T.” After opening the shell, let’s take a fresh start by listing the simple files and folders in our home directory of the Ubuntu 20.04 system. There are no soft links in the home directory right now. The execution can be checked in the below image.

We want to create a soft link for the “Music” directory within the “Desktop” folder listed in the output above. Firstly, we will move into the “Desktop” directory and see if there is any soft link already created or not. We will be using the “cd” instruction and the path towards the “Desktop” directory. Then, we will utilize the list instruction to display the “Desktop” folder data. Right now, it is empty as per the output. The execution can be checked in the below image.

Let’s come back towards the home folder by the “cd” instruction on the shell via double dots. To create a symbolic link, we should be using the “ln” command. This may create a hard link instead of a “soft” link. So, to create a soft link, Linux came up with the “-s” option within the instruction. Along with the “-s” flag, you have to mention the directory path for which we created a soft link and the path to the directory where we think the soft link will be created. This command will return nothing and create a soft link in the destination folder. The execution can be checked in the below image.

Once again, we need to move within the “Desktop” folder by using its path in the “cd” instruction. After moving in, we need to list its contents. You will see that the soft link for folder “Music” has been created in the “Desktop” directory of Ubuntu 20.04. You will see that the soft link will be shown in more highlighted blue color than the normal directories display. The execution can be checked in the below image.

You can also open the file explorer of an Ubuntu 20.04 system and tap on the “Desktop” folder to see the soft link within it. You will see that the soft link for the directory “Music” will be shown as a directory icon with the arrow sign on it as per the below image output.

Soft Link for File:

You may have thought that creating soft links for simple files might be similar to soft links for directories. But, this is not true. To create a soft link for a simple file or document, we must opt for a completely different method. To do this, you must have a file on your system first. If not, try making one using “touch” instruction on the shell. We have been creating a new text type file, “new.txt,” with the “touch” command as demonstrated. After creating a file, we have utilized the “ls” list command to display the files and folders in the home directory. The newly created text file “new.txt” is also on the list. The execution can be checked in the below image.

We will be using the “ln” instruction along with the “-s” option to create a soft link for the “new.txt” file. You don’t need to use the path to the file while you have been working in the home directory. You have to mention the name of an original text file, i.e., “new.txt,” first and then add the new name for a file as a weak link with the same extension, i.e., “newlink.txt.” The “ls” command has been showing that the soft link “newlink.txt” is created for file “new.txt.” The execution can be checked in the below image.

Looking at the home directory via the file explorer, we found the soft link “newlink.txt” for the file “new.txt” at the same location.

If you want to overwrite the already created soft link for any specific file, the system will not allow you to do that. For instance, try using the same “ln” instruction with the “-s” option and same filename and same name “soft” link. The execution will show you an error as beneath.

In any case, if you want to overwrite the already created soft link at any cost, you have to use the “-f” option in combination with the “-s” option of the “ln” instruction as beneath. The execution can be checked in the below image.

To remove a soft link, use the “rm” instruction and the soft link’s name on the shell. It will delete the link from your system. The execution can be checked in the below image.

Conclusion:

This is all about creating a soft link in Ubuntu 20.04 system. We have taken a separate look at creating a soft link for a file and a soft link for a directory. After taking a good look at this guide, we hope you will be able to create soft links for files and folders now.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.