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

Mike stuff at dustsmoke.com
Sat Jul 3 01:05:54 MST 2010


Use readdir() to read the dir file so it doesn't have to stat() 
everything. Depending on the tool you use depends on what it does under 
the hood. Both ls and find stat() files I believe.

Example:

# time ls -l /dir/with/40k/files | wc -l
6709

real	0m2.606s
user	0m0.096s
sys	0m0.136s

# time python -c 'from os import listdir; print 
len(listdir("/dir/with/40k/files"));'
6709

real	0m0.220s
user	0m0.020s
sys	0m0.004s

-Mike

On 7/2/10 6:17 AM, kitepilot at kitepilot.com wrote:
> 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
> ---------------------------------------------------
> PLUG-discuss mailing list - PLUG-discuss at lists.plug.phoenix.az.us
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss



More information about the PLUG-discuss mailing list