OK, how do I count files in a directory QUICK!!!

kitepilot at kitepilot.com kitepilot at kitepilot.com
Fri Jul 2 06:17:30 MST 2010


NAHW, that's easy:
ls|wc -l 

Or I could, (couldn't I)
find . -type f|wc -l 

There are other tricks, like:
du -a|wc -l 

Etc, etc, etc... 

There are also implications of depth, whether I want only files, and on, and 
on, and on... 

I'll keep it easy:
It is a directory that only contains files.
So, you'd say:
what's wrong with "ls|wc -l" ? 

Well, here is the catch:
There are almost a million files in that directory.
And it gets worse:
This count has to be placed in a loop in a shell script to report a 
second-to-second delta. 

The truth is that find takes some 3 seconds to do the count.
What about ls without sorting?
That was almost 15 seconds. 

Now, directories are files.
It would be great if I could "count lines" on that file or somehow 
interrogate it "how many lines do you have?" without actually hitting the 
filesystem for the count.
I'm considering writing a little C utility to do just that, but...
"struct stat" (my first shot) doesn't contain that information either. 

Finally, the question is:
Is there a utility that would tell me QUICK a file count under a directory 
(regardless of type)? 

And if not, are there C/C++ system calls that would tell me that?
Thanks everyone!   :)
Enrique A. Troconis


More information about the PLUG-discuss mailing list