0

I have a file that is constantly been written and is getting to big. I need to copy the existing one to another name and at the same time create a new file with the same name so the process that is writing on the file will not be interrupted.

Thanks.

Ncu
  • 3
  • 1
  • 1
    It's called `rotating file handle`. You may have luck with [that](https://unix.stackexchange.com/questions/231486/how-to-implement-logrotate-in-shell-script). [this](https://superuser.com/questions/291368/log-rotation-of-stdout/291397#291397) could also be very helpful. – Kev Inski Aug 12 '19 at 10:04

1 Answers1

3

with the same name so the process that is writing on the file will not be interrupted.

cp file file.new && > file 

will copy the file over to file.new and then empty the file. (don't use rm as that could crash your process).

You should use logrotate to have the system itself make compressed log files at certain events (certain size reached or a daily compression) See How to prevent logs from getting too big? for more on that.

Rinzwind
  • 293,910
  • 41
  • 570
  • 710
  • It works very well. Thanks Rinzwind – Ncu Aug 12 '19 at 10:54
  • @Ncu If Rinzwind's answer answered your question and worked for you. Please accept it. Please click on the check mark beside the answer to toggle it from greyed out to filled in. – Raffa Aug 12 '19 at 11:19