This question already has an answer here:

I've done this before, expect to mutate a file in place, something like $ tr '\t' ',' <file.txt >file.txt and well, clearly the > redirection truncates the file before < can read it;

How do I mutate a file inplace without truncating/deleting it?

marked as duplicate by Stephen Kitt, Timothy Martin, ThorSummoner, ilkkachu bash Jun 6 '18 at 20:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


You cannot. The > redirect operator will create a new empty file as part of the pipeline. Tools (like sed --in-place) which appear to do so are creating a new file, writing to it, and moving the new file in place of the old one:

$ ls -i file; sed --in-place 's/foo/bar/' file; ls -i file
266110 file
262180 file
  • yeah, in effect you will always need some kind of journal if you want to mutate the file contents in place, and it just so happens that if the working set fits in memory, and you have no power failures interruptions, you can get away with using the source file in memory as the effective journal :shrug: atleast thats how I put words to it – ThorSummoner Jun 6 '18 at 20:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.