Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using rotatelogs to create my daily apache logs in format host.<day>.<month>.<year>.access.log. Now I want to gzip and move log to different directory after it's been finished. How to do that?

Update: There was a little mistake. logrotate -> rotatelogs

share|improve this question
Thanks for correcting the question, I've removed my inaccurate answer. – jscott Dec 23 '11 at 15:23
You can write a simple script that gzip the old files and move/copy them to a different location. This script can be invoked in a daily cron job. – Khaled Dec 23 '11 at 15:37
I'm not familiar with shell scripts. Specifically, I don't know how to generate filename string for yesterday's date. – Poma Dec 23 '11 at 15:39
up vote 4 down vote accepted

I've came up with the following script

for file in $(ls /var/log/apache2/*.$(date +"%y.%m.%d" --date="1 day ago").access.log); do
    gzip $file
    mv $file.gz /var/log/apache2/archive

And following cron entry

15 0    0 0 0   root    /mypath/
share|improve this answer

I use find and crontab to accomplish this

# crontab -e

5 0 * * * /bin/find /path/to/logs/* -type f \( -mtime 1 ! -name "." ! -name ".gz" \) -print0 | xargs -0 gzip >/dev/null 2>&1;

-mtime n File’s data was last modified n*24 hours ago.

! -name "not ".*" hidden files

! -name "not ".gz" files which should already be compressed

-print0 & -0 to ensure spaces or special characters are piped correctly to xargs -> gzip (for filename safety)

find -exec could be used but has flaws where | xargs is more stable

share|improve this answer

Logrotate will happily do the compressing for you. Just add:


To the logrotate config for apache. There is also a neat option that delays the compressing by one day:


As for moving them, logrotate can't help you but a cron job like this can:

@daily mv /var/log/apache/*.gz /var/log/archive/
share|improve this answer
logrotate offers the olddir options which can move the logs -- see man logrotate. The OP edited the question to indicate he's using rotatelogs not logrotate. – jscott Dec 23 '11 at 17:46
Ah, that means I'm barking up the wrong tree. And I didn't even know about the olddir option. The shell script above seems like the best solution. – Ladadadada Dec 23 '11 at 18:01
logrotate is not rotatelogs! – MUY Belgium Apr 26 '13 at 9:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.