Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Does nano insert (append) new text to the existing file? Or does it empty (truncate) the file and then write the new (longer) version? Which is a new write which would trigger tail. –  Hennes Apr 6 at 16:05

2 Answers 2

up vote 10 down vote accepted

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.

share|improve this answer
1  
Nano was the problem. Thank you. Using echo text >> file.txt instead of nano solves the problem. –  Edu Apr 6 at 21:51
    
nano does however, delete the original and overwrite it with a new file, just pointing to the same inode. Are you sure about that? That sounds awfully unportable. Doesn't it just truncate the file and append new content? –  rr- Apr 7 at 7:13
    
@rr- yes, that's why you get the file truncated error. The original contents were removed (the file was deleted) and then the new contents are appended. –  terdon Apr 7 at 14:42
    
Deleting a file (remove(...); and recreating it from scratch) and truncating its contents (open(..., O_TRUNC | O_WRONLY); or fopen(..., "wb"); or ftruncate(...)) is not the same, hence my question. Both can lead to different kind of race conditions. –  rr- Apr 7 at 15:18
1  
@rr- ah, OK, I see what you mean. Well, I ran strace on nano and it looks like it first opens O_RDONLY, then closes and opens again O_WRONLY|O_CREAT|O_APPEND at this point it seems to do a write() call but I can't tell what it's writing. Finally, it closes again and opens with O_WRONLY|O_CREAT|O_TRUNC and then writes the text I told it to write. So, you seem to be quite right and the work is happening with a truncate call. I edited accordingly, thanks. –  terdon Apr 8 at 15:00

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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.