21

I want to read whole file and make it waiting for input, just like tail -f but with the complete file displayed.

The length of this file will always change, because this is a .log file.

How can I do it, if I don't know length of the file?

  • 2
    you already know that tail is close to what you want. did you read the man page for it? – njzk2 Apr 28 '16 at 16:14
  • 1
    less has the "F" key. Useful if you need interaction. – Thorbjørn Ravn Andersen Apr 30 '16 at 10:22
33

tail lets you add -n to specify the number of lines to display from the end, which can be used in conjunction with -f. If the argument for -n starts with + that is the count of lines from the beginning (0 and 1 displaying the whole file, 2 indicating skip the first line, as indicated by @Ben). So just do:

tail -f -n +0 filename

If your log files get rotated, you can add --retry (or combine -f and --retry into -F as @Hagen suggested)

Also note that in a graphical terminal, you can use the mouse and PageUp/PageDown to scroll back into the history (assuming your buffer is large enough), this information stays there even if you use Ctrl+C to exit tail. If you use less this is far less convenient and AFAIK you have to use the keyboard for scrolling and I don't know of a means to keep less from deinitialising termcap if you forget to start it with -X.

  • 1
    +1 Your answer appeared as soon as I posted mine and although mine works, yours is better. – Julie Pelletier Apr 28 '16 at 5:07
  • 1
    I got the message while I was still typing that another answer was there and that I should click to refresh. Your answer would get the job done, but I would not be surprised that counting from the beginning ( forced with the +) is implemented more efficiently. – Anthon Apr 28 '16 at 5:12
  • 1
    +1 for teaching me about +0. You may want -F instead of -f for rotating logfiles – Hagen von Eitzen Apr 28 '16 at 15:22
  • I think there's an off-by-one here. tail -n +1 shows the beginning of the file for me, and tail -n +2 skips one line. I think the number is the line number of the first displayed line. – Ben Millwood Apr 28 '16 at 20:51
  • 1
    @Anthon thanks! This is exactly what I need! – malworm Apr 29 '16 at 11:36
37

There is a way better way of achieving this:

less +F <file>

It'll show you the whole file, has the full power of less and will wait for new input. If you want to stop waiting for input, and read a specific part, you can stop it with ^C and resume with F.

The F command is always available in less, if you decide to watch for changes while having a file open in less, hitting F will turn it on. Thanks to hiergiltdiestfu and wildcard for pointing that out.

  • 2
    This is indeed much better than tail. Thanks for the tip; I would never have thought of using less for this. Note for others: You can also just run less <file> and then type F. – Wildcard Apr 28 '16 at 21:11
3

In addition to /u/Anthon's answer, you can do something like:

{ cat filename; tail -0f filename; }

That -0 option to tail is equivalent to -n 0, meaning: dispaly 0 lines. But the -f will display new lines.

You don't need the braces { }. I used them because sometimes you want to redirect the filedescriptors in some way. For instance:

{ cat ; tail -0f -; } < /var/log/messages

Noted by Ben Milwood: you could have a race condition where the file grows between the end of the cat operation and beginning of tail operation. But again, this is an "academic" problem to an academic solution.

  • 3
    If the file grows between when the cat finishes and the tail starts, you won't see those lines. Unlikely to be a big deal, but a reason to prefer the pure-tail solution. – Ben Millwood Apr 28 '16 at 20:53
  • I think { cat; tail -n +0 -f; } < file would fix the race, because tail will print any new data that appears between cat's exit and tail's startup. Of course, it makes the cat redundant. The redirection only happens once, and tail's stdin is the file descriptor that cat already read out to EOF, so its current position is where cat stopped. You can test it by putting an echo foo >> file inside the {}, to create the race every time. – Peter Cordes Apr 29 '16 at 17:00
2

watch command should do that for you.

You can also try

less +FG 

You will have more options with less command to scroll through your file as you say it's a large file.

  • Less also doesn't follow a growing file. – Shadur Apr 28 '16 at 8:01
  • 7
    @Shadur, it does, that's what the +F option is for – glenn jackman Apr 28 '16 at 12:46

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.