3 # This script finds good directories for archiving based on the
4 # average age of files (access time) in the directory and the
7 # Directories whose size (in megabytes) is less than this value will
11 # Directories whose average file age (in days) is less than this value
12 # will not be displayed
13 my $average_age_minimum = 30;
17 die "Usage: $0 [directory1, directory2, ...]\n" unless @ARGV;
21 # ---------------------------------------------------------------------------
23 # Global so that the callback can see them
28 sub compute_age_callback
30 my $file_location = $File::Find::name;
33 # Return if it's the '.' or '..' files;
34 return if $filename eq '.' || $filename eq '..';
35 # Return if it's a directory
36 return if -d $filename;
38 return if -l $filename;
40 my @stats = stat $filename or die "Couldn't stat $filename: $!";
41 my $access_time = $stats[8];
44 my $current_time = time;
46 # print "# $file_location\n# Current: $current_time, Access time: $access_time\n";
47 my $age = $current_time - $access_time;
54 sub compute_directory_stats
56 my $starting_directory = shift;
62 find(\&compute_age_callback, $starting_directory);
64 return (0,0) if $total_files == 0;
66 return ($total_age / $total_files / 60 / 60 / 24,
67 $total_size / 1024 / 1024);
70 # ---------------------------------------------------------------------------
72 @ARGV = ('.') unless @ARGV;
76 foreach my $directory (@ARGV)
78 next unless -d $directory;
80 my ($average_age, $size) = compute_directory_stats($directory);
81 push @directory_stats, [$directory, $average_age, $size];
84 @directory_stats = sort { $b->[2] <=> $a->[2] } @directory_stats;
86 @directory_stats = grep { $_->[2] > $size_minimum } @directory_stats;
87 @directory_stats = grep { $_->[1] > $average_age_minimum } @directory_stats;
89 foreach my $directory_stat (@directory_stats)
91 print "$directory_stat->[0]:\n";
92 my $size_string = sprintf 'Size: %6.2f megabytes', $directory_stat->[2];
93 my $age_string = sprintf 'Average age: %6.2f days', $directory_stat->[1];
94 printf ' %-38s%s', $size_string, $age_string;