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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThere 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.
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
.
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
.
+
) is implemented more efficiently.
+1
for teaching me about +0
. You may want -F
instead of -f
for rotating logfiles
Apr 28, 2016 at 15:22
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.
Apr 28, 2016 at 20:51
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.
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.
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.
Apr 28, 2016 at 20:53
{ 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.
Apr 29, 2016 at 17:00
less
has the "F" key. Useful if you need interaction.