38

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.

  • 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 8:42
  • see also: stackoverflow.com/questions/11291465/… – user1742529 Dec 25 '19 at 7:51
55

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.
  • 3
    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
  • 2
    @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
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.

  • 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 Feb 8 '19 at 15:25

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.