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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38600497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:56:30  来源:igfitidea点击:

Why is `find -depth 1` so slow to list directories?

bashperformancefindls

提问by Remi.b

I am listing directories in the current directory. Here are the two commands I am comparing:

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

ls -F | grep /

find . -type d -depth 1

The lscommand is quasi instantaneous while the findcommand takes about 10 seconds. It feels like the findcommand is going through the content of each subdirectory while it does not seem to be required by the command.

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

What is find . -type d -depth 1doing to be so slow?

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

回答by Eric Renouf

-depthdoes not stop at a single layer, you want -maxdepthfor that. Instead it tells findto process the directories contents before itself, i.e., a depth first search.

-depth不会停留在单层,你想要-maxdepth的。相反,它告诉find在其自身之前处理目录内容,即深度优先搜索。

Try instead

试试吧

find . -maxdepth 1 -type d

it will find more than ls -F | grep /because it will also search "hidden" files, and for my example it was ever so slightly faster (0.091 seconds compared to 0.1).

它会找到更多,ls -F | grep /因为它还会搜索“隐藏”文件,在我的例子中,它的速度稍微快了一点(0.091 秒与 0.1 相比)。