Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to remove all the symbolic links of files. I have a directory structure like /usr/local/instantclient/ 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/ containing files then it will leave the broken links in /usr/local/lib.

share|improve this question

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.

share|improve this answer
Just to clarify: this assumes that the target directory has already been removed, so the links are already broken. The command will list all broken symlinks in the /usr/local/lib directory (but not recurse), so if there are other broken links, then it will show more than needed. Not that there should be a lot of broken links there to begin with, so it's probably OK, but it is possible for a system to have purposefully broken links, and we'd like to spare those. – Daniel Andersson Apr 24 '13 at 7:24
I tried the command suggested by Petter. The -follow option doesn't work. Only find /usr/local/lib/ -maxdepth 1 -type l | delete worked. But i think this is not the best way to do it. There is actually no way to list all the soft links of a file while hard links of a files can be listed using inode value of file. Here is a discussion about it… – MutantMahesh Apr 24 '13 at 8:50
How to find and delete all hard links to a file is discussed here… but this doesn't work for sybmolic links. – MutantMahesh Apr 24 '13 at 8:54
While there may be no (easy) way of "reverse readlink" a symbolic link, I don't see how that is relevant to your problem; you said that the links created were located in /usr/local/lib? also, do you have no -follow option in your version of find? – Petter H Apr 24 '13 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .