Ubuntu – sorting a file using the sort command and overwrite it

sort

i was trying to sort a text file and replace the sorted file with the original one, so i set the file to be also the output of the sort command too, but when i do that it clears the file contents and the file becomes empty, why? i can use another file to solve this problem but it doesn't feel nice :D, ( and i'm using Lubuntu. )

faav@faav-XPS-L501X:~$ cat test
A
C
B
faav@faav-XPS-L501X:~$ ls -l test
-rw-rw-r-- 1 faav faav 6 Apr 14 00:28 test
faav@faav-XPS-L501X:~$ sort test > test
faav@faav-XPS-L501X:~$ cat test
faav@faav-XPS-L501X:~$ ls -l test
-rw-rw-r-- 1 faav faav 0 Apr 14 00:28 test

Best Answer

Use the -o option of sort instead of using redirection operator, this will edit the file in place.

From man sort:

-o, --output=FILE
              write result to FILE instead of standard output

Here is a test:

$ cat test.txt 
A
C
B

$ sort test.txt 
A
B
C

$ sort test.txt -o test.txt

$ cat test.txt 
A
B
C
Related Question