読者です 読者をやめる 読者になる 読者になる

perlでディレクトリ配下のファイル数、合計サイズを出力

もっとスッキリさせたい

#!/usr/bin/perl

my $size = 0;
my $sizek = 0;
my $sizem = 0;
my @du;
my $dusizek = 0;
my $dusizem = 0;
my $count = 0;
my $sec;
my $min;
my $hour;
my $mday;
my $mon;
my $year;

open( IN, "ls -ltr $ARGV[0] |");
@INPUT_ARG = <IN>;
close(IN);

foreach $INPUT_ARG_TMP (@INPUT_ARG) {
	chomp ($INPUT_ARG_TMP);
	@RESULT_LIST = split(/\s+/,$INPUT_ARG_TMP);
	$size += $RESULT_LIST[4];
	$count++;
}

($sec,$min,$hour,$mday,$mon,$year) = (localtime(time))[0..5];
$year += 1900;
$mon += 1;

@du = split(/\s+/,`du -ksS $ARGV[0]`);
$dusizek = @du[0];

print "DATA			: ", "$year-$mon-$mday $hour:$min:$sec\n";
print "PATH			: ", $ARGV[0], "\n";
print "DIR SIZE [ls - KB]	: ", $size = &SIZE_CONV(KB), "\n";
print "DIR SIZE [du - KB]	: ", $dusizek = @du[0], "\n";
print "DIR SIZE [ls - MB]	: ", $size = &SIZE_CONV(MB), "\n";
print "DIR SIZE [du - MB]	: ", $dusizem = @du[0] / 1024, "\n";
print "FILE COUNT		: ", $count, "\n";
print "----------------\n";

sub SIZE_CONV {
	my ($arg) = @_;
	my ($return) = 0;
	
	if ( "KB" eq $arg) {
		$return = ($size / 1024) ;
	} elsif ( "MB" eq $arg ) {
		$return = (($size / 1024) / 1024) ;
	}
	return $return;
}