如何获取系统或某个目录中所有链接文件的列表。我曾经创建链接,但随着时间的推移,它们变得无法管理。我想要一个目录中所有这些链接的列表。有人可以帮忙吗?

有帮助吗?

解决方案

查找符号链接很简单:

% find . -type l

查找硬链接很棘手,因为如果相关目录的子目录也有子目录,那么这些会增加硬链接数。这就是子目录在UNIX中与父类链接的方式(它是每个子目录中的..条目)。

如果您只想查找链接的文件(而不是目录),这将有效:

% find . -type f \! -links 1

这是有效的,因为 的文件具有硬链接将具有链接计数<!> gt; 1,未链接文件的链接数== 1,因此该命令查找链接数<!> lt; <!> gt;的所有文件; 1

或者,在较新版本的find上,您可以使用:

% find . -type f -links +1

这与上述原因相同;但是,较新版本的find可以使用+ n或-n而不只是一个数字。这相当于分别测试大于n或小于n的值。

其他提示

find / -xdev -samefile filename

@OP,如果您有GNU查找,您可以使用-printf "%n"查找硬链接, e.g。

find /path -type f -printf "%f/%n/%i\n" | while IFS="/" read filename num_hlinks inum
do
 echo "Filename: $filename. Number of hard links: $num_hlinks, inode: $inum"
 # if 2 or more files have the same inode number, then they are hard links. 
 # you can therefore count how many $inum that are the same and determine those hard links, which 
 # you have to try doing yourself.
done

参见例如这里 https://www.gnu.org/software/findutils /manual/html_node/find_html/Hard-Links.html 或者将Alnitak和amber_linux的答案结合起来 find -L /where/to/search -samefile /some/link/to/file 查找指定文件的所有硬链接和软链接。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top