Linux find and remove all symlinks relating to a directory

symlink

So, I have a bit of a situation where I created many symlinks in an attempt to get them to work, trying all sorts of combinations .. now I have this error:

ls: cannot access /etc/sv/me: Too many levels of symbolic links

There is no symlinks in /service/ or /etc/sv/
The only way I can find to solve this error is to remove all of the symlinks relating to /service/ or /etc/sv/ but i don't even see the symlinks?

Best Answer

You can identify cyclic symlinks with a bit of find trickery, try this:

find /path/to/search -type l -a ! \( -xtype b -o -xtype c -o -xtype d -o -xtype p -o -xtype f -o -xtype s -o -xtype l \) 2>/dev/null

This works by filtering for symlinks, then excluding anything where the type of the symlink's target is any of the possible inode types. The only things are left are those where find can't determine the type of the target, which only happens for cyclic symlinks (broken ones match -xtype l)

Related Question