PL> Either use one of the bazillion Date parsing/modifying modules
PL> available on CPAN (for example, Date::Calc), or just calculate the
PL> timestamp yourself, subtract 24*60*60, and get the new date:
PL> #!/usr/bin/perl
PL> use strict;
PL> use warnings;
PL> use Time::Local;
PL> use POSIX qw/strftime/;
PL> my $today = '040107';
PL> my ($m, $d, $y) = $today =~ /(\d\d)(\d\d)(\d\d)/;
PL> my $timestamp = timelocal(0,0,12,$d,$m-1,$y);
PL> my $yesterday = $timestamp - 24*60*60;
PL> my $newdate = strftime("%m%d%y", localtime($yesterday));
PL> print "$newdate\n";
PL> __END__
To find the next-to-last log, you don't necessarily need to do date
arithmetic. On the downside, this approach will be wrong when a log
is missing:
ls -t $dir | tail -2 | head -1
Another approach is to find the time period you want ('yesterday' is
usually defined as the period from midnight 1 day ago to midnight
tonight, excluding 00:00 tonight). If any file creation times fall
within that time period (assuming the logs are produced during
"yesterday") you have a hit.
It all comes down to what you are looking for: the file *stamped*
yesterday in the file name (Paul's solution), or the file *produced*
yesterday (my second suggestion), or the next-to-last file (my first
suggestion).
My personal favorite solution to this and most other timestamp issues
is to use Unix timestamps, which don't have the annoying problem of
date arithmetic (and they are timezone-neutral, which is something you
should consider). You then just need to pipe timestamps through a
simple filter like this (I call it t.pl and alias it to `t'):
#!/usr/bin/perl -p
use POSIX;
s/(\D?)(\d{10})(\D?)/$1 . strftime('%Y-%m-%d_%H:%M:%S', localtime($2)) . $3/e;
This will rewrite Unix timestamps as normal dates in your local
timezone. So if you stamp your files like this, just do 'ls|t'
Ted