Saturday, May 4, 2024
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 7058  / 2 Years ago, sun, january 23, 2022, 10:54:37

I'm using the command:



tail -f -n 0 file.txt


But it keeps repeating itself.







  1. What I'm doing is the following:


  2. Create the file in the first terminal: touch file.txt


  3. Start the tail in the second terminal: tail -f -n 0 file.txt


  4. Edit the file in the first terminal: nano file.txt


  5. Just add text, save and close. The second terminal outputs text like it should.


  6. Edit the file again in the first terminal: nano file.txt


  7. Add in the end (2nd line) more text, save and close. The second terminal outputs:





tail: file.txt: file truncated

text

more text




Edit the file again in the first terminal: nano file.txt



Add in the end (3rd line) even more text, save and close. The second terminal outputs even more text like it should.



Edit the file again in the first terminal: nano file.txt



Add in the end (4th line) and even more text, save and close. The second terminal outputs:



tail: file.txt: file truncated
text
more text
even more text
and even more text





I need the command to not repeat itself.



The output resulted in:



tail -f -n 0 file.txt
text
tail: file.txt: file truncated
text
more text
even more text
tail: file.txt: file truncated
text
more text
even more text
and even more text


And I need it to output:



tail -f -n 0 file.txt
text
more text
even more text
and even more text


Or, if it's not possible to remove the "file truncated" message, something like:



tail -f -n 0 file.txt
text
tail: file.txt: file truncated
more text
even more text
tail: file.txt: file truncated
and even more text


I also don't know why it gives the file truncated message. I start inserting the new text at the end of the file.


More From » command-line

 Answers
4

First, you get the error because you are using a text editor for this. This means that every time you open the file, edit and save it, the original is overwritten with the new contents. Whether you added a single line to the end or 100 lines all over the place is irrelevant, the point is that the file is being opened, edited and saved and that overwrites the contents.


For example, compare your workflow to running this:


for((i=0;i<20;i++)); do echo $i >> file.txt; sleep 1; done

That will write a number to file.txt every second for twenty seconds. If you open another terminal and run tail -fn 0 file.txt, you will see the output you expect.


So, to get the desired behavior add the text to the file using >> from a terminal instead of manually editing it.




More details


nano seems to be the odd one out here. Most editors when opening a file then saving it will actually delete the original file and save a new one with the same name. You can test this by checking the inode number of the file:


$ ls -il file.txt
16647801 -rw-r--r-- 1 terdon terdon 9 Apr 6 18:19 file.txt

Files are simply hardlinks to specific inodes, in this case, file.txt points to inode 16647801. Now, open the file in gedit, add a line and check the inode again:


$ gedit file.txt
$ ls -il file.txt
16647854 -rw-r--r-- 1 terdon terdon 13 Apr 6 18:23 file.txt

As you see, the inode number has changed, in other words, the original file was removed and a new one was created. nano does not do that, trying the same thing with nano does not change the inode. It does, however, delete the original contents overwriting them with the new contents. That's why tail actually shows the output, if you try it and edit the file with gedit (or emacs or a number of other editors), the extra lines you add won't be shown in the output of tail at all.


[#26174] Tuesday, January 25, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quivedge

Total Points: 223
Total Questions: 116
Total Answers: 124

Location: Slovenia
Member since Wed, Apr 6, 2022
2 Years ago
;