Skip to content

Instantly share code, notes, and snippets.

@mperry2
Created August 16, 2013 13:38
Show Gist options
  • Save mperry2/6250046 to your computer and use it in GitHub Desktop.
Save mperry2/6250046 to your computer and use it in GitHub Desktop.
A Perl script to find the largest files in the given directories. See http://www.reddit.com/r/perl/comments/1kcfy1/largest_morning_challenge_to_you_all_improve_this/ for more information.
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
if ($#ARGV < 1) {
die "Usage: $0 <count> <dir> [dir ...]\n";
}
my $count = shift @ARGV;
my %size;
find(sub { $size{$File::Find::name} = -s if -f; }, @ARGV);
my @sorted = reverse sort { $size{$a} <=> $size{$b} } keys %size;
if (@sorted > $count) {
splice @sorted, $count;
}
my @units = (' ', qw{ K M G T });
for (@sorted) {
my $u = int( log($size{$_}) / log(1024) );
printf "%7.2f %sB %s\n", $size{$_}/1024**$u, $units[$u], $_;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment