有没有办法总结某个目录的磁盘使用情况文件传输协议?我试图创建一个脚本来检查当前目录的磁盘使用情况并打印出主目录的可用空间。
例子:
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
由于某种原因,我看不到目录的任何大小。但它们里面有我需要检查使用情况的文件,所以我必须得到这样的东西:
total disk usage for /home/directory = "some count"
total disk usage for /next/directory = "some count"
total disk usage for /../directory = "some count"
答案1
我建议你使用卷曲文件系统将 FTP 存储库挂载到文件系统上。然后du -shc .
在想要了解磁盘使用情况的文件夹中使用传统命令。
答案2
你可以使用 Perl。从http://aplawrence.com/Unixart/perlnetftp.html:
#!/usr/bin/perl
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";
$ftp->quit();
exit 1;
}
else {
# parse and loop through the directory listing
foreach my $file (parse_dir(\@ls))
{
$i++;
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
$ftp->quit();
}
# merge the array returned from the recursive call with the current directory listing
# return (@dirs,@xx);
}
@y = getRecursiveDirListing ("$param");
运行它:
$ ./getSize.pl <directory>