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 directory with many log files, all of which I would like to rotate daily. For organizational purposes I would like to be able to move the rotated logs into a different directory (or subdirectory) named by date, keeping the last week of logs.

I can use logrotate to achieve most of this by roatating the files in-place or even move them to a single different directory using the olddir directive but I'm having trouble finding a solution for making individual sub-directories per rotation. How can I achieve this?:

Logs to rotate: /var/log/example/*

Desired target directories (keeping a week):
    [ ... ]
share|improve this question
up vote 6 down vote accepted

You should be able to call an external script in the postrotate directive:


and have that script do the moving, e.g.:


newdir=/var/log/example/`date +%Y%m%d`

mkdir $newdir
mv /var/log/example.1.gz $newdir

find /var/log/example -mindepth 1 -maxdepth 1 -mtime +7 \
  -type d -print0 | xargs -0 rm -rf

However, it might be easier to just use the dateext directive. With that the rotated files will be appended with a timestamp (although not moved to a different directory).

See logrotate(8) for details about both directives.

share|improve this answer
It seems that postrotate will do the trick. The only thing I need to add is purging the old directories that are over a certain count or older than a certain date. – Dave Forgac Oct 15 '12 at 13:56
See updated answer. The find | xargs command should delete all immediate subdirectories of /var/log/example if they were last modified more than 7 days ago. – Ansgar Wiechers Oct 15 '12 at 17:28
Thank you again! – Dave Forgac Oct 15 '12 at 19:16

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.