Skip to content

@wchristian /largest.pl secret
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.