Need a script to move files where their last mod date <= 'xx
MMM yyyy'
Evan
evan_lsa at internode.on.net
Fri Nov 17 04:18:49 CST 2006
Evan wrote:
> List,
> I am looking at some rotation of logs etc and I don't want to lose the
> info just move it as it clutters up the directory.
>
> I will run it in a cron job but for my own edification I am after
> something to cp the files and then rm or mv for last mod < specific day.
>
> Bash script iterating will work or even Perl (I am slowly becoming
> useless in it which is 1 up from yesterday)
>
> Cheers
> Evan
List further to this query I came up with the following block of code
pieced together from googling etc
It unfortunately doesn't work based on the output im seeing. Can anyone
help.
OutPut looks like.
proxsmtpd.fXQmIR.spam ' '1163733642 ' '20061117_13:50:42
proxsmtpd.RAOc8W.spam ' '1163733653 ' '20061117_13:50:53
proxsmtpd.SPsOva.spam ' '1163733945 ' '20061117_13:55:45
proxsmtpd.1AfEx1.spam ' '1163734495 ' '20061117_14:04:55
proxsmtpd.MvcqCB.spam ' '1163734540 ' '20061117_14:05:40
proxsmtpd.U2AC6y.spam ' '1163734959 ' '20061117_14:12:39
proxsmtpd.5jICco.spam ' '1162999205 ' '20061109_01:50:05
proxsmtpd.XrPcj5.spam ' '1162999420 ' '20061109_01:53:40
proxsmtpd.XLEsGz.spam ' '1162999512 ' '20061109_01:55:12
Code is
#!/usr/bin/perl
use File::stat;
use Time::localtime;
{
my ($dir, $file, $file2, $hit, $subject, $subject2, $sender,
$sender2, $recipient, $recipient2, $date, $date2);
$dir="/var/log/copfilter/default/opt/proxsmtp/quarantine";
opendir(BIN, $dir) or die "Can't open $dir: $!";
my $cnt = 0;
foreach my $file (grep {/\./} sort by_last_mod readdir BIN){
$hit = 0;
$date = "";
$hit = 1 if -T "$dir/$file";
$cnt++;
if ($hit == 1) {
my ($sec, $tm);
$sec = stat("$dir/$file")->mtime;
print "$file ' '$sec ' '";
$tm = localtime($sec);
printf("%04d%02d%02d_%02d:%02d:%02d\n",
$tm->year+1900, $tm->mon+1, $tm->mday,
$tm->hour, $tm->min, $tm->sec);
# print "\n";
}
}
closedir(BIN);
print <<END
END
;
}
sub by_last_mod {
# vars $a and $b automatically passed in
# perl function 'stat' returns array of info on a file
# 10th element of the stat array is last modified date,
# returned as number of seconds since 1/1/1970.
my $adate = (stat(a))[10]; # get last modified date
my $bdate = (stat(b))[10]; # get last modified date
return $adate <=> $bdate;
}
More information about the linuxsa
mailing list