Score:0

Why tail to same file create a empty file, or empty the file?

in flag

I try to maintain the last lines of a file, but when tail -n 10 test.txt > test.txt create or empty the file...

SO: Ubuntu server

Example:

#test.txt file 20 May 23 12:24 test.txt
 a
 a
 a
 a
 a
 a
 a
 a
 a
 a

use tail -n 10 test.txt > test.txt
new file is empty 0 May 23 12:36 test.txt

us flag
Rob
Does this answer your question? [Linux: How to use a file as input and output at the same time?](https://serverfault.com/questions/135507/linux-how-to-use-a-file-as-input-and-output-at-the-same-time)
Score:0
us flag
Rob

Output Redirection by the shell (as well as input redirection) happens before the requested command is started. The > file output redirect opens filefor writing and creates file if it does not exist; if it does exist it is truncated to zero size.

Once that has happened and your tail command is started it can only read an empty file...

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.