31

Via command line, I have a log file I'd like to keep track of.

What I want is to have, basically, a tail that refreshes when the log is updated making the text scroll upwards as new lines are appended to the log file.

Is there anything out there that does that without having to write some code?

fossfreedom
  • 172,746
WernerCD
  • 632

5 Answers5

37

tail has the -f option:

From the man page:

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

Thus if you type:

tail -f [path_and_name_of_logfile] - you will see the output in the terminal as the log file itself is appended to.

N.B. [path_and_name_of_logfile] is the parameter, so to give an example:

tail -f /var/log/messages

If you combine with the -n [number_of_lines] option you can start the output from the last [number_of_lines] in the file - for example

tail -n 10 -f /var/log/Xorg.0.log

enter image description here


Some programs will periodically change their log file, moving the old one to a new name (e.g. log.0) and starting over.

N.B. logrotate does this to log files for other programs that don't do it themselves.

tail -f will continue to follow the old file after it's renamed.

tail -F will follow the file by name, so will switch to follow the new file.

fossfreedom
  • 172,746
  • both answers are spot on... didn't realize Tail had that option. you get the check for the extra info. – WernerCD Sep 13 '11 at 22:07
  • As an aside, theres also a few awesome scripts out there to colorize your tailing log. one example that comes to mind is colorize.pl, and you achieve this by piping it through the colorize script. eg: tail -f /var/log/messages | /home/me/colorize.pl – lazyPower Sep 14 '11 at 05:27
  • @lazyPower Nice as well. I'll have to tinker with that, although I don't think it'll super useful in my current situation. – WernerCD Sep 14 '11 at 18:28
20

While tail is certainly the usual way to do this, it should be noted that less has the same feature and is sometimes more usefull.

If you opened a file with less then you can press Shift + F to have it follow the file (i.e. it will display new lines, just as tail -f does). You can exit this mode with Ctrl + C

You can also start less with the +F option, in which case it will start in that mode. Generally speaking + can be used to give "keyboard-commands" to less which it will execute upon startup.

Using less for this has the advantage that you can easily search the file or view other areas if the need arises. I've frequently done that with log files, for example.

  • Never heard about "follow" (more, less, tail, cat). Good to know stuff. – WernerCD Sep 14 '11 at 18:27
  • 2
    Agreed. I much prefer less. Ctrl-C will stopp the scrolling when you spot something interesting, then b to scroll back or f forward and / or ? to search forward or back. g will take you to the start of the file and G to the end. Everything you need. – Martin Dow Sep 15 '11 at 20:47
6

Does

tail -f something.log

do what you want?

4

tail has two implementations to follow and output data that gets appended to a file

  • follow the file descriptor
  • follow a file with a certain name

With parameter -f, --follow and --follow=descriptor, tail follows the file descriptor. This method allows the file to be followed across a renaming but the tracking stops when the file is rotated (a new file is created with a different file descriptor).

When following files that are rotated, either use --follow=name or -F parameter which equals to --follow=name --retry. That way tail will periodically reopen the file to overcome the possible rotation.

Jawa
  • 141
1

I think ccze is the right tool for you.

It does same thing of colorize. You can watch log scrolling down, but it is easier to read, because lines are printed with colors (errors in red, and so on). You can quickly try it with something like this:

tail -f /var/log/syslog | ccze
Eliah Kagan
  • 117,780
Gelma
  • 107