How does the "tail" command's "-f" parameter work?

  • $ tail -f testfile
    

    the command is supposed to show the latest entries in the specified file, in real-time right? But that's not happening. Please correct me, if what I intend it to do is wrong...

    I created a new file "aaa" and added a line of text and closed it. then issued this command (first line):

    $ tail -f aaa
    xxx
    xxa
    axx
    

    the last three lines are the contents of the file aaa. Now that the command is still running (since I used -f), I opened the file aaa via the GUI and started adding a few more lines manually. But the terminal doesn't show the new lines added in the file.

    What's wrong here? The tail -f command only shows new entries if they are written by system only? (like log files etc)

  • From the tail(1) man page:

       With  --follow  (-f),  tail  defaults to following the file descriptor,
       which means that even if a tail’ed file is renamed, tail will  continue
       to  track  its  end.   This  default behavior is not desirable when you
       really want to track the actual name of the file, not the file descrip-
       tor (e.g., log rotation).  Use --follow=name in that case.  That causes
       tail to track the named file  in  a  way  that  accommodates  renaming,
       removal and creation.
    

    Your text editor is renaming or deleting the original file and saving the new file under the same filename. Use -F instead.

    worked! So, I can use `$ tail -F filename` command all the time instead of `$ tail -f filename` right?

    *If* that's your intended behavior. There *may* be cases where you want to follow by descriptor instead of filename, but to be fair I haven't come across many of those.

    `lsof` can show this happening - for example `lsof -Fpcftni` would show that the inode being followed by `tail` is no longer the same one that the editor has open.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM