Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
$ 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)

share|improve this question

3 Answers 3

up vote 29 down vote accepted

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.

share|improve this answer
    
worked! So, I can use $ tail -F filename command all the time instead of $ tail -f filename right? –  its_me Aug 15 '11 at 2:18
11  
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. –  Ignacio Vazquez-Abrams Aug 15 '11 at 2:18
    
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. –  Aaron D. Marasco Aug 15 '11 at 8:58

Your editor has its own buffer for the file. When you modify the text in the editor, nothing is written to the file itself.

When you save your changes, chances are that the editor simply deletes the old file and create a new one. tail -f will still be connected to the deleted file, so it won't show anything new.

share|improve this answer
1  
The editor overwrite the file, the logs append the text. This maybe be the problem. –  Juan Francisco Cantero Hurtado Aug 15 '11 at 2:16
    
@Juan: I don't understand your comment. "overwrite" has no concrete meaning except for the one I described in my answer. –  Stéphane Gimenez Aug 15 '11 at 11:51
    
Yes, I did mean the same that you :) –  Juan Francisco Cantero Hurtado Aug 15 '11 at 13:24

tail "refresh" each 1 second by default, not realtime.

Try with this (you need bash4):

  • Open 2 terminals.
  • In the first terminal execute touch ~/output.txt and tail -f ~/output.txt.
  • In the second terminal execute for i in {0..100}; do sleep 2; echo $i >> ~/output.txt ; done
  • Look at the output of tail in the first terminal.
share|improve this answer
    
Did you mean echo $i >> ~/output.txt? Also, this answer misses the point of the question. –  Ignacio Vazquez-Abrams Aug 15 '11 at 2:09
1  
Yes, I corrected the fail while you wrote the comment :) . My answer is only a test for the problem The tail -f command only shows new entries if they are written by system only? –  Juan Francisco Cantero Hurtado Aug 15 '11 at 2:12
3  
@Juan: Nowadays, on linux, tailf has a inotify based implementation. So it will refresh in realtime. –  Stéphane Gimenez Aug 15 '11 at 2:13
    
Yes for tailf, but tail uses inotify?. I didn't know tailf. The manpage of tail show the default of 1 second for -s. –  Juan Francisco Cantero Hurtado Aug 15 '11 at 2:25
3  
Yes, tail followed and is now using inotify too when available. tailf is not polling at all, just sleeping, when there is no activity on the file. tail -f shows some activity (see strace output). –  Stéphane Gimenez Aug 15 '11 at 3:46

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.