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
.
Add a comment
|
2 Answers
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
.
-
1Just 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. Apr 24, 2013 at 7:24 -
1I tried the command suggested by Petter. The
-follow
option doesn't work. Onlyfind /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 stackoverflow.com/questions/4532241/… Apr 24, 2013 at 8:50 -
How to find and delete all hard links to a file is discussed here linuxcommando.blogspot.in/2008/09/… but this doesn't work for sybmolic links. Apr 24, 2013 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 HApr 24, 2013 at 8:55
On Linux this will do it. Modify levels deep to suit you:
find /home/cc-000000025-com/domains -maxdepth 3 -type l -exec rm {} ;