仅使用 find 列出指向有效目录的所有符号链接

muf*_*fel 6 directory find symlink

我可以用

find /search/location -type l
Run Code Online (Sandbox Code Playgroud)

列出/search/location中的所有符号链接。

如何将输出限制为find引用有效目录的符号链接,并排除损坏的符号链接和文件链接?

Gil*_*il' 6

使用 GNU find(在非嵌入式 Linux 和 Cygwin 上的实现):

find /search/location -type l -xtype d
Run Code Online (Sandbox Code Playgroud)

对于缺少-xtype主要的find 实现,您可以使用 的两次调用find,一次用于过滤符号链接,另一次用于过滤指向目录的链接:

find /search/location -type l -exec sh -c 'find -L "$@" -type d -print' _ {} +
Run Code Online (Sandbox Code Playgroud)

或者你可以调用test程序:

find /search/location -type l -exec test {} \; -print
Run Code Online (Sandbox Code Playgroud)

或者,如果你有 zsh,它只是两个全局限定符的问题@= 是一个符号链接,-= 以下限定符作用于链接目标,/= 是一个目录):

print -lr /search/location/**/*(@-/)
Run Code Online (Sandbox Code Playgroud)