【问题标题】:Why is `find -depth 1` so slow to list directories?为什么`find -depth 1`列出目录这么慢?
【发布时间】:2016-11-30 17:29:03
【问题描述】:

我在当前目录中列出目录。这是我比较的两个命令:

ls -F | grep /

find . -type d -depth 1

ls 命令是准瞬时的,而find 命令大约需要 10 秒。感觉就像find 命令正在遍历每个子目录的内容,而该命令似乎不需要它。

find . -type d -depth 1 怎么这么慢?

【问题讨论】:

    标签: bash performance find ls


    【解决方案1】:

    -depth 不会停留在单层,您需要-maxdepth。相反,它告诉find 先处理目录内容,即深度优先搜索。

    试试吧

    find . -maxdepth 1 -type d
    

    它会找到比ls -F | grep / 更多的东西,因为它还会搜索“隐藏”文件,在我的示例中,它的速度略快(0.091 秒,而 0.1 秒)。

    【讨论】:

    • FreeBSD 和 Darwin (macOS) 在其 free 命令的实现中包含一个 -depth n 主节点,这就是原始发帖人所询问的内容。这与-depth 主要(没有数字参数)非常不同。有问题的-depth n 选项是在目录树中测试深度,不会导致深度优先遍历(-depth 也是如此),也不会限制下降深度(-maxdepth n 也是如此)。
    猜你喜欢
    • 2015-03-15
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2020-02-08
    • 2012-07-17
    • 2011-11-07
    • 2015-08-24
    相关资源
    最近更新 更多