-
-
Save wchristian/98bdbf20c109f300557d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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