Need a script to move files where their last mod date <= 'xx MMM yyyy'

Evan evan_lsa at internode.on.net
Fri Nov 17 12:10:29 CST 2006


Thanks Daryl
I look forward to trying this out.
Also for general interest I have decided to go one better for it's use 
and amend a scripy (in perl) ued by copfilter to help us control spam.


Daryl Tester wrote:
> Evan wrote:
>
>> 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.
>
> This will probably get me kicked out of the Python club, but here goes:
>
>> #!/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){
>
> Unless you run this script in the directory "/var/log/.../quarantine",
> all the stat() calls that by_last_mod makes will fail because they
> are being passed in file names without the directory prefix - i.e.
> "file1", not "/path/to/file1".
>
> Using "$dir/$a" in by_last_mod's stat call probably won't work
> because $dir is declared a local variable, and won't be seen by
> by_last_mod (out of scope).
>
>>           my ($sec, $tm);
>>           $sec = stat("$dir/$file")->mtime;
>
> Remember this for the next point.
>
>> 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
>
> I've no idea why Perl didn't spit over this - a and b should be
> scalar variables for starters.  Also, mtime is 9, not 10, according
> to the perlfunc man page.  Plus, because you're using File::stat
> (as shown in the main loop above), this should read as
> "stat($a)->mtime".  And yet, the stat will still fail because it
> doesn't have the full path of the file (unless there is a file of
> the same name in the current directory).  At least the "->mtime"
> form of File::stat should blow up more spectacularly (this is
> my biggest beef about Perl - silent errors).
>
> For debugging purposes, if you at this point stick in a
>
> print "$a, $adate, $b, $bdate\n";
>
> you can see the results being returned aren't the ones you want.
>
> So, given that, I would modify the foreach loop to add the path
> to the filename (actually, I'd break it up for readability, but
> you're already using grep and sort in list):
>
>>     foreach my $file (grep {/\./} sort by_last_mod  readdir BIN){
>
>    foreach my $file (grep {/\./} sort by_last_mod map { "$dir/$_" } 
> readdir BIN) {
>
> Remove all the "$dir/$file" strings in the main loop, because $file
> will already have the path prepended.
>
> And change by_last_mod (I ripped out the locals, as they were
> being thrown away again almost immediate after) to read:
>
>
> 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.
>    return stat($a)->mtime <=> stat($b)->mtime;
> }
>
>



More information about the linuxsa mailing list