#!/usr/bin/perl -w use strict; my $path "./"; my @oldies = recurse_dirs($path); print "These directories have more files unused than used: \n". join("\n", @oldies), "\n"; sub recurse_dirs { my ($path) = @_; # make sure we're dealing with a directory return () unless(-d "$path"); my $used_recently = 0; my $unused = 0; my @old; my $now = time(); my $max_age = 180*24*60*60; # Open the directory, read each file in. opendir(DIRECTORY, $path) or die "Cannot open directory $path\n"; foreach my $file (readdir(DIRECTORY)) { # we don't want . or .. next if $file eq "." or $file eq ".."; # skip if this is a symbolic link. next if -l "$path/$file"; # it's a directory. Note that by pushing the # returned list on to our list we don't accidently # clobber previous values if(-d "$path/$file") { push @old, recurse_dirs("$path/$file"); next; } # it's not a directory, so find out it's last # access time. my $last_access = (stat("$path/$file"))[8]; # increment accordingly if(($now - $last_access) > $max_age) { $unused++; # not used recently } else { $used_recently++; # used recently } } close(DIRECTORY); # There are more unused files than used recently if($unused > $used_recently) { push @old, $path; } return @old; }