Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following is working as expected.

find /opt/ -name "somefile.out" -exec grep 'Connection refused' {} \; | more

But if I want to search only in the tail of the found files, I can not add tail or tail -100 like this...

find /opt/ -name "somefile.out" -exec grep 'Connection refused' tail {} \; | more

What is the best way to search for text in the last few lines?

share|improve this question
up vote 2 down vote accepted

This seems to work for me, not sure if it's the best way:

find . -name '*.log' -exec tail {} \; | grep GET | more

The main thing is executing the commands in a more correct order.

Good luck!

share|improve this answer

Your question is probably already answered here:

You can use perl for both tail's and grep's job, though (note: this is semi-tongue-in-cheek because of the surprising complexity, but it should work):

find /opt/ -name "somefile.out"  -exec  perl -ne 'push @a, $_; @a = @a[@a-NUMBEROFLINES..$#a]; END { foreach (@a) { print if /PATTERN/ } }' '{}' \;

If instead of a definite range of lines you want to scan after a certain "footer" is seen perl does make it easier on you:

perl -ne 'print if /footer/ .. eof() and /PATTERN/'
share|improve this answer
Nice tips, I guess perl is good for something after all! – baraboom Jun 30 '11 at 21:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.