【问题标题】:How do you list all symlinks in a directory that has non-hanging links? [closed]您如何列出具有非悬挂链接的目录中的所有符号链接? [关闭]
【发布时间】:2012-02-11 16:08:34
【问题描述】:

我想获得一个目录中包含有效链接的所有符号链接的列表。换句话说,我希望在我的列表中丢弃所有损坏的链接。

【问题讨论】:

  • 最好使用 bash 或其他脚本语言

标签: linux bash symlink symlink-traversal


【解决方案1】:

在 shell 中,[ -L "$f" ] && [ -e "$f" ] 为真当且仅当 "$f" 是其目标存在的符号链接的名称。所以:

for f in *; do
    if [ -L "$f" ] && [ -e "$f" ]; then
        # do something with "$f"
    fi
done

(切勿将-a-o 选项用于test/[...];不能依靠它们来获得合理的优先级。)

【讨论】:

    猜你喜欢
    • 2020-10-07
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 2023-03-14
    • 2010-09-11
    • 1970-01-01
    相关资源
    最近更新 更多