#!/usr/bin/perl # # fileages.pl v0.0.1.1 # 10.26.07 Paul Venezia # # Recurse a directory structure, noting the atime, ctime, and mtime of each file, then display the results # # use strict; use File::Find; use Cwd; use Getopt::Std; use Switch '__'; my %opt; my ($path, $totsize, $totofiles, $totfiles, $type); my $opt_string = 'disht:p:'; getopts( "$opt_string", \%opt ); if ($opt{h}) { print "\nUsage: $0 -[dhsi] [-t (atime|ctime|mtime)] [-p ]\n\n"; print " -d detailed output\n"; print " -h this help\n"; print " -s supplemental ages\n"; print " -i ignore .snapshot dirs\n"; print " -t (atime|ctime|mtime) type of scan. Default is atime\n"; print " -p path to scan\n"; print "\nIf -p isn't specified, then the current directory is used.\n\n"; exit; } if ($opt{p}) { $path = $opt{p}; } else { $path = getcwd(); } if ($opt{t} && ($opt{t} =~ /(atime|ctime|mtime)/)) { $type = $opt{t}; } else { $type = 'atime'; } my $time = time(); my %oldfiles; my %ages; my @dirs = ($path); if (!$opt{d}) { print "\n---------------------------------\n"; print " Path: $path\n"; print " Scanning for $type\n"; print " Total files scanned: "; } sub calcsize { my $size = shift; my ($psize, $m); if ($size / 1024 > 1024000) { $psize = ($size / 1024 / 1024 / 1024); $m = "GB"; } elsif ($size / 1024 > 10240) { $psize = ($size / 1024 / 1024); $m = "MB"; } else { $psize = ($size / 1024); $m = "KB"; } return $psize, $m; } find(\&wanted, @dirs); sub wanted { next if -l || -d; if ($opt{i}) { next if $File::Find::dir =~ /\.snapshot/; } $totfiles++; if (!$opt{d}) { print $totfiles; printf "\b" x length($totfiles); } my $file = $File::Find::name; my @inf = stat($file); my $ttime; if ($opt{d}) { print "\nFile: $file\n"; printf "\tLast Access: %2d days\n", (($time - @inf[8]) / 60 / 60 / 24); printf "\tModified: %2d days\n", (($time - @inf[9]) / 60 / 60 / 24); printf "\tCreated: %2d days\n", (($time - @inf[10]) / 60 / 60 / 24); my ($psize, $m) = &calcsize(@inf[7]); printf "\tSize: %.2f $m\n", $psize; } $totsize = $totsize + @inf[7]; if ($type eq 'atime') { $ttime = @inf[8]; } elsif ($type eq 'mtime') { $ttime = @inf[9]; } elsif ($type eq 'ctime') { $ttime = @inf[10]; } my $tage = ($time - $ttime); switch ( $tage ) { case __ > 63072000 { $ages{730}{count}++; $ages{730}{size} = $ages{730}{size} + @inf[7]; } case __ > 31536000 { $ages{365}{count}++; $ages{365}{size} = $ages{365}{size} + @inf[7]; } case __ > 15552000 { $ages{180}{count}++; $ages{180}{size} = $ages{180}{size} + @inf[7]; } case __ > 7776000 { $ages{90}{count}++; $ages{90}{size} = $ages{90}{size} + @inf[7]; } case __ > 5184000 { $ages{60}{count}++; $ages{60}{size} = $ages{60}{size} + @inf[7]; } case __ > 2592000 { $ages{30}{count}++; $ages{30}{size} = $ages{30}{size} + @inf[7]; } if ($opt{s}) { case __ > 1209600 { $ages{14}{count}++; $ages{14}{size} = $ages{14}{size} + @inf[7]; } case __ > 604800 { $ages{7}{count}++; $ages{7}{size} = $ages{7}{size} + @inf[7]; } case __ > 259200 { $ages{3}{count}++; $ages{3}{size} = $ages{3}{size} + @inf[7]; } case __ > 86400 { $ages{1}{count}++; $ages{1}{size} = $ages{1}{size} + @inf[7]; } } } } if ($opt{d}) { print "\n-------------------------------------------\n"; print " Path: $path\n"; print " Total files scanned: $totfiles\n"; } else { print "\n"; } foreach my $thisage ( sort { $a <=> $b } keys %ages ) { my ($psize, $m) = calcsize($ages{$thisage}{size}); print "------\n"; print " Total files older than $thisage days: $ages{$thisage}{count} \n"; printf " Total size: %.2f $m\n", $psize; } print "------\n"; my ($psize, $m) = calcsize($totsize); printf " Total size of all files: %.2f $m\n", $psize;