Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
4  
If you need to do this more than once, logrotate is your friend –  sr_ Dec 18 '12 at 8:20
    
@sr_ Maybe you can extend it in an answer? –  Bernhard Dec 18 '12 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 '12 at 9:06
    
what do you mean by " delete the older files." , you want compress then delete those same files which are compressed ? –  Rahul Patil Dec 18 '12 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 '12 at 9:41
add comment

2 Answers

up vote 2 down vote accepted

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
share|improve this answer
    
Thank you that helped –  Ajo Mathew Dec 18 '12 at 10:02
add comment

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 {
        daily
        rotate 7
        missingok
        create 640 root root
        compress
}

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.

share|improve this answer
1  
Great suggestion, yet it doesn't meet the "to a single zip" requirement of the OP yet. –  gertvdijk Dec 18 '12 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 '12 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 '12 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 '12 at 9:52
add comment

Your Answer

 
discard

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.