Linux – Tail, cat and grep a file at the same time

bashgreplinuxtailunix

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?

EDIT:

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

log.txt:
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.

Best Answer

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"