sponge (1) is a simple way to do this, but not available on all systems. See <a href="http://joey.kitenet.net/code/moreutils/" rel="nofollow">http://joey.kitenet.net/code/moreutils/</a>
I use this technique a lot:<p><pre><code> mv thing thing.old && mv thing.new thing
</code></pre>
It works whether the thing is a file or a directory; I have an automatic backup called thing.old; I suppose it's reasonably atomic; and I can do it in ssh with no worries about losing the network connection in between the two moves.
Diagnosis for 2 is wrong. The shell won't be scheduled to truncate the file after the shell is scheduled to fork/exec cat. The shell is not multithreaded.
I don't understand this article. Saying "Replacing the contents of a file on the UNIX command line using the standard cp/mv commands is surprisingly tricky" is like saying "driving a car using only your ass is surprisingly tricky". That's true, but when there are standard, simple, non-tricky ways to do it then you're really just limiting yourself.<p>But hey, I'll play along. Won't this work:
cp file /dev/stdout > file