Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 have a service that automatically creates a log file with a timestamp in the filename on startup. Thus, I don't need logrotate to rename/copy/create files, but what I do want is that logrotate shall keep just the three newest of those files (and optionally compress them). Can I do that somehow?

share|improve this question
up vote 3 down vote accepted

I'm not sure you can do this by using logrotate. Can you run the following as a daily cron job:

rm $(ls -t | sed -e '1,3d')
share|improve this answer

I do not think that it can be done exactly as you want. In case you want to still use logrotate first you need to specify how often logrotate will rotate your log (daily/weekly/monthly/yearly). You can set it like this:

# rotate log files daily

# Log files are rotated count times before being removed or mailed to the address
# specified in a mail directive. If count is 0, old versions are removed rather than 
# rotated.
rotate 3

# Old versions of log files are compressed with gzip(1) by default.

But as @quanta wrote it can't be probably done with logrotate. You will either use some similar settings as I wrote above, or you will need to use probably other tool.

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.