Gice

Technology and General Blog

A symbolic link is a shortcut file for any directory or file. The symlink or soft link are the other names of the symbolic link. In Ubuntu, symbolic links work like a string that generates paths between various files and directories. These links are frequently utilized for linking libraries. It also ensures that files exist in consistent locations. Symbolic links are used for storing numerous copies of the same file in several locations while still referring to the same file.

What are the types of symbolic links in Ubuntu

  • Soft Link: A soft link or symbolic link refers to the original file’s location or path. On the internet, it functions similarly to a hyperlink.
  • Hard Link: A hard link works by generating a new filename related to the original file’s inode data (reference file). This is equivalent to making a copy of the specific file.

Want to create symbolic links? In this article, we will explain how you can create symbolic links in Ubuntu. So let’s start!

How to create a symbolic link for a directory in Ubuntu

Ubuntu users utilize the “ln” command for creating symbolic links in their system. This command creates hard links by default. However, you can add the “-s” or the “–symbolic” option if you want to create a soft link.

Now, check out the syntax of the “ln” command:

$ ln -s [Source_File_Path] [Symbolic_Link_Path]

To follow up the procedure of creating a symbolic link using the “ln” command, firstly, open your Ubuntu terminal by pressing “CTRL+ALT+T”. We will utilize the “ln” command for creating a symbolic link to any specific directory. Adding the “-s” option indicates that a soft link will be created for the specified directory. Here is the syntax of the “ln” command:

ln -s [Source_Directory_Path] [Symbolic_Link_Destination_Path]

We will execute the below-given command to create a soft symbolic link for the “Download” folder. This command will save the symbolic link to our “Desktop”:

$ ln -s /home/linuxhint/Download  /home/linuxhint/Desktop

The error-free output declares that the symbolic link for the “Download” folder is successfully created. We will open our “Desktop” directory using Ubuntu UI to check the existence of the “Download” symbolic link:

How to create a symbolic link for a file in Ubuntu

In this section, we will show how you can create a symbolic link for a specific file. The below-given image has a highlighted “testfile” that’s because now we will create a symbolic link for this “testfile”:

If you want to create a symbolic link for a file, then check out the syntax of the “ln” command:

$ ln -s [Source_File_Name] [Symbolic_Link_Name]

As we have shown you, we have selected our “testfile” for creating a symbolic link. This “testfile” is a text file located in our home directory. To create a symbolic link for this “testfile”, we will write out this command in our Ubuntu terminal:

$ ln -s testfile testfilelink

The execution of the above-given command will create a symbolic link named “testfilelink” of the “testfile”. The symbolic link will be saved at the location of the source file:

You can execute the “ls” command to verify the presence of the created symbolic link:

From the output, you can see that symbolic link “testfilelink” is created, and both of the files are present in the home directory:

Ubuntu UI can also show you the created symbolic link file located in the system home directory:

How to overwrite a symbolic link in Ubuntu

We have already created a symbolic link “testfilelink” for the “testfile“. So, when you execute the below-given command again in your Ubuntu terminal, it will show you a “failed to create symbolic link” error:

$ ln -s testfile testfilelink

Add the “-f” option in the “ln” command to overwrite the already created symbolic link forcefully:

$ ln -sf testfile testfilelink

How to remove symbolic link in Ubuntu

The symbolic link becomes unusable if you remove the source file or transfer it to another location. In that case, you can delete the symbolic link by using two commands: The “rm” and “unlink” commands.

First, check out the syntax of the rm command that we are going to utilize for deleting the symbolic link:

$ rm [Symbolic_Link_Path]

Now, we will execute the below-given “rm” to remove or delete the “Download” symbolic link:

$ rm /home/linuxhint/Desktop/Download

However, if the symbolic link is present inside your current working directory, you only specify its name in the “unlink” or “rm” command. Write out the below-given “unlink” command to delete the “testfilelink” symbolic link:

Now, you can execute the “ls” command to verify that the symbolic link is deleted or not:

How to find and delete broken symbolic links in Ubuntu

Ubuntu also provides you the facility to find and delete the broken symbolic link on the system. To do so, we will utilize the “find” command, having the following syntax:

$ find [Directory_Path] -xtype l

Here, “-xtype” is used to define the type of file we are searching for, and “l” denotes that we are looking for the broken symbolic links:

$ find /home/linuxhint -xtype l

The output of the above-given command will show you the broken symbolic link present in the specified directory:

You can also add the “-delete” parameter to delete those broken symbolic links from your home directory:

$ find /home/linuxhint -xtype l -delete

Conclusion

Symbolic links are a unique feature of Linux-based operating systems like Ubuntu. Symbolic links are used to create shortcuts to the Linux files system. Ubuntu users utilize the “ln” command for creating symbolic links. We have explained how you can create symbolic links in Ubuntu using the “ln” command. Moreover, we have also provided you the method to overwrite, find, and delete symbolic links in your system.

Leave a Reply

Your email address will not be published. Required fields are marked *