bash - 为什么 `find -depth 1` 列出目录这么慢?

标签 bash performance find ls

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

ls -F | grep /

find . -type d -depth 1

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

什么是 find 。 -type d -depth 1 做的这么慢?

最佳答案

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

试试看

find . -maxdepth 1 -type d

它会发现比 ls -F | grep/ 因为它还会搜索“隐藏”文件,对于我的示例来说,它的速度稍快(0.091 秒与 0.1 秒相比)。

关于bash - 为什么 `find -depth 1` 列出目录这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38600497/

相关文章:

c++ - std::vector::erase 与 "swap and pop"

jQuery $ ('#submitForm' ).find ('form' ) 与 jQuery $ ('#submitForm form' )

Bash:在文件名中用空格扩展大括号和 globs?

regex - 如何在 bash 脚本中匹配正则表达式中的数字

bash - 每两周一次的 Crontab 计划

Javascript 秒表性能

c++ - 为什么对于这个涉及求幂的简单函数,clang 生成的代码比 gcc 快得多?

linux - 有什么办法可以保留文件名(+日期小时)

linux - 使用 Find -exec 增加

BASH - 在 shell 脚本上获取 UID 不起作用