#!/usr/bin/perl -w use strict; my $del_dir = "/directory/location/of/files"; my $del_days = 7; #delete files that are X days old my $now = time(); my $del_date = $now - (86400 * $del_days); #current date - (second per day * days) my @dir_list; chdir $del_dir; opendir(DH, $del_dir) or die "Unable to read $del_dir directory. ERROR: $!\n"; @dir_list = grep { /(txt)$/ } readdir(DH); #build list of files that match "txt" extension closedir(DH); foreach (@dir_list) { my ($file_time) = (stat($_)) [9]; # unlink $_ if ($del_date > $file_time); print "If the above was not commented out, $_ would have been deleted!\n"; }