Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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

like image 925
Remi.b Avatar asked Jul 26 '16 21:07

Remi.b


1 Answers

-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.

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).

like image 77
Eric Renouf Avatar answered Sep 28 '22 06:09

Eric Renouf