How do I clear the contents of a file before writing to it? For example:echo one > filename.tmp# filename.tmp now contains oneecho two > filename.tmp# filename.tmp should now contain two, not one and twoMy goal is:Start a listener$ nc -l 7007 > /var/tmp/test.logSend some data$ telnet localhost 7007hisecond_wordTest the file$ cat /var/tmp/test.logsecond_wordI don't want hi to show up in the log; I want second_word to have replaced it
How to keep overwriting the contents of a file instead of appending to it
linux;command line;networking
You need a separate program to clear and write the new file since nc doesn't offer that option.nc -l 7007 | while true; do    while read line; do      echo $line > /tmp/test    donedoneYou can save everything after the pipe in a separate script that accepts a file path.save-last-line.shwhile true; do    while read line; do        echo $line > $1    donedoneThen it's simply:nc -l 7007 | save-last-line.sh /var/tmp/test.logYou'll want to add checks to make sure $1 is writable and show usage when $1 isn't specified.