How do I execute any command editing its file (argument) "in place" using bash?

jm. picture jm. · Sep 28, 2008 · Viewed 61.2k times · Source

I have a file temp.txt, that I want to sort with the sort command in bash.

I want the sorted results to replace the original file.

This doesn't work for example (I get an empty file):

sortx temp.txt > temp.txt

Can this be done in one line without resorting to copying to temporary files?


EDIT: The -o option is very cool for sort. I used sort in my question as an example. I run into the same problem with other commands:

uniq temp.txt > temp.txt.

Is there a better general solution?

Answer

daniels picture daniels · Sep 28, 2008
sort temp.txt -o temp.txt