Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trim the access log file 1

Status
Not open for further replies.

SM777

Technical User
Mar 7, 2001
208
GB
how do I trim the log file so that I only have the last 4 weeks information?

Cheers.
 
Quick and dirty.
grep "Jun" /var/log/access.log

replaceoldlog-last two months listings:
#!/bin/sh
holdfile="/tmp/foo"
#main
cat /var/log/access.log | awk ' {
if ($0 ~ /May/ || $0 ~ /Jun/) {
print $0
}
}' > $holdfile && mv $holdfile /var/log/access.log


Bye.
 
Ok, I like the look of the quick and easy grep version. I had to modify it slightly because it was picking up entries in earlier months which had June, Juniper, Juno somewhere on the line.

I did this:

grep "Jun/2001" access.log >temp_log
mv temp_log access.log

 
#!/bin/sh
str=0
while test "$str" != ""
do
echo -n "String to look for: "
read str
grep $str "/var/log/access_log"

done

Interactive.

You could run the other as a cron job if you just want
to prune logs, replacing the old month with the new one of course.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top