No, tail
doesn't read the whole file, it seeks to the end then read blocks backwards until the expected number of lines have been reached, then it displays the lines in the proper direction until the end of the file, and possibly stays monitoring the file if the -f
option is used.
Note however that tail
has no choice but to read the whole data if provided a non seekable input, for example when reading from a pipe.
Similarily, when asked to look for lines starting from the beginning of the file, with using the tail -n +linenumber
syntax or tail +linenumber
non standard option when supported, tail
obviously reads the whole file (unless interrupted).
You describe the GNU tail
utility. The difference between these two flags is that if I open a file, a log file for example, like this:
$ tail -f /var/log/messages
... and if the log rotation facility on my machine decides to rotate that log file while I'm watching messages being written to it ("rotate" means delete or move to another location etc.), the output that I see will just stop.
If I open the file with tail
like this:
$ tail -F /var/log/messages
... and again, the file is rotated, the output would continue to flow in my console because tail
would reopen the file as soon as it became available again, i.e. when the program(s) writing to the log started writing to the new /var/log/messages
.
On the free BSD systems, there is no -F
option, but tail -f
will behave like tail -F
does on GNU systems, with the difference that you get the message
tail: file has been replaced, reopening.
in the output when the file you're monitoring disappears and reappears.
YOU CAN TEST THIS
In one shell session, do
$ cat >myfile
That will now wait for you to type stuff. Just go ahead and type some gibberish, a few lines. It will all be saved into the file myfile
.
In another shell session (maybe in another terminal, without interrupting the cat
):
$ tail -f myfile
This will show the (end of the) contents of myfile
in the console. If you go back to the first shell session and type something more, that output will immediately be shown by tail
in the second shell session.
Now quit cat
by pressing Ctrl+D, and remove the myfile
file:
$ rm myfile
Then run the cat again:
$ cat >myfile
... and type something, a few lines.
With GNU tail
, these lines will not show up in the second shell session (where tail -f
is still running).
Repeat the exercise with tail -F
and observe the difference.
Best Answer
Here's one way to do it:
There doesn't seem to be any difference between a
+0
and a+1
, so this would be equivalent: