2

I have a folder /home/usr/logs/ which contain log files which are older than 1 day. I wish to compress all the log files older than one day to separate compressed archives (e.g. zip or tar.gz) and move them to the folder /home/usr/logs/archive.

The log files are on naming formats, such as valid.app5s.log.1019, app5s.gf3sts.1019, valid.app5s.gf3log.1019, app5s.gf3log.1019, app5s.gf1sts.1019, valid.app5s.gf1log.1019, app5s.sts.1019.

I tried like this:

find . -mtime +1 -exec zip filename.zip '{}' + && mv filename.zip archive/ \; 

But those files are not zipped properly. Will anyone please help me in figuring out the mistake my code?

1 Answer 1

4

logrotate can do this, the pattern match can be for *log, then you add a section called "postrotate", consider postrotate a small bash script that runs after rotation.

example;

/home/usr/logs/*.log { daily missingok compress #delaycompress (this will prevent compressing of one day) notifempty create 640 root adm sharedscripts postrotate mv /home/usr/logs/*.gz /home/usr/logs/archive/; endscript } 

UPDATE1:

if you want to use zip instead of gzip, add;

compresscmd /usr/bin/zip compressoptions -r compressext .zip 
2
  • Thanks mike. But my problem is how to zip each log files with different xtension in name. Commented Oct 26, 2016 at 10:24
  • if you mean zip instead of gzip i've added an update, if you mean your src files have a different extension, just change the pattern match from .log to whatever your files have. for example /home/usr/logs/.log.gz, it also supports using multiple patterns. Commented Oct 26, 2016 at 10:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.