1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

directory size using Net::FTP or ssh

Discussion in 'Perl' started by Rakish, Dec 19, 2006.

  1. Rakish

    Rakish New Member

    Joined:
    Jun 30, 2006
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    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.
     
  3. Rakish

    Rakish New Member

    Joined:
    Jun 30, 2006
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Thanks pradeep,
    I have written a recursive routine to calculate directory size and it works great. Hope it proves helpful for others.

    Code:
            
             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";
     
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Really cool Rakish, but this will recurse only 1 level into the directory hierarchy, what if it has more than 1 levels?
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:

Share This Page