secret
Created

  • Download Gist
largest.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/env perl
 
use strictures;
use File::Find;
use Math::Round 'nearest';
 
my $path = shift or die 'Usage: largest (path) ([number of files])\nExample: largest /var 15\n';
my $count = shift || 10;
die "$path is not a directory.\n" if !-d $path;
 
print "Beginning search on $path, returning the top $count largest files.
Depending upon how many files you have, this might take a while. Please
be patient.\n\n";
 
find( { 'wanted' => \&size, 'no_chdir' => 1 }, $path );
 
my %files;
 
sub size { $files{$_} = ( stat( $_ ) )[7] if !-d $_ }
 
my @sorted = sort { $files{$b} <=> $files{$a} } keys %files;
@sorted = @sorted[ 0 .. $count - 1 ];
 
my @units = ( #
[ "G", 1024**3 ],
[ "M", 1024**2 ],
[ "k", 1024**1 ],
[ "b", 0 ],
);
 
for my $file ( @sorted ) {
my ( $unit ) = grep { $files{$file} >= $_->[1] } @units;
my $size = $files{$file} / ( $unit->[1] || 1 );
$size = nearest( .01, $size );
printf "%6.2f %s\t%s\n", $size, $unit->[0], $file;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.