I've written a short program to recursively delete files/directories
that haven't been modified in a certain length of time. It appears to
work, but in the interest of improving my code/coding skills, I'd like
to get any/all comments and criticisms, particularly about the way
I handled getting the file information (using @ls=`ls -A $dir`). Thank
you.
        Pete

#!/usr/bin/perl -w

use strict;
use File::stat;
my $startingdir="/tmp"; # could use $ARGV[0] here to make it more
generic
my $cutoff=5;                    # could also pass in the cutoff from
the CLI

&Purge($startingdir);

########## Subroutines #############

sub Purge {
  (my $dir)=@_;
  my @ls=`ls -A $dir`;           # All files except . and ..
  foreach my $file (@ls) {
    chomp $file;
    my $date= ( -M "$dir/$file" ); # -M = age of file (at startup) in
days since modification
    if ($date>$cutoff) {           # If the file/directory is greater
than $cutoff days old ...
      print "Purging $dir/$file\n";
      system("rm -rf $dir/$file");   # Delete recursively and force it
    } elsif ( -d "$dir/$file" ) {  # otherwise if it's a directory ...
      print "Recursing into $dir/$file ...\n";
      &Purge("$dir/$file");          # Recurse into it
    }
  }
}

Reply via email to