#!/usr/local/bin/perl use strict; use warnings; use Net::FTP; my $ftphost = $ARGV[0] || ''; my $ftpuser = $ARGV[1] || ''; my $ftppass = $ARGV[2] || ''; my $ftpdir = $ARGV[3] || ''; my $ftp = Net::FTP->new($ftphost, Debug => 0) or die 'Cannot connect to ' . $ftphost . ': ' . $@; $ftp->login($ftpuser, $ftppass) or die 'Cannot login ' . $ftp->message; my @directories = (''); my $totalsize = 0; my $verbose = 0; while(defined(my $directory = shift @directories)) { print "checking " . $ftpdir . $directory . "\n" if($verbose); if(my $listing = $ftp->dir($ftpdir . $directory)) { for my $element (@$listing) { if(my ($permissions, $trash, $user, $group, $size, $month, $mday, $year, $name) = split(/\s+/, $element, 9)) { if(substr($permissions, 0, 1) eq 'd') { push @directories, $directory . '/' . $name; print "adding $directory/$name to list\n" if($verbose); } else { $totalsize += $size; } } } } else { die 'Cannot read ' . $ftpdir . $directory . ': ' . $ftp->message; } } $ftp->quit; if($verbose) { print "total size is: " . $totalsize . "\n" if($verbose); } else { print $totalsize; }