Skip to content

Instantly share code, notes, and snippets.

@tommybutler
Created May 14, 2019 00:31
Show Gist options
  • Save tommybutler/bf49429a6bf8a6a155e2e5c57312bd3d to your computer and use it in GitHub Desktop.
Save tommybutler/bf49429a6bf8a6a155e2e5c57312bd3d to your computer and use it in GitHub Desktop.
bracketize utilized disk by last-access time in months
#!perl
use strict;
use warnings;
use Data::Dumper;
my $last_access = {};
my $tot = 0;
my $cnt = 0;
# Pipe a list of files to this script. For example:
# $ sudo find / -xdev -type f | sudo perl this-script.pl
run();
sub run
{
bracketize();
#to_csv();
debug_summary();
}
sub bracketize
{
my $now = time;
while ( my $file = <> )
{
chomp $file;
my ( $size, $atime ) = ( stat $file )[ 7, 8 ];
my $days_since = sprintf '%.f', ( ( $now - $atime ) / 60 / 60 / 24 / 30.42 );
$last_access->{ $days_since } ||= {};
$last_access->{ $days_since }{bytes} += $size;
$last_access->{ $days_since }{files}++;
$tot += $size;
$cnt++;
}
}
sub to_csv
{
my $out = "bracket,files,bytes\n";
for my $bracket ( sort { $a <=> $b } keys %$last_access )
{
my $b = $last_access->{ $bracket };
$out .= "$bracket,$b->{files},$b->{bytes}\n";
}
return $out;
}
sub debug_summary
{
my $gbt = gib( $tot );
warn Dumper $last_access;
warn <<__OUT__;
$cnt Files - $tot bytes - $gbt gibibytes
__OUT__
}
sub gib { my $s = shift; sprintf '%.2f', $s / 2**30 }
sub teb { my $s = shift; sprintf '%.2f', $s / 2**40 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment