perl - quickest way to count the number of files in a directory containing hundreds of thousands of files -


in solaris system processes large numbers of files , stores information in database (yes know using database quickest way information number of files have). need fast way monitor files progress through system on way being stored in database.

currently use perl script reads in directory array , grabs size of array , sends monitoring script. unfortunately our system grows monitor getting more , more slow.

i looking method operate more instead of pausing , updating every 15-20 seconds after performing count operation on directories involved.

i relatively bottleneck read directory array operation.

i don't need information files, don't need sizes or file names, number of files in directory.

in code not count hidden files or text files use hold configuration information. great if functionality preserved not mandatory.

i have found references counting inodes c code or along lines not experienced in area.

i make monitor real-time possible.

the perl code use looks this:

opendir (dir, $currentdir) or die "cannot open directory: $!"; @files = grep ! m/^\./ && ! /config_file/, readdir dir; # skip hidden files , config files closedir(dir); $count = @files; 

what right reads whole directory (more or less) memory discard content count. avoid streaming directory instead:

my $count; opendir(my $dh, $curdir) or die "opendir($curdir): $!"; while (my $de = readdir($dh)) {   next if $de =~ /^\./ or $de =~ /config_file/;   $count++; } closedir($dh); 

importantly, don't use glob() in of forms. glob() expensively stat() every entry, not overhead want.

now, might have more sophisticated , lighter weight ways of doing depending on os capabilities or filesystem capabilities (linux, way of comparison, offers inotify), streaming dir above you'll portably get.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -