#!usr/bin/perl
# Author: Ryan Scadlock
# Date: 6 July 2002
use strict;
####
my $path = './';
die "The file $base_file does not exist!\n" if (!-f $base_file);
####
# Initiate the recursion
&RecurseDirs($path);
####
recurseDirs($path);
####
rm temp;
####
unlink "temp.txt";
####
sub RecurseDirs
{
my ($path) = @_;
my $file; #Variable for a file
foreach $path($path){
####
opendir (DIRECTORY, $path) || die "Can't read $path\n";
####
my @files = readdir(DIRECTORY);
####
if (-d "$path$file/") { #If it's a directory...
####
$path find -type f >temp.txt;
####
system("find", "$path -type f -atime +180 > temp.txt");
####
$str = wc temp -l;
$unused = int::substr($str, 0, 1);
#returns the first character as an int
####
# Count files in dir
$path find -type f >temp.txt;
$str = wc temp -l;
$count = int::substr($str, 0, 1); #returns the first
#character as an int
####
# Yes: write full dir path to file
if (($unused * 2) > $count){
$path >>result.txt;
}
####
#!/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;
}