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?

  • 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
  • Truncating the active logfile is much easier than rewriting it, so I would not do it. But why do you care? If you have 7day logfiles you can search them with zgrep bla errors.log.* – eckes May 8 '19 at 5:10
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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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