Assuming you name this script cleanup.pl, you would use it like this:#!/usr/bin/perl use strict; use File::Find; if ($ARGV[0] eq "") { $ARGV[0]="."; } my @file_list; find ( sub { my $file = $File::Find::name; if ( -f $file && $file =~ /^DATE_/) { push (@file_list, $file) } }, @ARGV); my $now = time(); # get current time my $AGE = 60*60*24*14; # convert 14 days into seconds for my $file (@file_list) { my @stats = stat($file); if ($now-$stats[9] > $AGE) { # file older than 14 days print "$file\n"; } }
If you don't specify a directory, it will use whatever the current directory is. Note than the stats function returns an array of info, which I'm saving into the @stats array. Element 9 contains the last modification time, which may be different than the actual creation time (read up on stats so you know which one you want to use).cleanup.pl /var/backups/repository
Also, this example just prints out the files starting with DATE_ that are 14 days old (or older). Change the print statement to:
to actually delete them. This may leave you with empty directories, but you can write another script to delete empty directories after running this one.unlink $file;
In reply to Re: How do I find and delete files based on age?
by scorpio17
in thread How do I find and delete files based on age?
by macvsog
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |