I'm using tail -f a.txt to watch updates on a file called a.txt.

If I update the file using something like ls -a >> a.txt in a second virtual console, the changes will display in real-time in the first one.

If I update the file using Vim in a second virtual console, the changes will not display in the first one.

I don't necessarily expect it to trigger an update in that window - but why exactly doesn't this update the terminal running the tail -f command?

  • 5
    tail -f checks for appends. tail -F checks for file renames. – thrig Jul 21 '17 at 16:54
up vote 31 down vote accepted

If you edit a file with vim, typically it reads the file into memory, then writes a new file. So tail is now operating on an out of date copy of the file (which remains in the file system until tail (and any other program) stops using it.

You can make tail follow the filename (rather than the file) by using:

tail -F yourfile  

Note the upper case F.

  • 1
    tail -F works! The console is now displaying a message like "a.txt has been replaced: following end of new file" and updates accordingly. Thanks! – alex Jul 21 '17 at 17:37

My understanding is that typically when editing with vim, you are editing a copy of the file, which is moved into place when you write your changes to disk. Since this happens by unlinking the original and moveing the new into place, tail does not see new changes to the original file.

  • Makes sense. Thanks! – alex Jul 21 '17 at 17:38

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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