20080512 cleanup old files with perl my way - plembo/onemoretech GitHub Wiki

title: Cleanup old files with perl, my way link: https://onemoretech.wordpress.com/2008/05/12/cleanup-old-files-with-perl-my-way/ author: lembobro description: post_id: 531 created: 2008/05/12 18:17:35 created_gmt: 2008/05/12 18:17:35 comment_status: open post_name: cleanup-old-files-with-perl-my-way status: publish post_type: post

Cleanup old files with perl, my way

“There’s more than one way to do it” strikes again.

Here’s mine:

`

#!/usr/bin/perl
# Cleanup files over x number of days old
my $HOME = $ENV{'HOME'};
my $bakdir = "/u01/backup";
my $errfile = "/u01/logs/backup.log";
my $timestamp = localtime();
	
open LOGZ, ">$errfile" or die $!;
	
print "$timestamptCleanup backup directory $bakdirn";
	
opendir(DIR,"$bakdir") or die "Can't open directory: $!n";
	
my @files = readdir DIR;
	
closedir DIR;
	
chdir($bakdir);
	
for my $file(@files) {
	
   print LOGZ "tchecking $filen";
	
	if (-M $file > 10) {
	
	    print LOGZ "tDeleting $filen";
	
	    unlink($file);
	
	}
	
}
	
close LOGZ;
	
__END__;

`

Basically what this does is check the target directory and delete any file whose modification date is more than 10 days old.

Copyright 2004-2019 Phil Lembo