{"id":17290,"date":"2022-06-02T05:57:17","date_gmt":"2022-06-02T05:57:17","guid":{"rendered":"https:\/\/linuxways.net\/?p=17290"},"modified":"2022-06-02T05:57:17","modified_gmt":"2022-06-02T05:57:17","slug":"how-to-find-and-remove-broken-symlinks-on-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-find-and-remove-broken-symlinks-on-linux\/","title":{"rendered":"How to Find and Remove Broken Symlinks on Linux"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<h2>Find and Remove Broken Symlinks on Linux<\/h2>\n<p>In this method, we will see how to find and remove the broken Symlinks in a Linux OS.<\/p>\n<p>To view the Symlinks in the current directory, run the command below:<\/p>\n<pre>$ ls -l<\/pre>\n<p>In the below output, you can see two Symlinks; one pointing to a file and the other pointing to a directory.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"742\" height=\"202\" class=\"wp-image-17291\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-66.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-66.png 742w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-66-300x82.png 300w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/p>\n<p>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\u2019s delete both the file and the directory to which these Symlinks points to.<\/p>\n<pre>$ rm myfile<\/pre>\n<pre>$ rm -r myonedrive<\/pre>\n<p>Now if we run the ls command, we will see the Symlinks are listed in red color indicating they are broken Symlinks.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"157\" class=\"wp-image-17292\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-67.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-67.png 710w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-67-300x66.png 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/p>\n<p>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.<\/p>\n<p>To find all broken symbolic links in a specific directory, use the command below:<\/p>\n<pre>$ find \/path\/to\/directory -xtype l<\/pre>\n<p>To find the broken Symlinks in the current Terminal directory, use the command below:<\/p>\n<pre>$ find . -xtype l<\/pre>\n<p>This command will list only the broken Symlinks in your current directory which you can remove one by one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"377\" height=\"69\" class=\"wp-image-17293\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-68.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-68.png 377w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-68-300x55.png 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/p>\n<p>You can also find and remove the broken Symlinks using the single command as follows:<\/p>\n<pre>$ find . -xtype l -exec rm {} \\;<\/pre>\n<p>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:<\/p>\n<pre>$ find . -xtype l<\/pre>\n<p>You will see now there is no broken Symlink as they have been removed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"539\" height=\"68\" class=\"wp-image-17294\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-69.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-69.png 539w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-69-300x38.png 300w\" sizes=\"auto, (max-width: 539px) 100vw, 539px\" \/><\/p>\n<h2>Alternate Methods to Remove Broken Symlinks<\/h2>\n<p>There are two more ways that can be used to remove broken Symlinks in a Linux OS which are as follows:<\/p>\n<h3>Using the unlink Command<\/h3>\n<p>To remove a broken Symlink, type <em>unlink<\/em> followed by the Symlink name as follows:<\/p>\n<pre>$ unlink symlink_name<\/pre>\n<h3>Using the rm Command<\/h3>\n<p>To remove a broken Symlink, type <em>rm<\/em> followed by the Symlink name as follows:<\/p>\n<pre>$ rm symlink_name<\/pre>\n<p>You can also remove multiple Symlinks at once using the rm command:<\/p>\n<pre>$ rm symlink1 symlink2 symlink3<\/pre>\n<p>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.<\/p>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>","protected":false},"author":1,"featured_media":17505,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,891,5,83,165,2],"tags":[35,1012],"class_list":["post-17290","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-manjaro","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-linux","tag-symlinks"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17290"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17290\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/17505"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}