Keith Olmstead wrote:
Hello,

I am neeing some help on a script that I am writing. Here is what I have so far:

my $startdir = "/opt/log/hosts/";
use File::Find;
#use strict;
use warnings;
my @dirlist;
@logfile = ("cron","messages","maillog","ldap");
foreach $log (@logfile) {
sub eachFile {
if (-e $_ && $_ =~ /$log$/) { push @log, $File::Find::name;}
}
find (\&eachFile, "$startdir");
}
foreach $file(@log){
#system("gzip $file");
print "$file done!\n";
}


It currently looks for @logfile and then gzips them. What I am needing help with now is this. It gzips all the files, and I only want it to gzip the files from the previous day. Here is an example of what it finds..

/opt/log/hosts/server_ip/2003/08/04/maillog done!
/opt/log/hosts/server_ip/2003/08/03/maillog done!
/opt/log/hosts/server_ip/2003/08/02/maillog done!
/opt/log/hosts/server_ip/2003/08/01/maillog done!
/opt/log/hosts/server_ip/2003/07/31/maillog done!

Whenever the date changes to the next day, then a new dir is created and new log files are written to that dir.

An Idea that I had was to check the current date against the date # in the dir, ie 31, 01, 02.. from above and if the current date does not equal that number then gzip the files in it.

How does this sound? Is there a better way to do this?

Thanks,

Keith


It sounds like you are compilcating things more than reqd Why do you want to find the file when you you already know where is it??

use POSIX qw(strftime);

$basedir =  "/opt/log/hosts/server_ip";
$yesterday= time - 86400;
$yesterday = strftime '%Y/%m/%d' localtime($yesterday);
               # There are better ways  trust me But find them yourself

chdir "$basedir/$yesterday" || die "Bla bla";
@files = <*>;   # Ofcourse doing a readdir may be better

Hope that helps
Ram


BTW Do you need to do it in perl
See to it that you dont just overdo perl when It is lot simpler using utilities


You can just do this ( on any unix like system with gnu find )
find $DIR -mtime +1 -mtime -2 -type f -exec gzip {} \;
























-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to