Perl: Fastest way to get directory (and subdirs) size on unix - using stat() at the moment

Posted by ivicas on Stack Overflow See other posts from Stack Overflow or by ivicas
Published on 2010-04-21T08:35:08Z Indexed on 2010/04/21 15:13 UTC
Read the original article Hit count: 209

Filed under:
|

I am using Perl stat() function to get the size of directory and it's subdirectories. I have a list of about 20 parent directories which have few thousand recursive subdirs and every subdir has few hundred records. Main computing part of script looks like this:

sub getDirSize {
my $dirSize = 0;
my @dirContent = <*>;

my $sizeOfFilesInDir = 0;
foreach my $dirContent (@dirContent) {
   if (-f $dirContent) {
        my $size = (stat($dirContent))[7];
        $dirSize += $size;
   } elsif (-d $dirContent) {
        $dirSize += getDirSize($dirContent);
   } 
}
return $dirSize;
}

The script is executing for more than one hour and I want to make it faster.

I was trying with the shell du command, but the output of du (transfered to bytes) is not accurate. And it is also quite time consuming. I am working on HP-UNIX 11i v1.

© Stack Overflow or respective owner

Related posts about filesystems

Related posts about perl