Remi.b
Remi.b

Reputation: 18219

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?

Upvotes: 16

Views: 15500

Answers (1)

Eric Renouf
Eric Renouf

Reputation: 14500

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

Upvotes: 35

Related Questions