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.
find -L . -name . -o -type d -prune -o -type l -exec rm {} +
You can also use a shell loop. The test -L matches symbolic links, and -e matches existing files (excluding broken symlinks).
for x in * .[!.]* ..?*; do if [ -L "$x" ] && ! [ -e "$x" ]; then rm -- "$x"; fi; done
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 reports l for broken links).
find -xtype l -delete
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.
find . -type l -exec sh -c 'for x; do [ -e "$x" ] || rm "$x"; done' _ {} +
The simplest solution is to use zsh. To delete all broken symbolic links in the current directory:
rm -- *(-@D)
The characters in parentheses are glob qualifiers: - to dereference symlinks, @ to match only symlinks (the combination -@ means broken symlinks only), and D to match dot files. To recurse into subdirectories, make that:
The package symlinks is pre-installed on many distributions (including Ubuntu 16.04 & Fedora 25) and has some really useful features, one of which does precisely what you're looking for:
First, change directory to the folder that contains broken symlinks, and then
run the following commands to find bad symlinks pointing nowhere:
rmlint --types="badlinks"
Then rmlint will create a bash script rmlint.sh in your current directory and print a list of bad symlinks in your terminal. To delete all the bad symlinks in your current directory, you can run
./rmlint.sh
Not exactly one liner, but it is very easy to use.