How to Find and Delete Broken Symlinks on Linux
Symlink or a.k.a symbolic link is a file containing the address of some file or directory and points towards that directory or file. But, when you move or delete the original file, then the symlink gets broken, and it does not work anymore. Although the broken symlinks do not work, they consume the space on your system, and over some time, you realize that you need to get rid of them and free up some storage space.
This post will provide you with a complete and in-detail guide on finding and deleting a broken Symlink on the Ubuntu 20.04 LTS system. So, let’s start with learning how to verify a symbolic link file.
How to check a symlink?
If you want to know whether the chosen file is a symlink or not and if it is a symlink, then where it is pointing to, you can simply type the command given below to get all such information needed.
This way, you can have all the necessary information related to the symlink.
The screenshot highlighted l in the screenshot mentions that the file type is a link, and the arrow ( -> ) between the two files is pointing towards the original file.
Alright, after understanding the concept of the symlink, let’s learn to find a broken symlink and delete it.
How to Find a Broken Symlink?
To find anything in the Linux-based operating system, we can use the find command. If you want to find a broken symlink in a specific directory and its subdirectories, then first go to the directory and execute the find command using the following syntax:
The above command will fetch all the broken symlinks from the directory and its subdirectories and print them out in the terminal if there are any.
You can also provide the directory path directly to the find command, and it will also work perfectly fine. The command for doing so will go like this:
$ find /Documents/directory -xtype l
You can witness that both of the above commands have given us the same output.
After knowing about all the broken symlinks, the deletion or removal of the symlink is really simple and easy. Let’s learn to delete or remove a broken symbolic link.
How to Delete a Broken Symbolic link?
To delete or remove any file in the Linux Operating system, we can use the rm command. So, if you want to manually remove the symbolic link, then just provide the symbolic link to the rm command:
This command will not show any success output, but the broken symlink will get deleted in a blink of an eye.
Well, this is not the only way that we have to find and delete a broken symlink. We can find and delete a broken symlink in a single command using the find command as well. For deleting a symlink using the find command, type the command provided below:
$ find /Documents/directory -xtype l -delete
Using the above command, all the broken symlinks inside the directory and its subdirectories will be deleted without any hassle, and you will have a clean and clear directory free from the broken symbolic links.
Conclusion
In this post, we have walked through a simple and to-the-point guide on How to verify, find, and delete a broken symlink in any Linux Operating system. We have shown you how you can get rid of the symbolic links and free up the system storage taken by the broken symlinks.