How to find and delete broken symlinks on Linux?

How to find and delete broken symlinks on Linux?

One of the best features available in Linux is symbolic links. In the future, there is a chance to fix in position as a broken link. It will be on your list but you cannot use that anymore just like a doll. So, you must find a way to reuse or delete it from your Linux.

Either the directories or files will point out the symlinks which are also known as symbolic links. Its presence is the same as other directories and files because it shows an entry point in a terminal window. There is no exact space for finding the symbolic links so, you need to go for a full search to find the file system tree. For instance: Let us take the symbolic link name as “dave-link” and it directly points out the file name “text-file.txt”. By now, it would start pointing the command execution one by one where the symbolic links start the search in directory or file. To fetch or view the text-file.txt content, try executing the command in symbolic links such as less or cat.

Also, you cannot count the symbolic links like 1, 2, and 3, etc. It has more consistency in the installation of Linux in all standards. The usage of the symbolic link is not only decided by you even the operating system takes the responsibility to use symbolic links. The executable files are easily pointed using symbolic links whenever the installation takes place. There is a sequence of binary file changes which is an update by new versions whenever you give an update on software.

If you change the file name then the symbolic link process will be stopped or delayed with execution. In case, if you haven’t change the name of the file and it holds the same name as before then the symbolic link will be processed like usual.

Execute the command ls in the root directory:

ls /


1609311508948.png


The –l command is used for checking interiorly. To make a check on all the bin entries and lib entry follow the execution of the below-mentioned command:

ls -l /lib* /bin

1609311521896.png


You can observe that each line is started with the word “l” because; you must indicate the symbolic link is listed with items present over here. -> - This symbol represents the symbolic links point directed towards the directory or files. Here, it points out the directories!!

You can execute, read, or write but don’t forget these kinds of permissions are only for name sake. The permissions are not allotted for pointing out symbolic links but the real host goes to the file system.

Why the symlinks are said to broken links?

The broken links are originated from the symbolic links where it is moved or deleted constantly. If you haven’t complete the update or some applications will interrupt in middle. At this time, the links are left as the broken ones. As said, symbolic links will point out directories or files if it points to a file, and without proper checking, you might delete it. So, you cannot see the presence of symbolic links anymore.

To check the behavior of symbolic link whether it is present in Linux or not start executing the command:

ls -l

Check the htg program present inside the “bin” directory. If you can see the symbolic links then it will be displayed as a program:

./hello

If you run the program directly, you can see the below-mentioned command:

../bin/htg

We have received a response in the same manner. So, start doing the deletion by executing the program file:

rm ../bin/htg

Now, the symbolic link is presented in red highlighted ones. Up to Linux, it is identified as a broken link here. You can fix the symbolic link by program recompilation and file replacement.

Run the symbolic link so that, you will get the symbolic link as a reference name instead of a file name.

./hello

1609311544506.png


How to find the broken links?

To find the broken link, we recommend you to use extended type as xtype. Search links by adding l to xtype and don’t add any other flag because; xtype is forced to get the result as broken links.

find . -xtype l

1609311572164.png


Some search is only done at home directory but it’s must search in all subdirectories without fail. At starting, we broken a link called hello is listed here. Browsers called firefox have one symbolic link and for other snaps, it holds the same.

Start piping the wc with –l for counting the lines and it relays the same for counting the broken too.

find . -xtype l | wc -l

1609311584210.png


The broken link present here is 24!!!

Now it's time, to find and analyze the broken links one by one by using the command find at starting. Some would go for direct deletion on broken links without analyzing it. In many cases, the target file creates the problem but in some cases, the symbolic links will create a problem. You might see the target is correct but the symbolic link is pointed out wrongly. It must do the creation of symbolic links again!!

The symbolic broken links are used as an indicator or it may act as a file lock. Firefox follows the same methodology but it won’t prefer using a test machine. It’s lucky for us to delete the symbolic link safely. Sometimes, the target appearance is vanished out suddenly and periodically it appears. In this case, it’s a fine point to delete by performing another function.

If you faced a problem in software installation then the standard says that it results in a broken link. Instead of deleting the symbolic link please search for fixing the software installed correctly. Once if you fix the broken link problem then it should not appear in the search result. So, make sure of this before starting the next work.

Note: Simply, set the limitation of removing the symbolic links from directories. It will help at most care.

How to remove broken links?

In search result find, do command execution as –exec and rm for removing the symbolic links. Instead of using string {}, use the broken link name. For terminating the command list, use a semicolon when running –exec. The semicolon is escaped by using \ to search in the find command.

find . -xtype l -exec rm {} \;

1609311596364.png


Now, ensure whether the broken link is deleted or not.

Use the command: find. -xtype l

1609311603287.png

You won’t get any result it means the removal of the broken link is confirmed.

Note: Take a look before removing the symbolic links from the directory or files. If the command runs from the directory “.snap” and so, you can do the deletion of symbolic links manually by stating “hello”.
Author
bhawanisingh
Views
101
First release
Last update
Rating
0.00 star(s) 0 ratings
Top