90

I would like a tail -f type of behavior that reads the entire file and then continues to follow it as it's written to.


SOLUTION

Based on the answer I accepted, this works: tail -f -n +1 {filename}

Why it works: The -f option continues to "follow" the file and output new lines as they are written to the file. The -n +1 instructs tail to start reading the file from the first line. Using -n -10 would start with the last ten lines of the file.

Sonny
  • 1,245
  • 2
    This is a comment not a question. And does tail not already do that: tail -f -n 10000000000000000000 probably will show all the lines would it not? (maybe the 10000000000000000000 is a bit much :=) ) – Rinzwind Aug 11 '14 at 17:34
  • I believe more fileName would do just that – ryekayo Aug 11 '14 at 17:37
  • 1
    no..more will not add extra lines added to the file after more was started. – Rinzwind Aug 11 '14 at 19:17
  • Presumably tail -f -n 100... will involve pre-reading the file to determine the number of lines, unless it's smart enough to work out an upper bound for the line count based on the file size. – mwfearnley Oct 15 '18 at 08:42
  • see also: https://stackoverflow.com/questions/11291465/bash-alternative-to-cat-in-reading-writing-to-named-pipe – user1742529 Dec 25 '19 at 07:51

2 Answers2

103

Use

tail -f -n +1

Using man tail will give you more details, the relevant excerpt follows.

<snip>Numbers having a leading plus (`+') sign are relative to the
beginning of the input, for example, ``-n +2'' starts the display at the
second line of the input.</snip>

-f      The -f option causes tail to not stop when end of file is
        reached, but rather to wait for additional data to be appended to
        the input.  The -f option is ignored if the standard input is a
        pipe, but not if it is a FIFO.

-n number
        The location is number lines.
Timo Kluck
  • 8,893
  • 4
    Could you please add those "more details" to your answer? Please explain why and how your solution works. (Found your answer in the low quality posts queue.) – kraxor Aug 11 '14 at 19:36
  • 1
    @kraxor You can find the "more details" by executing man tail (and reading it) like the answer says, or do an online search for "tail manpage" and read one of them. tail works by starting at a position in a file, reads (and displays) to the end, then monitoring for file size increases. When the file size increases, tail reads and displays from the former EOF position to the new end of file. – waltinator Aug 11 '14 at 23:08
  • 3
    @waltinator I didn't really ask for myself. I asked him to add more details because his post was flagged low quality but I didn't want to vote to delete because he gave a correct answer. – kraxor Aug 11 '14 at 23:25
  • 1
    This is exactly what I was looking for. I have used the tail -50 {filename} previously, but wasn't having success combining the two. – Sonny Aug 12 '14 at 15:15
  • 1
    http://explainshell.com/explain?cmd=tail+-f+-n+%2B1 – brooks94 Nov 10 '15 at 14:07
  • To save you a click: The n parameter that begins with the + sign refers to the position from the beginning of the file, while a number without any prefixes refers to the number of lines from the end of the file. So -n +1 starts tailing from the beginning of the file and -n 10 starts tailing from 10th line from the end of the file (where latter is the default behaviour if you omit the -n param). – Marko Bonaci Feb 13 '16 at 18:26
  • @kraxor This shouldn't have been flagged as low quality in the first place, and the person who did flag it should be banned for abuse. – user234461 Oct 07 '16 at 10:59
3

Try this:

watch tail {filename}

where {filename} is the file that you want to keep an eye on. This will continuously monitor the command for changes and output the changes to stdout. It's pretty handy.

Eliah Kagan
  • 117,780
Rick Chatham
  • 378
  • 1
  • 11
  • This looks like a very nice tool. While it didn't quite give the results I was looking for, I will definitely keep it in mind. Combining it with the accepted answer is pretty nice: watch tail -n +1 {filename} – Sonny Aug 12 '14 at 15:17
  • Yep, looks like you perfected it! – Rick Chatham Aug 14 '14 at 18:40
  • watch is really a useful tool but for watching of changing output of a command but to watch the end of a file being appended it is so much better to use tail -f. --- watch tail {filename} will reopen and reread the end of the file every two seconds. On the other hand tail -f watches for the file's growth and when detected it immediately reads just the appended part. tailf -f is much more efficient and the reaction is much faster. In addition it will continuously show the file content from the point where you begun. --- watch overwrites its output every two seconds. – pabouk - Ukraine stay strong Feb 08 '19 at 15:25