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