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.

  • 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy