So I need to monitor a log file in linux and I'd like to cat the contents (to see all previous entries) as well as tail the log file while simultaneously using the grep to filter out the stuff I want to see from the log. Also, it would be nice if the tail would continue where the cat command stopped displaying entries.

How would I go about doing that?


To make my question a bit more clear, here is what I want:

Line 1 <--- Starting from here is what lines I need
Line 2
Line 3
Line 4
Line 5 <--- Here is where the tail command will start displaying
Line n - 1
Line n <--- Here is where the tail command will continue to go

I need to be able to grab all of this and grep it.

2 Answers 2


I don't know if this is what you are looking for but you might try this:

grep "pattern you are looking for" log.txt;tail -n 0 -f log.txt | grep "pattern you are looking for"

it's calling two commands one after another. first you grep the pattern you are interested in from the log file and then you start tail -f at the end of the file redirecting output to grep.

however I suggest you do it using one command only, you can specify the number of lines to be displayed by tail using -n parameter (+1 means from the first line):

tail -n +1 -f log.txt | grep "pattern you are looking for"

I would go for something like

tail -f log_file | grep -E "^|your_pattern"

Test it by opening two tabs.

In the first, run this command

while :; do echo "$v"; ((v++)); sleep 1; done > mytest

That is, generate a continuous stream adding a number every second to a file mytest

Then, in the other tab, say

tail -f mytest | grep -E "^|2"

That is, tail the file continuously and grep for two patterns: ^ matching everything and 2. This way, ^ matches everything making all lines to be shown, and 2 just matches those lines you want to highlight. More info on this trick in “Grep” that highlights instead of filter.

  • 1
    +1 for the cute trick. The original question is impossible, though. Dec 21, 2015 at 10:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .