2

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 2

2

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.

5
  • Why wouldnt it work with huge files ? Commented 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. Commented 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 use Commented Nov 4, 2016 at 12:04
  • You may use it then. I just wanted you to know the shortcomings of the script. Commented 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 ? Commented Nov 7, 2016 at 9:10
0

For any new curiosity mongers:

:> filename

Seen here.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.