Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone help me to calculate the size of the directory. I am not sure whether my code is right
#!/usr/bin/perl use File::Find; #use strict; #use warnings; my $dir = "1.0.0"; defined $dir or die "usage: timer.pl [DIR]\n"; #find(\&show_subs, $dir ); find sub {$fsize += -s},"\&calc_sub, $dir"; print $fsize; sub calc_sub { if ( -d ) { if ( $_ eq '.svn' ) { # don't traverse into subversion related directories $File::Find::prune = 1; return; } # for directories, only print path print "$File::Find::name\n"; return; } }

Replies are listed 'Best First'.
Re^5: File::Find::prune problems
by hbm (Hermit) on Jan 20, 2009 at 02:49 UTC

    I haven't used it, but it seems Filesys::DiskUsage could accomplish your goal with one line.

    Update: If you want to stick with File::Find, the following closely matches du -s or du -sk on my system:

    use strict; use warnings; use File::Find; my $dir = shift; my $k = shift || 0; die "usage: $0 <DIR> [-k]\n" unless -d $dir; my $size; find sub { next if /\.svn$/; $size += -s; }, $dir; $size = int($size/1024)."K" if $k; print "$size\n";
      Thank you. Filesys::DiskUsage was really helpful to fix my script. Thanks.