For a given folder, how can I delete all broken links within it?
I found this answer that shows how to delete one broken link, but I can't put that together in only one line. Is there a one-liner for this?
A broken symbolic is a link that points to a file/folder that doesn't exists any longer.
Best Answer
Here's a POSIX way of deleting all broken symbolic links in the current directory, without recursion. It works by telling
find
to traverse symbolic links (-L
), but stopping (-prune
) at every directory-or-symbolic-link-to-such.You can also use a shell loop. The test
-L
matches symbolic links, and-e
matches existing files (excluding broken symlinks).If you want to recurse into subdirectories, this technique doesn't work. With GNU find (as found on non-embedded Linux and Cygwin), you can use the
-xtype
predicate to detect broken symbolic links (-xtype
uses the type of the target for symbolic links, and reportsl
for broken links).POSIXly, you need to combine two tools. You can use
find -type l -exec …
to invoke a command on each symbolic link, and[ -e "$x" ]
to test whether that link is non-broken.The simplest solution is to use zsh. To delete all broken symbolic links in the current directory:
The characters in parentheses are glob qualifiers:
-
to dereference symlinks,@
to match only symlinks (the combination-@
means broken symlinks only), andD
to match dot files. To recurse into subdirectories, make that: