Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Server
User Name
Linux - Server This forum is for the discussion of Linux Software used in a server related context.


  Search this Thread
Old 12-10-2008, 02:40 PM   #1
LQ Newbie
Registered: Feb 2005
Distribution: RH9, RHEnt.
Posts: 23

Rep: Reputation: 0
Question rotating apache logs.....looking for a good script to delete old log file

Hi All,
I am running apache web server and I was wondering if anyone had a good script for deleting old log files.

I am using cronolog with the following entries in my httpd.conf:
CustomLog "|/usr/local/sbin/cronolog /usr/local/apache2/logs/%y/%m/%d/access_log.%y%m%d" combined
ErrorLog "|/usr/local/sbin/cronolog /usr/local/apache2/logs/%y/%m/%d/error_log.%y%m%d"
Notice I am sorting the logs into directories according to /year/month/day and I would like to delete anything older than 7 days.

I have no idea how to write this script.....would it be a cron job, or a function of cronolog, or some other logging tool?

Old 12-10-2008, 03:10 PM   #2
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
AFAIK, you do it in a cron, regardless of your method.

logrotate is designed to rotate, compress, remove old, & mail logs. You would run logrotate in a cron. see "man logrotate" (I would give examples if I knew it better, but don't... so see the man page )

if all you need to do is remove old logs, you can do that with a simple find command:

find /log/location -type f -mtime +7 -exec rm -v '{}' \;
of course, you lose all the other fancy features of logrotate, but if all you're after is deleting files... that will do it.

"-type f" means only return files (as opposed to files and directories and other stuff)
"-mtime +7" means anything older than 7 days (actually, older than 7*24 hours ago)
"-exec rm -v '{}'" means verbosely remove the file(s) - I like leaving verbosity on so that my cron mails me with what it did.

Last edited by BrianK; 12-10-2008 at 03:13 PM.
Old 12-10-2008, 03:19 PM   #3
LQ Newbie
Registered: Feb 2005
Distribution: RH9, RHEnt.
Posts: 23

Original Poster
Rep: Reputation: 0 that was fast!!!!

Another question: How can I make this recursive? Because I am storing files in a "/year/month/day/log.file" directory structure that rotates every 24hrs. I would need to delete all files older than 7 days and delete just the empty directories after the files were deleted 1st.

Thank you very much for your help!
Old 04-01-2009, 10:57 AM   #4
LQ Newbie
Registered: Mar 2009
Posts: 5

Rep: Reputation: 0

i am using following command to delete the logs from my /var directory

find -P /var -name -"*.part" -mtime 2 -exec rm -rf {} \;

this is working fine but what i am facing problem is that, it also deletes the .part extension files from the sub directories of /var. can anybody please help me out on this? i just want to to make sure that it delete only from that 1 directory which i have mentioned in the command...

Old 04-24-2020, 01:51 PM   #5
LQ Newbie
Registered: Apr 2020
Posts: 1

Rep: Reputation: Disabled

i have the same issue as of yours,
please let me know how to delete the old files and where do i do the cron job in detail as i am new to this.

Last edited by nharish143; 04-24-2020 at 01:53 PM.
Old 04-25-2020, 12:44 PM   #6
Senior Member
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,238

Rep: Reputation: 103Reputation: 103
Is there any good reason why you structure your logs like that? Each directory corresponding to year, month, day? You only want to keep your logs for a week, so that seems in my mind unnecessarily complicated. And then you already have that information in the file itself. Why don't you just use one folder where you can store the one-week logs?
Then you can easily appply logrotate on that directory with whatever criteria you like.
Old 04-25-2020, 12:59 PM   #7
Registered: Aug 2002
Posts: 23,454

Rep: Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083Reputation: 5083
Welcome to LinuxQuestions.

This thread is over 11 years old and ringding has not been active for 8 years. Your question should be in its own thread but since there is no information there is little benefit to moving it. Please start a new thread and provide details on what linux distribution / version you are running. Most distributions are configured with logrotate which will save old logs then automatically delete them.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Rotating Apache logs and Webalizer xMeta4x Linux - Software 5 03-20-2009 12:42 AM
Logrotate: Rotate Apache logs, but never delete kenneho Linux - Newbie 2 10-23-2008 06:58 AM
Can I delete all logs in /var/log? HURRY! Tenover Linux - General 4 01-03-2006 01:09 PM
problems rotating logs uros678 Linux - Software 0 12-15-2003 02:55 AM
Squid and rotating logs using crond 80s Linux - Software 0 02-21-2003 03:53 AM > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:19 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration