Is there something that allows me to move just the content of a file, and save it in another file? I know there is copytruncate, but I can't use logrotate in my setup. The file the content is moved from, has to stay as it is. Just the lines written would be transferred to another file. I need something that i can put in a bash script.
2 Answers
I am not sure if this is the best way but you can try
cat file > new_file
truncate -s0 file
If the files are huge then you should keep looking for a better way.
-
Why wouldnt it work with huge files ?Meerkat– Meerkat2016-11-04 10:37:52 +00:00Commented Nov 4, 2016 at 10:37
-
The copying of data will take some time and if it is more than a few milliseconds, new data would be written to the file before truncate call is triggered. In case of huge files it can take seconds.kn330– kn3302016-11-04 10:46:34 +00:00Commented Nov 4, 2016 at 10:46
-
If new Data would be coming in during the clearing, if thats whats you mean, that would be fine. Its a logfile in whitch is constantly in useMeerkat– Meerkat2016-11-04 12:04:22 +00:00Commented Nov 4, 2016 at 12:04
-
You may use it then. I just wanted you to know the shortcomings of the script.kn330– kn3302016-11-04 12:08:32 +00:00Commented Nov 4, 2016 at 12:08
-
So I tried you suggestion, and it seems to work, but there is one thing. All the Data that has been copied away has now been replaced with a @ in my test enviroment. Any idea why that is ?Meerkat– Meerkat2016-11-07 09:10:35 +00:00Commented Nov 7, 2016 at 9:10