{"id":1865,"date":"2017-03-23T16:57:31","date_gmt":"2017-03-23T11:27:31","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1865"},"modified":"2017-03-28T18:03:10","modified_gmt":"2017-03-28T12:33:10","slug":"can-find-broken-symlinks","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/can-find-broken-symlinks\/","title":{"rendered":"[ Solved &#8211; 5 Answers ] PYTHON &#8211; How can you find broken symlinks"},"content":{"rendered":"<p>Is there a way to find all symbolic links that don&#8217;t point anywere?<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">find .\/ -type l<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>will give me all symbolic links, but makes no distinction between links that go somewhere and links that don&#8217;t.<\/p>\n<p>You are currently doing:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">find .\/ -type l -exec file {} \\; |grep broken<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>The symlinks command can be used to identify symlinks with a variety of characteristics. For instance:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Bash Code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">$ rm a<br\/>$ ln -s a b<br\/>$ symlinks .<br\/>dangling: \/tmp\/b -&gt; a<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<p>find -L can have unexpected consequence of expanding the search into symlinked directories, so isn&#8217;t the optimal approach.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">find \/path\/to\/search -xtype l<\/code><\/pre> <\/div>\n<p style=\"top: 46px;\">[ad type=&#8221;banner&#8221;]\n<p>is the more concise, and logically identical command to<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">find \/path\/to\/search -type l -xtype l<\/code><\/pre> <\/div>\n<p>None of the solutions presented so far will detect cyclic symlinks, which is another type of breakage. this question addresses portability.<br \/>\nTo recap, the portable way to find broken symbolic links, including cyclic links, is:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">find \/path\/to\/search -type l -exec test ! -e {} \\; -print<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<p>To display a list of the broken links, replacing ${directory} with the desired root directory Edit<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">find \/path\/to\/search -xtype l<br\/>      for f in $(find \/path\/to\/search -type l); do if [ ! -e &quot;$f&quot; ]; then echo &quot;$f&quot;; fi; done<\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<p>To display a list of the broken links,<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">find -xtype l -exec ls -l {} \\;<br\/>find \/usr\/local\/bin -type l | while read f; do if [ ! -e &quot;$f&quot; ]; then ls -l &quot;$f&quot;; fi; done<\/code><\/pre> <\/div>\n<p>all one line, if there are broken links it will display them with ls -l<\/p>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<p>This will print out the names of broken symlinks in the current directory.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Python Code<\/span> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">for l in $(find . -type l); do cd $(dirname $l); if [ ! -e &quot;$(readlink $(basename $l))&quot; ];<br\/>         then echo   $l; fi; cd - &gt; \/dev\/null; done<\/code><\/pre> <\/div>\n<p>Works in Bash.<\/p>\n<p style=\"top: 46px;\">[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Is there a way to find all symbolic links that don&#8217;t point anywere? [ad type=&#8221;banner&#8221;] will give me all symbolic links, but makes no distinction between links that go somewhere and links that don&#8217;t. You are currently doing: SOLUTION 1: The symlinks command can be used to identify symlinks with a variety of characteristics. For [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4148],"tags":[3933,3942,3941,3943,3936,3934,3939,3931,3935,3937,3938,3940,3932],"class_list":["post-1865","post","type-post","status-publish","format-standard","hentry","category-python","tag-and-any-combination-thereof-leading-to-a-filedir","tag-how-can-i-relink-a-lot-of-broken-symlinks","tag-how-can-i-detect-whether-a-symlink-is-broken-in-bash","tag-how-do-i-tell-if-a-folder-is-actually-a-symlink-and-how-do-i-fix-it-if-its-broken","tag-how-to-find-dead-symbolic-links","tag-how-to-find-a-symlink-when-the-location-in-which-it-was-created-is-unknown","tag-how-to-grep-stderr","tag-how-to-prevent-find-execdir-from-following-symlinks","tag-how-would-i-find-any-symlinks-on-my-system-that-reference-tmp","tag-linux-find-broken-symlinks-with-python","tag-list-all-symbolic-links-to-valid-directories-only-with-find","tag-soft-link-detection","tag-thoroughly-find-all-links-hard-and-symlinks"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1865"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1865\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}