8

I am trying to tail file with pagination:

tail -f foo.txt | more

This works fine until file gets, lets say, 200 lines injected, when this happens nature of tail command is to go to end of file, at that point I lose track of trailing the log.

Is there a way to avoid this?

3 Answers 3

9

Use tail -f log.txt | less instead . On my quick testing, it updates to advance to the new information, and you can then go up or down. I.e., less updates as an output buffer, not statically referring to the original content.

Or, a slight variation with fewer moving parts (no pipe) -- the solution that Dolphin mentioned in a comment -- less +F log.txt is a nice way to go. Here's what the man page says for F:

   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.)

In other words, this is perfect for this case.

It does have one problem that prevents it from being my favorite. An addition like less +F daemon.log | egrep session doesn't work.

One reason I continue to use pipes in my own day-to-day command line work is that, when coupled with history, it's crazy easy to go back, add a pipe, and change the behavior of your command as the data dictates. If a simple tail -f log isn't working because of the volume of data scrolling off the screen, slap on the pipe to less. If the volume is too heavy, but you see hints of the lines you're looking for, add a pipe to grep or egrep. Then chain awk, or sed, or cut, to get exactly what you want.

This is also a reason that "unnecessary use of cat" is often a ridiculous criticism. If you cat a file, but find that it is unexpectedly long, use the shell history, hit up-arrow and do something else with it. Quick fix? Just add cat foo | more. How long is it? Up-arrow and | wc. How many lines are there with "pangloss"? Add | egrep pangloss | wc . It's nice if you can start a command knowing exactly where your investigation is going to go, but if you can't, history and pipes are a good way to get things done. Even if it's ugly, cat file | more | egrep something works fine.

4

My solution is this:

less some-file.log

Then, in less I hit Shift+F. This causes less to update with the file. To scroll back, just hit Ctrl+C.

1
  • 1
    I settled with less +F foo.log same command and same shortcuts just starts of with following file.
    – Dolphin
    Nov 5, 2012 at 11:58
1

Not really, using that method. You are trying to use programs that both limit data, but in very different ways.

Try

watch -n 5 tail  -n 40 /var/log/thingy

That will update the tailed display every 5 seconds.

That's as close as I can get, but it's not what you want.

The problem is that more expects all it's data at once, where tail is continuing to pipe. If your just trying to read the last few lines over and over then watch will work. If you want just be able to keep a log and scroll back then use the console backbuffer.

In X this would be scroll bars, if your on a true console then you have to use something like:

kernel /vmlinuz root=/dev/sdb5 video=radeonfb fbcon=scrollback:128 to increase the size at boot time. Then you can use Shift UP and Shift Down to scroll.

This is much easier in X. Just

tail -f /var/log/something.log and scroll back and forth. You can hit enter to create a visual break of where you were.

In a VTY you need to use shift up and shift down to to accomplish the same thing, and if you need more scroll back use the fbcon=scrollback:128 argument at boot.

1
  • You might want to try shift+page up and shift + page down too. Those sometimes work when shift + up/down will not.
    – coteyr
    Nov 2, 2012 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .