Linux – Remove all symbolic links of files in one command

bashlinux

I want to remove all the symbolic links of files. I have a directory structure like /usr/local/instantclient/11.2.0.3 which contains lots of files and i have symbolic links of these files in /usr/local/lib/ Now i want to delete all these symbolic links of those files. How can i do this in one command. If i remove the actual dir /usr/local/instantclient/11.2.0.3 containing files then it will leave the broken links in /usr/local/lib.

Best Answer

To remove the links (from man find under -type):

          l      symbolic link; this is never true if the -L option or the
                 -follow option is in effect, unless the symbolic link  is
                 broken.  If you want to search for symbolic links when -L
                 is in effect, use -xtype.

I think this should do the trick:

find /usr/local/lib/ -maxdepth 1 -follow  -type l

Does the output produce a list of the files you want to delete? If so, when you are 100% sure:

find /usr/local/lib/ -maxdepth 1 -follow  -type l -delete

This will remove only broken links. To delete all links, remove the -follow stanza, but I wouldn't do that under /usr/local/lib.

Related Question