Problem with Bash output redirection

前端 未结 11 1903
梦毁少年i
梦毁少年i 2020-12-06 06:19

I was trying to remove all the lines of a file except the last line but the following command did not work, although file.txt is not empty.

$cat file.txt |ta         


        
11条回答
  •  情深已故
    2020-12-06 07:15

    As Lewis Baumstark says, it doesn't like it that you're writing to the same filename.

    This is because the shell opens up "file.txt" and truncates it to do the redirection before "cat file.txt" is run. So, you have to

    tail -1 file.txt > file2.txt; mv file2.txt file.txt
    

提交回复
热议问题