Skip to content

Instantly share code, notes, and snippets.

@ryochin
Created July 24, 2013 03:40
Show Gist options
  • Save ryochin/6067914 to your computer and use it in GitHub Desktop.
Save ryochin/6067914 to your computer and use it in GitHub Desktop.
make a sorted list of 'tmutil compare' by file size, in order to find large ones.
#!/usr/bin/env perl
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
use Number::Bytes::Human ();
use utf8;
my $from = shift @ARGV or die "usage: $0 <from> <to>";
my $to = shift @ARGV or die "usage: $0 <from> <to>";
open OUTPUT, "/usr/bin/tmutil compare -s $from $to |" or die $!;
my $list = {};
while(<OUTPUT>){
chomp;
next if $_ !~m{/}o;
my ($mark, $size, $path) = split /\s+/o, $_, 3;
next if $path eq '';
next if $size eq '0B';
$path =~ s{^\(\w+\)\s+}{}o;
$path =~ s{^.+/\d{4}-\d{2}-\d{2}-\d{6}}{}o;
$path =~ s{^/[\w\d]+ HD}{}o;
$size = &size2byte( $size );
# omit small files
next if $size < 10 * 1024 ** 2;
push @{ $list->{ $size } }, $path;
}
for my $size( sort { $b <=> $a } keys %{ $list } ){
printf "%s%s%s\n\n", &size2color( $size ), Number::Bytes::Human::format_bytes( $size ), RESET;
for my $path( sort @{ $list->{ $size } } ){
printf " %s\n", $path;
}
print "\n";
}
sub size2byte {
my $size = shift;
my $n = substr $size, 0, -1;
$n = int $n;
if( $size =~ /K$/o ){
return $n * 1024;
}
elsif( $size =~ /M$/o ){
return $n * 1024 ** 2;
}
elsif( $size =~ /G$/o ){
return $n * 1024 ** 3;
}
elsif( $size =~ /T$/o ){
return $n * 1024 ** 4;
}
else{
return $n;
}
}
sub size2color {
my $size = shift;
if( $size > 1 * 1024 ** 3 ){
return RED;
}
elsif( $size > 100 * 1024 ** 2 ){
return MAGENTA;
}
else{
return YELLOW;
}
}
__END__
@Arroyodorado
Copy link

Hello ryochin,
was really happy to find your script, but it shows an error on my system: "Can't locate Number/Bytes/Human.pm in @inc"
What am I missing?
Thanks in advance,
arroyodorado

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment