Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 logs in the following format: YYYYMMDD

I want to compress old logs (older then current day) and maybe move them to a different directory afterwards.

Can I do this in logrotate, or do I have to use a custom script in cron?

share|improve this question
up vote 3 down vote accepted

Here's a quickie script which will do what you need:

TODAY=$(date +'%Y%m%d')

[ -d $OLDLOGS ] || mkdir -p $OLDLOGS


for LOG in $(ls | egrep '^[[:digit:]]{8}$'); do
    [ $LOG -lt $TODAY ] && gzip $LOG && mv $LOG.gz 

Make the script executable:

$ chmod +x /where/you/put/this/script

The crontab entry will look like:

30 0 * * * /where/you/put/this/script

Just adjust LOGDIR and OLDLOGDIR. At 12:30am it will move all logs in the format of YYYYMMDD for the previous (and earlier, if any) days.

share|improve this answer
Yes, but it doesn't really answer my question. My question was whether I can use logrotate for this. – Let_Me_Be Jun 24 '11 at 11:04
@Let_Me_Be - I thought it was implicit in my answer. No, logrotate won't do exactly what you want. That is, you can't only match YYYYMMDD files. Otherwise you can come close by using nodateext, olddir, compress, and daily options. – Mark Mann Jun 24 '11 at 11:26

logrotate can do it with olddir if your log file name is the same every time it runs and you can add dates. If your log file name changes i.e. YYYYMMDD then logrotate won't do it for you.

# sample logrotate conf file
dateformat %Y%m%d.
extension log
olddir ./logarchive

/logs/sys.log {
    rotate 7

Copies and gzips /logs/sys.log to /logs/logarchive/sys.20120101.log.gz, keeps one week worth of logs.

share|improve this answer

logrotate itself does not do this. I'd recommend writing a supplementary script and invoking it from logrotate using the postrotate option in the configuration.

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.