Removing Dangling Symlinks?

I spent last night setting up embedded DiVX movies on my guild’s website. To separate the upload dir from the viewing dir, I decided to symlink all the appropriate files from the viewing location to uploads (at the file level, not dir). This allows the viewing dir to have only the acceptable files (if you upload .php, for example, it’s not in the viewing dir), but avoiding copying these rather large files. All is good. Almost.

When an upload is deleted the link stays; a dangling symlink. I’d like a simple/quick/easy way to delete the symlinks which no longer point to a valid file. Clearly I could do this in Perl, but I’m thinking simpler, maybe a single shell command. Ideally /usr/bin/find would be able to filter these; hmm… off to think about this one.


This seems to work:

$ find -type l -xtype l -exec rm {} \;