Hello,
I'm writing a perl script to rename some old log file with the date the file was last modified to be in the file name. Example: maillog.1.gz rename to maillog.20090213.gz.
Here's what I have written so far. It sorts the files newest to oldest and then prints the new filename with the mtime, replacing the digit between maillog and gz.
I'm not very experienced with perl, so I'm looking for advice if what I have written is any good and how it could be improved.
Thanks for your help!
#!/usr/bin/perl -w
use strict;
use File::stat;
use POSIX qw(strftime);
my $maillog_dir = '/var/log/maillogs';
opendir ( LOG, $maillog_dir) or die ("Can't open $maillog_dir: $!");
my @list = sort { -M "$maillog_dir/$a" <=> -M "$maillog_dir/$b" }
grep { -f "$maillog_dir/$_" }
readdir ( LOG );
foreach my $file ( @list ){
my $datestamp = strftime("%Y%m%d", localtime(stat("$maillog_di
+r/$file")->mtime));
$file =~ s/\d+/$datestamp/;
print "$file\n";
}
close ( LOG );