I want to schedule logs rotate with these rules:

  1. log files for the last 2 day are kept untouched
  2. log files older than 2 days are archived and moved to folder named by date, they were created. (For ex. All logs for 01 Dec 2012 are moved to 20121201 and archived.)
  3. log files older than 14 days are removed.

I want to use logrotate for this, but I'm not sure it suits my needs or not.

I'd like something like this, but it doesn't work.

/mylog/* {   
DIR=$(date +%y%m%d); // actually it's current date
mkdir $DIR
rotate 2
olddir /mylog/$DIR
       find /mylog -type d -mtime +180 | xargs rm -f

Logrotate does not understand that $DIR is variable.

Any suggestions are welcome!


According to @koniu from superuser, it is not possible. As far as I checked, I didn't find any other ways for that.

For the age of the log, you have a maxage directive which according to the manpage:

   maxage count
          Remove  rotated  logs  older  than <count> days. The age is only
          checked if the logfile is to be rotated. The files are mailed to
          the configured address if maillast and mail are configured.

Looks like it doesn't work on old logs moved in olddir.

  • Thanks for the link. It covers the question about olddir. It's a bit tricky, but it should work! It was really helpful. Am I right, that logrotate can not delete old logs from olddir? Should my solution in postrotate work? – user148378 Dec 5 '12 at 13:04

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.