Search code examples
bashoverwrite

Bash- why can't a file be overwritten without the use of a temp file?


The standard procedure for overwriting a file is usually the following:

awk '{print $2*3}' file > tmp file
tmpFile > file

However, sometimes this poses to be a bit of a hassle because then one must remove the temp file after it is no longer being used.

So, why is it not possible to do this in the following way (without the need of a temp file) :

awk '{print $2*3}' file > file

The reason I ask is because I know that it is possible to append to a file as so:

awk '{print $2*3}' file >> file

So if appending a file, using >> as shown above, works fine, why can't one overwrite a file in the same way. Why are the two commands so different?

Moreover, does there exist a way of bypassing the need for a temp file (perhaps in a fashion similar to the 2nd excerpt), or is the first excerpt the only way?

NOTE: the awk command is irrelevant, it can be replaced by any other command


Solution

  • Using a temp file is a good idea because you can never be sure if the entire file will be read into memory. If you try to write it before it was read, then you might get a different result than you might have expected.

    When using append, the command always goes through the entire file before adding new content, so there never remains a part of the file to be read.