Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using linux and doing some bash scripting. I have a log file which is making logs of all the events/changes our in particular directory. I need to copy these logs to some other file so that I can work on it. But I cannot just use:

cp /mnt/abc.log /tmp/xyz.log

Because it will not delete the logs which are already copied from abc.log - I need to copy the logs and then delete the copied logs (not the file) so that next time I will get new logs.

Because If I will copy the logs the delete that file and again recreate the file then I will loss the events occurred in the mean time. I need some bash script for this.

share|improve this question

migrated from stackoverflow.com Feb 7 '13 at 16:36

This question came from our site for professional and enthusiast programmers.

    
you want to clear abc.log after copying? –  ogzd Feb 7 '13 at 14:23
    
Yes I want clear only copied logs only - I cannot clear entire log file because its always written by filesystem. –  user1993197 Feb 7 '13 at 14:47
    
did you try > /mnt/abc.log ? –  ogzd Feb 7 '13 at 14:50
    
You can try to roll your own with cp source dest, then >source to empty it and possible kill -1 to the program writing to it. But as Mark already answered, these problems have already been solved before and there is rarely a reason to create your own version. (Well, bar place with a NotInvertedHereSyndrome) –  Hennes Feb 7 '13 at 16:52
add comment

1 Answer

Just use logrotate, or any of the log-rotation software that's available on the Internet. There's no sense rolling your own here.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.