Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

#!/bin/bash
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

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
1  
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

Your Answer

 
discard

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.