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.


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.

|improve this answer|||||
  • Why wouldnt it work with huge files ? – Meerkat Nov 4 '16 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 Nov 4 '16 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 use – Meerkat Nov 4 '16 at 12:04
  • You may use it then. I just wanted you to know the shortcomings of the script. – kn330 Nov 4 '16 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 Nov 7 '16 at 9:10

For any new curiosity mongers:

:> filename

Seen here.

|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.