{"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\u2019t point anywere?<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dfind%20.%2F%20-type%20l%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>will give me all symbolic links, but makes no distinction between links that go somewhere and links that don\u2019t.<\/p>\n<p>You are currently doing:<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201dfind%20.%2F%20-type%20l%20-exec%20file%20%7B%7D%20%5C%3B%20%7Cgrep%20broken%0A\u201d message=\u201dPython Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%24%20rm%20a%0A%24%20ln%20-s%20a%20b%0A%24%20symlinks%20.%0Adangling%3A%20%2Ftmp%2Fb%20-%3E%20a%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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\u2019t the optimal approach.<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201dfind%20%2Fpath%2Fto%2Fsearch%20-xtype%20l%0A\u201d message=\u201dPython Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 46px;\">[ad type=\u201dbanner\u201d]\n<p>is the more concise, and logically identical command to<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201dfind%20%2Fpath%2Fto%2Fsearch%20-type%20l%20-xtype%20l%0A\u201d message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dpython\u201d manual=\u201dfind%20%2Fpath%2Fto%2Fsearch%20-type%20l%20-exec%20test%20!%20-e%20%7B%7D%20%5C%3B%20-print%0A\u201d message=\u201dPython Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dpython\u201d manual=\u201dfind%20%2Fpath%2Fto%2Fsearch%20-xtype%20l%0A%20%20%20%20%20%20for%20f%20in%20%24(find%20%2Fpath%2Fto%2Fsearch%20-type%20l)%3B%20do%20if%20%5B%20!%20-e%20%22%24f%22%20%5D%3B%20then%20echo%20%22%24f%22%3B%20fi%3B%20done%0A\u201d message=\u201dPython Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<p>To display a list of the broken links,<\/p>\n[pastacode lang=\u201dpython\u201d manual=\u201dfind%20-xtype%20l%20-exec%20ls%20-l%20%7B%7D%20%5C%3B%0Afind%20%2Fusr%2Flocal%2Fbin%20-type%20l%20%7C%20while%20read%20f%3B%20do%20if%20%5B%20!%20-e%20%22%24f%22%20%5D%3B%20then%20ls%20-l%20%22%24f%22%3B%20fi%3B%20done%0A\u201d message=\u201dPython Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dpython\u201d manual=\u201dfor%20l%20in%20%24(find%20.%20-type%20l)%3B%20do%20cd%20%24(dirname%20%24l)%3B%20if%20%5B%20!%20-e%20%22%24(readlink%20%24(basename%20%24l))%22%20%5D%3B%0A%20%20%20%20%20%20%20%20%20then%20echo%20%20%20%24l%3B%20fi%3B%20cd%20-%20%3E%20%2Fdev%2Fnull%3B%20done%0A\u201d message=\u201dPython Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Works in Bash.<\/p>\n<p style=\"top: 46px;\">[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Is there a way to find all symbolic links that don\u2019t point anywere? [pastacode lang=\u201dbash\u201d manual=\u201dfind%20.%2F%20-type%20l%0A\u201d message=\u201dBash Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] will give me all symbolic links, but makes no distinction between links that go somewhere and links that don\u2019t. You are currently doing: [pastacode lang=\u201dpython\u201d manual=\u201dfind%20.%2F%20-type%20l%20-exec%20file%20%7B%7D%20%5C%3B%20%7Cgrep%20broken%0A\u201d message=\u201dPython Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] SOLUTION 1: The [&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}]}}