What do you do when find cannot handle the sheer number of files, argument too long from the linux hardcoded MAX_ARG_PAGES
We need a dedicated utility that counts inodes.
What do you do when find cannot handle the sheer number of files, argument too long from the linux hardcoded MAX_ARG_PAGES
We need a dedicated utility that counts inodes.
The output from find is a list of filenames separated by newlines. The wc command is reading from stdin and counting those newlines[1] -- wc itself has just two arguments in its argv array here ("wc", "-l"). You will not exceed its argument list. Further, it is not at all a hack to count the number of files this way; rather using pipes to compose functionality is the Unix way[2].
But fwiw, if you want a count of the inodes in use in the entire filesystem, you can get that directly from "df -i".
In any case, as an exercise to the reader, I encourage you to grab the source to find and add the -count output option you desire.
[1] Technically a filename can contain newlines, so this would throw off the count. The fix for that would be to use find's -print0 output option and then pipe to something which can count on the nulls. In practice, you're unlikely to have such filenames.
Also, to count the number of files in a directory, you need to do readdir(), which will get you the names. Then, to see if any of them are directories to be recursed into you need to stat() the names individually. This is all "find" does. How, I'd very much like to know, is this "a nasty hack"?
All we want from find is "count the number of inode entries in this tree branch that are type f". No formatting, no output, just count. That has to be much more efficient.
https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux....
That's "time find . | wc -l"
It's going to be disk limited unless you're in cache.
Switching to "time find . -printf '1' | wc -c" lowers overhead to .7 seconds, so roughly 1us/file.