What is the best way to count “find” results?

Why not

find <expr> | wc -l

as a simple portable solution? Your original solution is spawning a new process printf for every individual file found, and that’s very expensive (as you’ve just found).

Note that this will overcount if you have filenames with newlines embedded, but if you have that then I suspect your problems run a little deeper.

Leave a Comment