Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   directory size using Net::FTP or ssh (http://www.go4expert.com/forums/directory-size-using-netftp-ssh-t2297/)

Rakish 20Dec2006 01:58

directory size using Net::FTP or ssh
 
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

pradeep 20Dec2006 14:37

Re: directory size using Net::FTP or ssh
 
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.

Rakish 20Dec2006 22:15

Re: directory size using Net::FTP or ssh
 
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";


pradeep 21Dec2006 11:10

Re: directory size using Net::FTP or ssh
 
Really cool Rakish, but this will recurse only 1 level into the directory hierarchy, what if it has more than 1 levels?

pradeep 26Dec2006 21:26

Re: directory size using Net::FTP or ssh
 
Rakish, check this out http://www.go4expert.com/showthread.php?t=2348


All times are GMT +5.5. The time now is 13:29.