perl 的一种方法:
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
open my $fh, '>:encoding(utf8)','your outfile.csv';
# write the first line with the column names
print $fh "Folder,Size,Billing Code\n";
# Do your folder calculations
my $dirpath = "/your/path";
my $bytes;
find(\&folders,$dirpath);
close $fh;
sub folders {
if (-d $_) {
$bytes = 0;
find(\&size,$_);
$bytes = $bytes/1_048_576; # Convert Bytes to Mega Bytes
print $fh $_.",".$bytes."M,your_billing_code \n";
}
}
sub size {
if(-f $_){
$bytes += -s _; # This is not a typo
# -X _ does a filesystem operation on the
# last used stat structure
# this saves new filesystem calls and therefore performance
# see http://perldoc.perl.org/functions/-X.html for more info
}
}
这会写入 CSV 文件而无需像 du 这样的外部命令,并且适用于每个平台。另请查看 Perl 的 CSV 模块以获取更多选项