| |||||||||||
| |||||||||||
How to clean up week old log files using perl Posted by raf (raf), 15 February 2006 Hello Guys, I'm fairly new to perl and I have recently signed up, however I have been reading perl programming book for a couple of months as I'm a unix-AIX administrator and I have done some korn shell scripting. I'm trying to write a simple perl script to clean up log files in unix older than seven days, I'm unable to determine how to delete files older than seven days, below is my perl code, please correct me whereever I'm wrong in my code below....Thanks for your help and sharing your knowledge #!/usr/bin/perl use Time::localtime; #Variables my $Cnt=0; my Logdir = "/var/adm"; my LogFile = "CLEAN.LOG"; my $FileName = "/var/adm/messages"; my $Date; my $Herald my @Days = qw(day0 day1 day2 day3 day4 day5 day6); my ($month, $year, $wday; Functions sub WriteToLog { open (LOG, ">>$LogDir/$LogFile") || die "Error: Unable to open $LogFile\n"; write(LOG); close(LOG); } sub CheckWeekOld my $cnt = 0; my $size = 0; my @Days; $Date = `date`; $Herald = "Week Old logs removing" WriteToLog(); Please correct me and tell me what do I do in the above code so it deletes the log files older then seven days.. Cheers, Raf Posted by admin (Graham Ellis), 15 February 2006 Hi, welcome.If something looks a bt tricky, there must be an easier way ... and your code looks a bit tricky ![]() use the -M operator on a file to find out how many days ago t was last modified. Here's a sample program that reports on all files in the current directory ... Code:
Output starts ... 4gen is 0 days old am is 3 days old badday.jpg is 5 days old courseend.linux IS OVER 7 days old courseend.ms IS OVER 7 days old cw IS OVER 7 days old d06 is 1 days old dl IS OVER 7 days old Posted by raf (raf), 16 February 2006 Dear Graham; Thank you very much for your help, as I'm in Australia I couldn't reply yesterday, with your help, I have now worked it out on how to delete files older than seven days, however one question for you, what do I do in my code below so it deletes it from a specified directory instead of deleting it from the current directory as I dont want to delete anything accidently in production ![]() Below is my code, could you please correct my code, so it only deletes files starting from word 'u' in /var/spool/uv/test directory.. #!/usr/local/bin/perl use strict; foreach (glob ("*u*")) { print; my $logfile = "/var/spool/uv/test/cleanfiles.out"; my $result = -M; if ($result > 7) { open (STDOUT, ">>$logfile"); print "$_ is over 7 days old..removing\n"; unlink $_; } else { print " is ",int($result), " days old\n"; close (STDOUT); } } ################################## I rea Posted by admin (Graham Ellis), 16 February 2006 You'll need to put the path into the glob ... and I think that wll do it. But do run it with a print not an unlink while you test.Timezones - sorry for a brief answer - I'm off to work in a minute! Graham Posted by raf (raf), 17 February 2006 Dear Graham,Thanks Graham for your prompt response and help. My code below now works fine but I don't want to use -M (match operator) and glob funtion in my code below. Could you please correct my code below so I can use something like 'OPENDIR' function, instead of using 'glob' and also I want to use the "Time::Local" module... All I want from code is to simply delete files older than seven days,...Please help as its urgent... Could you please correct and send me a new code Thank so much, I will only ask for a occassional help, as I very keen to learn and be a competent perl programmer. Below is my code #!/usr/bin/perl foreach (glob("*")) { #instead of this I want to use OPENDIR funtion here# my $age = -M ; #I don't want to use -M operator to match..Please correct it# my $logfile = "/var/spool/cleanlogs.out"; if ($age > 7) { #This statement will be changed once u correct it# OPEN (STDOUT, ">>$logfile"); #I dont want to use 'STDOUT' to send the output to a file,so this has to be changed, please correct'#. print " IS OVER 7 days old\n"; } else { print " is ",int($age)," days old\n"; CLOSE (STDOUT); } } ######################################################### I look forward to hearing from you soon. Cheers, Raf Posted by admin (Graham Ellis), 17 February 2006 Raf, I really can't "correct" your code as there's nothing wrong with using the -M operator ![]() However, I can appreciate that opendir and readdir are better alternatives to glob - replace the glob line with: opendir (DH,"."); foreach (readdir(DH)) { next if (/^\./); Better because glob does a sort you probably don't want, but readdir returns everything including hidden files and even the current and parent directories, so you need to add an extra line to get rid of those. Thus ... a longer piece of code. Posted by raf (raf), 19 February 2006 Dear Graham, Thanks again, I appreciate your advice and will be trying to get this sorted, if I will have any problems I will post it to you probably sometime next week. Many Regards, Raf Posted by raf (raf), 28 February 2006 Hi Graham, I have written a new code with your help to clean logfiles older than 7 days, could you please modify my code so it prints files to a log file instead of printing it to standard out and then deletes it.. below is my code use strict; my $today = time; my $dir = '.'; opendir DIR, $dir or die "opendir '$dir':" while (my $file = readdir DIR) { next if -d "$dir/$file"; my $mtime = (stat "$dir/$file") [9]; if ($today - 86400 * 7 > $mtime) { print "$dir/$file is older than 7 days\n"; } } closedir DIR; #END Could you please modify the above code so it prints the file to a log file instead of printing it to standard out before it deletes it, I'm unable to print and save the out put of files older than 7 days to a file, I guess it needs open LOG thing in the code. Please help Many Regards, Raf Posted by admin (Graham Ellis), 28 February 2006 Before the while loop ...open (LOGFILE,">>/var/log/whatever.abc"); Modify the print ... print LOGFILE "$dir/$file is older than 7 days\n"; After the loop ... close LOGFILE; And you REALLY don't like -M, do you ![]() Posted by raf (raf), 1 March 2006 Dear Graham, With your continous help, I have now written a code which works like a charm, I have started to feel a little bit more comfortable with perl, however I will be bothering you in future ![]() ![]() Many Regards, Raf #!/usr/local/bin/perl use strict; my $TODAY = time; my $DIR = '.'; #my $LOGFILE = "/var/spool/uv/test/fileout"; my $wktime = 604800; (ie 86400*7) ############################################################################# sub write_to_file { open (LOGFILE, ">/var/spool/uv/test/fileout"); opendir DIR, $DIR or die "could not open directory: $!"; while (my $file = readdir DIR) { next if -d "$DIR/$file"; my $mtime = (stat "$DIR/$file")[9]; if ($TODAY - $wktime > $mtime) { print LOGFILE "$DIR/$file is older than 7 days...removing\n"; } } } close LOGFILE; close DIR; ############################################################################### sub remove_old_files { opendir DIR, $DIR or die "could not open directory: $!"; while (my $file = readdir DIR) { next if -d "$DIR/$file"; my $mtime = (stat "$DIR/$file")[9]; if ($TODAY - $wktime > $mtime) { unlink $file; } } } close LOGFILE; close DIR; ################################################################################## Main: { write_to_file(); remove_old_files(); } This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
Comment by Anon (published 2011-08-18) It worked fine.Thanks for Creater. Sailendra [#31004] You can Add a comment or ranking or edit your own comments Average page ranking - 4.0 |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |