Search This Blog

Thursday, November 22, 2018

Sort a file in-place

You can use the -o option of sort to indicate the same input and output file:

sort file -o file

A common mistake is to try to redirect the output to the same input file (e.g. sort file > file). This does not work as the shell is making the redirections (not the sort(1) program) and the input file (as being the output also) will be erased just before giving the sort(1) program the opportunity of reading it.