Problem with Bash output redirection

前端 未结 11 1942
梦毁少年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:09

    Just for this case it's possible to use

    cat < file.txt | (rm file.txt; tail -1 > file.txt)
    That will open "file.txt" just before connection "cat" with subshell in "(...)". "rm file.txt" will remove reference from disk before subshell will open it for write for "tail", but contents will be still available through opened descriptor which is passed to "cat" until it will close stdin. So you'd better be sure that this command will finish or contents of "file.txt" will be lost

提交回复
热议问题