#!/usr/bin/perl use v5.12; use warnings; use DateTime; use Text::CSV; my $csv = Text::CSV->new ( { binary => 1 } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); $csv->column_names ( qw( fullpath filename mtime size digest ) ); my $database = "fileinfo.db"; open my $FH, q{<:encoding(utf8)}, $database or die "$database: $!"; say "connected to database '$database' successfully"; my %files_by_sha256; my $f_sha256ref = \%files_by_sha256; while ( my $hr = $csv->getline_hr( $FH ) ) { # next if $hr->{fullpath} =~ m/tmp\.oldest/; push @{ $f_sha256ref->{ $hr->{digest} }}, $hr ; }; $csv->eof or $csv->error_diag(); close $FH; foreach my $key ( sort keys %files_by_sha256 ) { next if scalar @{ $f_sha256ref->{$key} } == 1; my %agesort; foreach my $hr ( @{ $f_sha256ref->{$key} } ) { my $timestamp = DateTime->from_epoch( epoch => $hr->{mtime} ); $agesort{ $hr->{fullpath} }->{'DATETIME'} = $timestamp->datetime(); $agesort{ $hr->{fullpath} }->{'MTIME'} = $hr->{mtime}; $agesort{ $hr->{fullpath} }->{'SIZE'} = $hr->{size}; $agesort{ $hr->{fullpath} }->{'YEAR'} = $timestamp->year(); } # Now loop over the sorted list by mtime. my $first_mtime = 0; foreach my $instance ( sort { $agesort{$a}->{'MTIME'} <=> $agesort{$b}->{'MTIME'} } keys %agesort ) { say "$agesort{$instance}->{'DATETIME'} $instance"; if ( $first_mtime == 0 ) { $first_mtime = $agesort{$instance}->{'MTIME'}; } else { # say q{rm -v "} . $agesort{$instance}->{'FILENAME'} . q{"}; } } say "\n"; } # foreach $key __END__