Hi,
 
I'm wondering if there is a way to get exactly the same result of system fonction "du" in Perl.
 
I need to know the disk Space tooken by a directory and it's sub-directory. To do that, I used the find method in File::Find to get the size (using -s file test) of each file and dir in a directory, recursivly.
 
But the size I get is never the same as du ... why ? Both give a result in bytes, shouldn't it be the same ?
 
 
#!/usr/bin/perl -w
 
use strict;
use File::Find;
 

###Subs ...
#####################################################################
#       sub MyTraverseFolder   
#
#       Recursive Function
#####################################################################
sub MyTraverseFolder($&) {
  my $dir       = shift;
  my $wanted    = shift;
 
  find($wanted, "$dir");
}
 
#####################################################################
#       sub getsize()
#
#       modify the $main::size_value variable to add it the current
#       File::Find::name 's size in bytes;
#####################################################################
sub getsize() {
my $_size = -s $File::Find::name;
  $main::size_value += $_size;
    # debug it ...
  print sprintf("%d",(-s $File::Find::name)),"\t$File::Find::name\n";
}
 

###Main ...
#####################################################################
 
$main::size_value = 0;
 
MyTraverseFolder("$ARGV[0]",\&getsize);
print "Total size: [",sprintf("%d",($main::size_value)),"] \n";
 
For example on /etc , su gives 3552 kB, mydu gives 2882 kB !!!
 
I did not find any module that was doing the du function .
 
THanks for your help
 
 
Fred

Reply via email to