Operating System - HP-UX
1832694 Members
3184 Online
110043 Solutions
New Discussion

a perl command for `du -ks $directory`

 
SOLVED
Go to solution
David_246
Trusted Contributor

a perl command for `du -ks $directory`

Hi,

Does anyone of you know a perl command to get a (faster) output of du -ks ??
Doing a (fork) system call each time I probe a directory lasts a long time, does anyone know how to do this trick in Perl.

(Counting the sizes of localdir only might be a part of my solution as well. Just don't know how to do this.)

Thanks a lot in advance !!

Regs David
@yourservice
4 REPLIES 4
Brian Bergstrand
Honored Contributor
Solution

Re: a perl command for `du -ks $directory`

Here's something that should work.

HTH.

use File::Find;

my $base = $ARGV[0] || "./";

my $total = 0;

sub addsize {
$total += -s;
}
find(\&addsize, $base);

print "$total\n";
H.Merijn Brand (procura
Honored Contributor

Re: a perl command for `du -ks $directory`

Can't imagine it would help you gain speed. Even the simplest script is slower:

a5:/pro/local 108 > timex du -sk
324328 .

real 0.19
user 0.03
sys 0.17

a5:/pro/local 109 > timex du -sk
324328 .

real 0.24
user 0.03
sys 0.18

a5:/pro/local 110 > timex perl -MFile::Find -le'find(sub{$s+=-s},".");print$s'
406047862

real 0.49
user 0.27
sys 0.12

a5:/pro/local 111 > timex perl -MFile::Find -le'find(sub{$s+=-s},".");print$s'
406047862

real 0.41
user 0.27
sys 0.13

a5:/pro/local 112 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Brian Bergstrand
Honored Contributor

Re: a perl command for `du -ks $directory`

From what I understand, he didn't want a tool to replace du, he wanted a native perl equivalent so he didn't have to fork du from within his perl script. In this case (an existing script) the perl solution should be faster.
Stanimir
Trusted Contributor

Re: a perl command for `du -ks $directory`

# perl -MFile::Find -le'find(sub{-f and $s+=-s},"/perl");END{print$s}'

And see also attachment.
Regards.