'tail' command not getting only the new lines

Edu*_*Edu 4 command-line bash tail

I'm using the command:

tail -f -n 0 file.txt
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

And I need it to output:

tail -f -n 0 file.txt
text
more text
even more text
and even more text
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

ter*_*don 14

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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.


小智 7

As stated in one of the other answers already, this is happening because your file is being overwritten each time you save the file. The tail command was designed to monitor log files in which new lines are appended to the file.

In this particular situation I would use the following as a workaround:

watch -n 1 cat file.txt
Run Code Online (Sandbox Code Playgroud)

The watch command executes a command periodically. The update interval, in seconds, is specified with -n and can be adjusted to your liking.