Use fd
instead. It's a fast alternative to find
that traverses folders in parallel
$ time find ~ -type f 2>/dev/null | wc -l
445705
find ~ -type f 2> /dev/null 0.84s user 13.57s system 51% cpu 28.075 total
wc -l 0.03s user 0.02s system 0% cpu 28.074 total
$ time fd -t f -sHI --show-errors . ~ 2>/dev/null | wc -l
445705
fd -t f -sHI --show-errors . ~ 2> /dev/null 2.66s user 14.81s system 628% cpu 2.780 total
wc -l 0.05s user 0.05s system 3% cpu 2.779 total
As you can see, to match the options of find
in fd
you'll need -sHI --show-errors
. By default fd
skips hidden files/folders and .gitignore
and also doesn't print out permission errors so it's even far faster than this.
It's possible to tune this further by printing only a new line instead of piping the whole path. In find
you can achieve that with -printf '\n'
. This isn't currently supported on fd
but it's a feature being requested
Note that in Ubuntu due to name clashing you'll need to use fdfind
instead of fd
. You can just alias fd=fdfind
to overcome the longer name. And this obviously the above command won't work for file names containing \n
. You'll need to fix it like this
fd -t f -sHI . ~ | tr '\0\n' '\n\0' | wc -l
Another nice thing about fd
is that when running it in an interactive terminal you'll also get nice colorized texts unlike the output from find
.
find -maxdepth 1
. Note that with your current approach, you'll double-count any name that contains a newline character.