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 search through a number of sites on my server for a few key words and output these to a text file. I only know how to do:

 grep -r "keyword1" *
 grep -r "keyword2" *

I need to be able to combine both keywords into the search and output the locations into a text file.

share|improve this question

TIP: use a regexp for better performance and to combine results

grep -r -E "keyword1|keyword2" * > results

Combining both searches into the one will result in better performance for larger searches. You get both sets of results without the overhead of running grep again.

If you want just the actual matching part (not the whole line) then add the -o option.

share|improve this answer
I'm not entirely sure from the question if you are looking for the files, or the lines that match. If you are looking for the lines that match, this is exactly the answer. If you are looking for a list of files that match, add "-l" to the command to list the files that match. – Alex Jun 27 '11 at 14:10
touch outputfile.txt
grep -r "keyword1" * >> outputfile.txt
grep -r "keyword2" * >> outputfile.txt

This shell script wiil create a empty destination file an the following two commands will append the file. The script file must be executable.

share|improve this answer

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.