I have a folder /home/testuser/log which contain log files of one day old *.log. I wish to compress all the log files older than one day to a single zip(gzip or tar.gz) and delete the older files.

I tried to pipeline find and tar commands but didn't work

  • 4
    If you need to do this more than once, logrotate is your friend
    – sr_
    Dec 18, 2012 at 8:20
  • @sr_ Maybe you can extend it in an answer?
    – Bernhard
    Dec 18, 2012 at 8:54
  • I am planning to put a job in cron tab to do this. I compress-->Move to another location. This is what I am planning to do.. but not able to compress all files into one zip/gz
    – Ajo Mathew
    Dec 18, 2012 at 9:06
  • what do you mean by " delete the older files." , you want compress then delete those same files which are compressed ? Dec 18, 2012 at 9:37
  • sorry for the confusion giving an example example: log1 created today log2 log3 are old. Compress log2 log3-->output.gz then delete log2 and log3
    – Ajo Mathew
    Dec 18, 2012 at 9:41

2 Answers 2


Create tar.gz files older than one day logs

find /home/testuser/log/ -mtime +1 | xargs  tar -czvPf  /opt/older_log_$(date +%F).tar.gz

Delete older files [ Note:- if below find output is Correct then remove echo , after that it will delete those files]

find /home/testuser/ -mtime +1 | xargs  -n1 echo rm
  • 3
    For the second part, you can simply type: find /home/testuser/ -mtime +1 -delete Jul 27, 2014 at 21:10

Extending sr_'s comment, I'm using logrotate to housekeep a nightly SQL dump. I have this configuration in /etc/logrotate.d/mydbdump:

/var/backups/dump.sql {
        rotate 7
        create 640 root root

It basically takes the /var/backups/dump.sql file (you would specify the name of your logfile instead), compresses it and renames it to dump.sql.1.gz. Before that, it rotates the old dump.sql.1.gz to dump.sql.2.gz and so on, and deletes the oldest one.

logrotate itself is usually called by cron, so have a look at your /etc/cron* directories to control when it's run.

  • 1
    Great suggestion, yet it doesn't meet the "to a single zip" requirement of the OP yet.
    – gertvdijk
    Dec 18, 2012 at 9:33
  • I have few log files log1.log log2.log my idea was to take created date of files if(created date=current date-1) compress all, move to new location, Delete older files
    – Ajo Mathew
    Dec 18, 2012 at 9:40
  • @b00tbu9 logrotate does this for you. If you look at your /var/log dir and notice the *.#.gz files you'll see its results.
    – domsom
    Dec 18, 2012 at 9:46
  • @gertvdijk if the single zip file is a requirement, I'd suggest using the prerotate/postrotate options to manually merge the input/output files.
    – domsom
    Dec 18, 2012 at 9:52

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.