在一个命令中删除文件的所有符号链接


6

我想删除文件的所有符号链接。我有一个目录结构/usr/local/instantclient/11.2.0.3,其中包含很多文件,并且在这些文件中有符号链接。/usr/local/lib/现在,我想删除这些文件的所有这些符号链接。我如何在一个命令中执行此操作。如果我删除/usr/local/instantclient/11.2.0.3包含文件的实际目录,则会将断开的链接保留在中/usr/local/lib

Answers:


12

要删除链接(从man find下方-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.

我认为这应该可以解决问题:

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

输出是否产生要删除的文件列表?如果是这样,则当您100%确定时:

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

这将仅删除断开的链接。要删除所有链接,请删除该-follow节,但是我不会在下执行该操作/usr/local/lib


1
需要澄清的是:这假定目标目录已被删除,因此链接已断开。该命令将列出目录中所有断开的符号链接/usr/local/lib(但不递归),因此,如果还有其他断开的链接,则显示的内容将超出所需。并不是说一开始就应该有很多断开的链接,所以可能还可以,但是系统可能会故意断开链接,我们希望保留这些链接。
Daniel Andersson

1
我尝试了Petter建议的命令。该-follow选项不起作用。只能find /usr/local/lib/ -maxdepth 1 -type l | delete工作。但是我认为这不是最好的方法。实际上没有办法列出文件的所有软链接,而可以使用文件的inode值列出文件的硬链接。这是关于它的讨论stackoverflow.com/questions/4532241/…–
MutantMahesh

linuxcommando.blogspot.in/2008/09/…中讨论了如何查找和删除所有指向文件的硬链接 但这不适用于符号链接。
MutantMahesh

虽然可能没有(简单的)“反向读取链接”符号链接的方法,但我不认为这与您的问题有什么关系;您说创建的链接位于/usr/local/lib?另外,-follow您的find版本是否没有选择?
Petter H
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.