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 ofsort
instead of using redirection operator, this will edit the file in place.From
man sort
:Here is a test: