How to overwrite the destination file with netcat as opposed to appending?

1

I have two ubuntu servers and will be using a script (although I have not written it yet) on the sending server to send file abcd.txt whenever it changes using:

    nc 10.10.10.4 3333 < abcd.txt

On the listening server I am using the -k option to keep it continuously listening:

    nc -k -l 3333 > /home/abcd.txt

My goal is to have the file on the listening server be overwritten every time the sending server sends a file. This is not happening. Instead the entire file gets appended resulting in duplicated lines. Is there a way to fix this? I am open to options either than using netcat, but I have very little experience with linux. Please treat me as a beginner.

Also note, I cannot use a sort or uniq command to change the abcd.txt once it gets to the listening server due to the files being extremely large and timing constraints being imposed. (We will likely have the file on the sending end rolling every 60 seconds or so.)

Preston Roy

Posted 2017-07-12T03:40:43.333

Reputation: 11

This might be an idea a Linux guy could help you with potentially but from the Windows side if there's a command (or service) I must use that appends when I need an overwrite, then I'd put some rules around the process and especially if it's automated and do some [standard] forceful things like that. Now if these events can be triggered many times within a second then you may have to scale out of rules and make it more complex. I assume the netcat limitation and your method of using it is what's the trouble so those are my quick thoughts as man page said nothing about overwrite params, etc. – Pimp Juice IT – 2017-07-12T04:43:52.437

See if it's possible to have the sender send abc.txt to the listener and then deletes abc.txt once it confirms the listener received, etc.. The listener puts the abc.txt data it just got from sender to /home/temp.txt instead (maybe put logic if temp.txt exist del temp.txt or append a <YYYYMMDD_hhmmsst> time stamp to the temp file name so each has a unique name). It then deletes /home/abc.txt if it exists and then it renames /home/temp.txt to /home/abc.txt or creates a new file with the temp file's content or however you would handle this with Linux. Quick ideas only. – Pimp Juice IT – 2017-07-12T04:56:08.373

No answers