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

Brian Cluff brian at snaptek.com
Fri Jul 2 08:46:39 MST 2010


In theory you could do it like this:

- Establish the count of the files in the directory before you start 
whatever daemon is creating/deleting files
- then do something like this:
inotifywait -e delete,create -m /the/dir/to/monitor
(the command is in the inotify-tools package on ubuntu)
- Then you would just have to keep a running total of the number of 
files in your monitored directory buy adding or subtracting based on the 
event.  You should be able to do it all in shell script, but I'll leave 
that up to you :)

Brian Cluff

On 07/02/2010 06: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