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
Why is `find -depth 1` so slow to list directories?
提问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 ls
command is quasi instantaneous while the find
command takes about 10 seconds. It feels like the find
command 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 1
doing to be so slow?
怎么find . -type d -depth 1
会这么慢?
回答by Eric Renouf
-depth
does not stop at a single layer, you want -maxdepth
for that. Instead it tells find
to 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 相比)。