Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

deletes timestamped logfiles (MMDDYY) based on actual time

by tlay (Acolyte)
on Feb 06, 2002 at 14:47 UTC ( #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

Comment on deletes timestamped logfiles (MMDDYY) based on actual time
Download Code
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
Node Status?
node history
Node Type: perlcraft [id://143628]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-08-23 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (174 votes), past polls