0

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

DarckBlezzer
  • 183
  • 1
  • 1
  • 7

1 Answers1

0

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...

Rob
  • 1,175
  • 1
  • 7