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

My Apache logrotate config looks like this:

/var/log/http/*log {
    dateformat .%Y.%m
    [... rest stripped for brevity ...]

This works great, except that the date on the rotated filename is one period later than the period actually covered by the logs, for example error_log.2012.09 covers 2012-08-01 to 2012-08-31.

I realize that there are other options for Apache (eg. cronolog), but I have a bunch of other logs that I also need to rotate, and logrotate is really exactly what I need apart from this one issue.

Is there a way to get logrotate to use a date offset -- or, even better, figure out the previous time period -- when generating the rotated filename?

share|improve this question

migrated from Oct 17 '12 at 22:08

This question came from our site for professional and enthusiast programmers.

up vote 8 down vote accepted

Added in 3.8.0 (although looking at the svn history, it looks like it was actually added in 3.7.9):

- added "dateyesterday" option (see man page)
share|improve this answer
Despite the name, dateyesterday works even if you are rolling monthly (with something like dateformat .%Y-%m), and will use last month's number – Steve Armstrong Apr 24 '14 at 23:21

if logrotate in your distrib doesn't have 'dateyesterday' option yet, you can use script like the following:

LOGMTIME="$(env LC_ALL='C' stat $LOGFILE |awk '/^Modify/{print $2}')"
LOGSTART=$(( $(date +%Y%m%d -d "${LOGMTIME}") - 1 ))
mv $LOGFILE ${LOGFILE%-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]}-$LOGSTART


  1. script will work right only in case of 'daily' option
  2. if operating on multiple files, wrap the code block in "for LOGFILE in $@" loop.
  3. sequence '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' is the logrotate's default glob pattern, see description of dateext option of logrotate man.
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.