Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

I am trying to find some keywords in my /var/log directory, so using

cd /var/log cat * | grep keyword

I find the string is in that directory and see the lines it exists on, but don't know which file it came from. How can I locate the string, and see the file it cat from?

share|improve this question
up vote 4 down vote accepted

grep can take file names as a parameter.

cd /var/log
grep keyword *

And if you grep from more than one file at a time, the filename from which the line came from will be printed along with the found line.

If you only supply 1 file name to grep, but you want to show the name on the file anyway, pass the -H option to grep -- useful if you use a globbing (e.g. *.txt) at the command line and don't know how many files will be searched).

If you want to show line numbers as well, that's the -n option.

share|improve this answer
Awesome, thanks! – wjimenez5271 Jul 23 '11 at 20:37

find /var/log -type f | xargs grep -H


find /var/log -type f -name \*log | xargs grep -H

or (much slower than xargs)

find /var/log -type f -exec grep -H {} \;

and for bonus points, grep -i will make the search case-insensitive (but slower)

share|improve this answer
or better yet: grep -r "search phrase" /var/log/* – robbyt Nov 21 '11 at 17:05

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.