Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to sum up the disk usage of a certain directory while in ftp? I was trying to create a script that will check what is the disk usage of the current directory and prints out the free space for the home directory.


ftp> cd /home/directory/
drw-rw-rw-   1 user     group           0 Nov 16 /directory
drw-rw-rw-   1 user     group           0 Nov 16 next/directory
drw-rw-rw-   1 user     group           0 Nov 16 next/next/directory

For some reason, I cant see any size for directories. But inside them were files that I need to check the usage, so I have to get something like this:

total disk usage for /home/directory = "some count"
total disk usage for /next/directory = "some count"
total disk usage for /../directory = "some count"
share|improve this question

I suggest you to use curlftpfs to mount your FTP repository on your filesystem. Then use the traditional du -shc . command in the folder you want to know the disk usage.

share|improve this answer
up vote 0 down vote accepted

You could use Perl. From http://aplawrence.com/Unixart/perlnetftp.html:

my $param = $ARGV[0];
# required modules
 use Net::FTP;
 use File::Listing qw(parse_dir);

 sub getRecursiveDirListing
      # create a new instance of the FTP connection
      my $ftp = Net::FTP->new("fftpserver", Debug=>0) or die("Cannot connect $!");
      # login to the server
      $ftp->login("username","password") or die("Login failed $!");

      # create an array to hold directories, it should be a local variable
      local @dirs = ();

      # directory parameter passed to the sub-routine
      my $dir = $_[0];

      # if the directory was passed onto the sub-routin, change the remote directory
      $ftp->cwd($dir) if($dir);

      # get the file listing
      @ls = $ftp->ls('-lR');

      # the current working directory on the remote server
      my $cur_dir = $ftp->pwd();

      my $totsize = 0;
      my $i = 0;
      my @arr = parse_dir(\@ls);
      my $arrcnt = scalar(@arr);
      if ($arrcnt == 0) {
        print "$cur_dir 0\n"; 
        exit 1;
      else {
      # parse and loop through the directory listing
      foreach my $file (parse_dir(\@ls))
          my($name, $type, $size, $mtime, $mode) = @$file; 
          $totsize = $totsize + $size if ($type eq 'f');

          print "$cur_dir $totsize\n" if ($i == $arrcnt);

          # recursive call to get the entries in the entry, and get an array of return values
#          @xx = getRecursiveDirListing ("$cur_dir/$name") if ($type eq 'd');

      # close the FTP connection
      # merge the array returned from the recursive call with the current directory listing
#      return (@dirs,@xx);
@y = getRecursiveDirListing ("$param");

To run it:

$ ./getSize.pl <directory>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.