With Symlinks, you can have multiple access points to a file without duplicating it. Any changes made to the original file will be available in the symbolic link right away. However, if you move or delete the original file, the Symlinks to that file will be unusable or broken because it will no longer have something to point. The broken Symlink stays on the system and does not show any error until you try to use it. Therefore, it is better to remove the broken Symlinks that point to non-existent files.
This post covers how to find and remove broken Symlinks on Linux OS. We have demonstrated the commands shown here on Ubuntu 20.04, however, you can use them on any Linux distribution.
In this method, we will see how to find and remove the broken Symlinks in a Linux OS.
To view the Symlinks in the current directory, run the command below:
$ ls -l
In the below output, you can see two Symlinks; one pointing to a file and the other pointing to a directory.
A Symlink becomes broken or unusable when the file or directory it points to is removed or moved to another location. As we have 2 Symlinks in the current directory. Let’s delete both the file and the directory to which these Symlinks points to.
$ rm myfile
$ rm -r myonedrive
Now if we run the ls command, we will see the Symlinks are listed in red color indicating they are broken Symlinks.
There is another method to list the broken Symlinks in a current directory using the Find command. This command instead of listing all the directory content, lists only the broken Symlinks.
To find all broken symbolic links in a specific directory, use the command below:
$ find /path/to/directory -xtype l
To find the broken Symlinks in the current Terminal directory, use the command below:
$ find . -xtype l
This command will list only the broken Symlinks in your current directory which you can remove one by one.
You can also find and remove the broken Symlinks using the single command as follows:
$ find . -xtype l -exec rm {} ;
This command finds and removes all the broken links from the current directory. To verify if the broken links have been removed, run the command below:
$ find . -xtype l
You will see now there is no broken Symlink as they have been removed.
There are two more ways that can be used to remove broken Symlinks in a Linux OS which are as follows:
To remove a broken Symlink, type unlink followed by the Symlink name as follows:
$ unlink symlink_name
To remove a broken Symlink, type rm followed by the Symlink name as follows:
$ rm symlink_name
You can also remove multiple Symlinks at once using the rm command:
$ rm symlink1 symlink2 symlink3
Broken Symlinks should be removed from the system as they point to non-existent files or directories. In this post, we covered how to find and remove the broken Symlinks in the Linux OS using three different ways.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…
Now I want to share the DNS server installation process on your Ubuntu 20.04 server.…