I'm using the command:
tail -f -n 0 file.txt
But it keeps repeating itself.
-
What I'm doing is the following:
-
Create the file in the first terminal:
touch file.txt
-
Start the tail in the second terminal:
tail -f -n 0 file.txt
-
Edit the file in the first terminal:
nano file.txt
-
Just add
text
, save and close. The second terminal outputstext
like it should. -
Edit the file again in the first terminal:
nano file.txt
-
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.
Best Answer
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:
That will write a number to
file.txt
every second for twenty seconds. If you open another terminal and runtail -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:Files are simply hardlinks to specific inodes, in this case,
file.txt
points to inode16647801
. Now, open the file ingedit
, add a line and check the inode again: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 withnano
does not change the inode. It does, however, delete the original contents overwriting them with the new contents. That's whytail
actually shows the output, if you try it and edit the file withgedit
(oremacs
or a number of other editors), the extra lines you add won't be shown in the output oftail
at all.