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

Is it possible to get logrotate to keep the last week of logs in the current file?

for example :

errors.log contains 30 days of logs

after log rotate executes , I would have :

errors.log with the last 7 days of log


errors.log.1.gz compressed with the 23 other days.

Is that possible?

share|improve this question
Logrotate doesn't provide for this directly. You could write a small script to do it, but it would have to be able to parse the log format and filter out entries older than your threshold. (if there's no timestamp on the log lines, this would be impossible) Can I ask why you'd want to do it this way? – SmallClanger Sep 1 '11 at 8:41
rotate 4

followed by a postrotate script doing something like (untested):

zcat /var/log/errors.log.{4,3,2,1}.gz > $TMP
mv $TMP /var/log/errors.log.1
gzip /var/log/errors.log.1


share|improve this answer

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.