In Linux, Symbolic links are also known as symlinks and are similar to the shortcuts made in the Windows operating system. The symlinks refer to either a directory or a file on your system. These links are added as per the requirements and can be removed easily. This post will demonstrate the possible methods to remove or delete a symbolic link in Linux.
- Method 1: Using the rm Command
- Method 2: Using the find Command
- Method 3: Using the unlink Command
- Bonus-Tip: Difference between the rm, find, and unlink Methods?
Let’s get started!
Method 1: Using the rm Command
The “rm” command utility can remove the symbolic links in the Linux. The rm command can be used with the “-i” option, which will ask before deleting the symbolic link, and the multiple symlinks can also be deleted using the rm command.
The general syntax for removing the symbolic link is provided below:
$ rm -vi <symlink_1> <symlink_2>
In the above syntax, the rm command is used with its “v option that is used to display the verbose” and “i option is used to ask the confirmation before executing the command” to remove the symbolic links.
To understand the usage of the “rm” command for removing the symbolic links, we have multiple symbolic links as highlighted below:
To remove the symbolic links mentioned in the above output, we will see the next examples.
Example 1: Removing a Single Symbolic Link
To remove the symbolic links, use the “rm” command as follows:
$ rm -vi softlink.txt
The symlink of the “softlink.txt” has been deleted, and this can be verified by using the command:
The symbolic link has been deleted successfully.
Example 2: Removing Multiple Symbolic Links
To delete the multiple symbolic links in a single command, the “rm” command can also be used. For example, we will delete two symbolic links by running the command:
$ rm -vi softlink1.txt softlink2.txt
The symbolic links have been deleted successfully.
Method 2: Using the find Command
The next method of deleting the symbolic links in Linux is using the find command utility. When the symlinks are removed from the Linux, sometimes they get broken, and to remove these broken symlinks, the find command is used. The syntax of using the find command to remove the broken links:
$ find <dir_broken_link> -xtype l -delete
In the above general syntax, the find command will find out the broken links with its type and delete them. To delete the broken symbolic links, we will first list them with the ls command:
The red color files are the broken symbolic links that can be deleted using the find command with its delete option, as explained in the next examples.
Example 1: Remove Single Broken Symbolic Link
To remove the single broken symbolic link, we will use the command:
$ find softlink3.txt -xtype l -delete
The broken symbolic link mentioned in the above command will be deleted.
Example 2: Remove Mulitple Broken Symbolic Links
To remove the multiple broken symbolic links using the find command, we can run the command:
$ find softlink4.txt softlink5.txt -xtype l -delete
To confirm the deletion of those mentioned above broken symbolic links, we will list down the contents of the directory with the ls command:
The broken symbolic links have been deleted successfully.
Method 3: Using the unlink Command
The last approach to remove the symbolic links is using the unlink command, but his command will delete the link of the source file with the symbolic links in Linux. The command is used following the syntax provided below:
$ unlink symlink_name
The symlink will be deleted, and remember that this command will accept only one argument at a time. To understand the usage of the unlink command, we will delete the symbolic link:
$ unlink softlink8.txt
To confirm the execution of the above command, display the contents:
The symbolic link mentioned in the above command has been deleted.
Bonus-Tip: Difference between the rm, find, and unlink Methods?
In the above sections, we have used three methods to delete or remove the symbolic links in Linux. There is a difference in all three methods, which are explained in the table below:
|rm||The rm command will delete the symbolic link files as it is used to delete other files from the Linux.|
|find||The find command with the “delete” option will delete only the broken symbolic links in Linux|
|unlink||The unlink command will delete the link of the symbolic link with its source file, resultantly deleting the symbolic link.|
That’s all from this post!
To remove or delete the symbolic links in Linux, we can use the “rm”, “find”, and “unlink” command utilities. In this blog, three different approaches are explained with the help of examples to delete the symbolic links in Linux. The find command is used to delete the broken symbolic links, while the other two command utilities, rm, and unlink, are used to delete the symbolic links.