I need to delete files and the directories that contain them when they, the files (and directories; they'll be of the same date), are older than a specified number of days.
I'm using the following code...
#!/usr/bin/perl -w
# cleandumps.ps -clean pgsql dump files older than 7 days
use File::Find qw(finddepth);
*name = *File::Find::name;
finddepth \&cleanfiles, "/var/lib/pgsql/backups/daily/";
sub cleanfiles{
if(-M > 6){
if(!-l && -d _){
print "rmdir $name\n";
#rmdir($name) or warn "couldn't rmdir $name: $!";
} else {
print "unlink $name\n";
#unlink($name) or warn "couldn't unlink $name: $!";
}
}
}
...and it works up to a point. If I just print the results, it shows everything exactly as I need, but if I actually perform the unlink operation, it changes the date on the directory that contains the files. At that point, the directory no longer meets the age requirement and doesn't get cleaned up. Anyone have an idea of how I can perform this kind of recursive deletion based on age?
I'm using the following code...
#!/usr/bin/perl -w
# cleandumps.ps -clean pgsql dump files older than 7 days
use File::Find qw(finddepth);
*name = *File::Find::name;
finddepth \&cleanfiles, "/var/lib/pgsql/backups/daily/";
sub cleanfiles{
if(-M > 6){
if(!-l && -d _){
print "rmdir $name\n";
#rmdir($name) or warn "couldn't rmdir $name: $!";
} else {
print "unlink $name\n";
#unlink($name) or warn "couldn't unlink $name: $!";
}
}
}
...and it works up to a point. If I just print the results, it shows everything exactly as I need, but if I actually perform the unlink operation, it changes the date on the directory that contains the files. At that point, the directory no longer meets the age requirement and doesn't get cleaned up. Anyone have an idea of how I can perform this kind of recursive deletion based on age?