19

How can I open a text file and let it update itself? Similar to the way top works.

I want to open a log file and watch it update itself on the fly.

I have just tried:

$ tail error.log

But just realised, that it just shows you the lines in the log file.

I am using RHEL 5.10

  • 4
    Use tail -f error.log – garethTheRed Sep 9 '14 at 14:51
  • Ah, so tail is the correct way to use it? Wow, I really didn't think I could use that. I was expecting a much longer expression. – Kevdog777 Sep 9 '14 at 14:57
  • 1
    FYI, while tail is the tool for the job, literally open a text file and let it update itself can be achived with watch cat filename. – Cthulhu Sep 9 '14 at 15:04
  • @Cthulhu you mean watch cat filename or similar. watch filename will attempt to execute filename. – terdon Sep 9 '14 at 15:07
  • @terdon Yes indeed. – Cthulhu Sep 9 '14 at 15:07
26

You're looking for tail -f error.log (from man tail):

   -f, --follow[={name|descriptor}]
          output appended data as the file grows; -f, --follow, and --fol‐
          low=descriptor are equivalent

That will let you watch a file and see any changes made to it.

  • And this won't use up much resources? – Kevdog777 Sep 9 '14 at 14:58
  • 1
    @Kevdog777 not really no. Presumably, it just re-opens the file every few seconds to update it. This is the classic way of doing what you want though. – terdon Sep 9 '14 at 14:59
  • 1
    @terdon, it only reopens (may reopen) with -F. With -f it just reads every second. It's like a while cat; do sleep 1; done < file. See inotail on Linux for a more reactive tail (uses inotify to know when the file is being modified). – Stéphane Chazelas Sep 9 '14 at 16:13
  • @StéphaneChazelas: I am sure its a lot more efficient than that. I would bet it use select or poll to see if the fd is readable and then sleep until there is data available on the fd. Though I could be totally wrong. – Martin York Sep 9 '14 at 17:07
  • 1
    @LokiAstari, you can't get much more efficient that a read system call every second. A "read" select or poll on a regular file would always return true since a read() never blocks on a regular file, so wouldn't work. Having said that, GNU tail now uses inotify on Linux, so inotail is no longer needed. – Stéphane Chazelas Sep 9 '14 at 19:57
20

Use "less" instead of "tail" for scrollback and search

You can use tail -f error.log or, better: tail -F error.log.

But if you want to scroll back in the file, that's not very useful.

With

less +F error.log

you get the function of tail -f,
but can interrupt the reading of new input with Ctrl+C.

Then, you are in the normal less mode,
where you can scroll back to see what you may have missed with Up/Down
Also, you can read long log file lines without wrapping, using Left/Right

Search, and show only matching lines

You can also search for regular expressions with /, ? for backward, n and N for next/previous.

Very interesting for log files is that you can hide all non-matching lines for a search with &, filtering out only the matches.

Keys on command line

With F inside of less, you continue the tail -f-like mode.
The + in the command line less +F means "press these keys directly after starting less".

So we used keypress F at startup, which is described as:

F  Scroll  forward,  and  keep trying to read when the end of file is
   reached.  Normally this command would be used when already at  the
   end  of the file.  It is a way to monitor the tail of a file which
   is growing while it is being viewed.  (The behavior is similar  to
   the "tail -f" command.)

See also multitail if you need to watch more than one log file.

  • Thanks, but tail -f error.log is perfect for what I need. The log file only gets updated every hour, so is sufficient for my use :-) – Kevdog777 Sep 9 '14 at 15:09
  • 1
    Make sure to remember it for next time ;) – Volker Siegel Sep 9 '14 at 15:11
  • most also has a tail -f mode with F. – Stéphane Chazelas Sep 10 '14 at 12:46
  • @StéphaneChazelas right, thanks! Hmmm... I remember someone thanked me for mentioning most a while ago... and I forgot it. – Volker Siegel Sep 10 '14 at 12:51
  • @StéphaneChazelas But then, most does not seem to have the less filter feature & to show only matching lines? A useful feature in log files, usually. – Volker Siegel Sep 10 '14 at 13:28
5

Use -f option with tail:

-f, --follow[={name|descriptor}] output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent

Or use F command inside less:

   F      Scroll forward, and keep trying to read when the end of file is reached.  Normally this command would be used when already at the end of the file.  It is a way to mon‐
          itor the tail of a file which is growing while it is being viewed.  (The behavior is similar to the "tail -f" command.)
  • What is new in this answer? – bluefoggy Sep 9 '14 at 15:54
  • 1
    I don't understand what you are asking about. It was the second answer to this question and the first one that mentioned less. – Arkadiusz Drabczyk Sep 9 '14 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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