directory size using Net::FTP or ssh

Rakish's Avatar, Join Date: Jul 2006
Go4Expert Member
Hi all,

I have user directories on a FTP server, and I am usign Net::FTP module to manage directories...

I can find the file size using ftp->size(); function but i am unable to get the directory size.

I am trying to do as under:

Code:
my $ftp = Net::FTP->new("127.0.0.1", Debug => 0)   or die "Cannot connect to FTP Server: $@"; # connect
$ftp->login("ftp123",'xxxxxxx')  or die "Cannot login ", $ftp->message; # login
my $path="/home/temp/1/";
my $size=$ftp->size("$path");
this returns nothing,

Please let me know How I can find the directory size.

Also, can someone tell me how to get the directory size from an FTP Server using
ssh. I want to the Unix du command to get the directory size as it will be a lot faster.

Thanks a ton,

Rakish
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
You can't get the size of the directory using Net::FTP. FTP doesn't transmit the size of a directory, you have to do a recursive calculation to get the size of a directory.
Use a command line FTP tool to view a FTP directory listing, it never shows the size of the directory.
0
Rakish's Avatar, Join Date: Jul 2006
Go4Expert Member
Thanks pradeep,
I have written a recursive routine to calculate directory size and it works great. Hope it proves helpful for others.

Code: Perl
my $path="/home/ftp/".$login_id;
         my @dir=$ftp->ls or $newerr=1;
     my @files;
     my $project;
         $size=0;   
         foreach(@dir)
     {   
         my $project="/home/ftp/".$login_id."/".$_;       
         $ftp->cwd("$project") or die "Cannot change working directory ",
                 $ftp->message; # change dir if required
         @files=$ftp->ls or $newerr=1;       
             foreach(@files)
             {
                 $path=$project."/".$_;       
                 $size=$size + $ftp->size("$path");               
             }
     }
         print "Size = $size";
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Really cool Rakish, but this will recurse only 1 level into the directory hierarchy, what if it has more than 1 levels?
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Rakish, check this out http://www.go4expert.com/showthread.php?t=2348