Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

deletes timestamped logfiles (MMDDYY) based on actual time

by tlay (Acolyte)
on Feb 06, 2002 at 14:47 UTC ( [id://143628]=perlcraft: print w/replies, xml ) Need Help??

   1: #!/usr/bin/perl
   2: 
   3: #This is nothing special but it's the first real perl script I've done and I was interested
   4: #in hearing comments and opinions.  Sorry if it's a little remedial for everybody but I
   5: #think it beats the hell out of a hello world.
   6: 
   7: #This program deletes old unneeded backup logs with the goofy site dependent timestamp
   8: #backup.MMDDYY.log.Z
   9: 
  10: $now=time; #WHATS THE CURRENT DATE/TIME
  11: $deldate=$now-2419200;
  12: #GO BACK 20 WORK DAYS
  13: ($seconds,$minutes,$hour,$monthday,$month,$year,$weekday,$yearday,$dst_flag)=localtime($deldate);
  14: $month+=1; #FORMAT MONTH
  15: $month*=10000;
  16: $monthday*=100; #FORMAT DATE
  17: $year-=100; #FORMAT YEAR 
  18: $TS=$year+$month+$monthday; #FORMAT TIMESTAMP
  19: if ($TS < 100000) {
  20: 	$TS ="0$TS";
  21: }
  22: $fullstamp="backup.$TS.log.Z";
  23: unlink("/var/adm/$fullstamp"); #DELETE THE OLD LOGFILE

Replies are listed 'Best First'.
Re: deletes timestamped logfiles (MMDDYY) based on actual time
by blakem (Monsignor) on Feb 08, 2002 at 01:14 UTC
    If I was going to write this, I would use Time::Piece and the code would look like:
    #!/usr/bin/perl -wT use strict; use Time::Piece; my $daysago = 28; my $datadir = "/var/adm"; # MMDDYY of 28 days ago my $timestring = localtime(time-$daysago*24*60*60)->strftime("%m%d%y") +; my $file = "$datadir/backup.$timestring.log.Z"; -e $file or die "File '$file' doesn't exist"; unlink $file or die "Couldn't unlink '$file'";

    -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found