Skip to content

Instantly share code, notes, and snippets.

@yukioc
Created September 13, 2010 14:20
Show Gist options
  • Select an option

  • Save yukioc/577349 to your computer and use it in GitHub Desktop.

Select an option

Save yukioc/577349 to your computer and use it in GitHub Desktop.
show file size in human readable format.
#!/usr/bin/env perl
use warnings;
use strict;
die "Usage: $0 dir [dir dir ..]" if ($#ARGV<0);
sub filesize{
my $s=shift || die "filesize: lack arguments";
return "${s}B" if($s<10);
my $i=$s;
foreach my $a ('B','K','M','G','T','P','E','Z','Y'){
return sprintf "%3.1f$a",$i if($i<10);
return sprintf "%d$a",int($i) if($i<1024);
$i/=1024;
}
return $s;
}
sub du{
my $d=shift;
my @a=`du -sb $d`;
my %h;
foreach (@a){
my ($l,$n)=split;
die "Error: $n already exist\n" if($h{$n});
$h{$n}=$l;
}
return %h;
}
my %h=();
while(@ARGV){
%h=(%h,&du(shift));
}
foreach my $name (sort {$h{$b}<=>$h{$a}} keys %h){
printf "%5s %-30s\n", &filesize($h{$name}), $name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment