Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to execute double search within files and return file names.

I'm using

find ./ -iname '*txt' | xargs grep "searchtext" -sl

to find file names with 'searchtext' in them.

Command is returning a list of files.

How can I find "othersearchtext" in those already found files and show them in the same fashion?

share|improve this question
    
It's fine if the answer is below. Please don't add an answer to your question. Thanks! –  slhck Nov 23 '11 at 9:59

3 Answers 3

up vote 5 down vote accepted

Feed the result to another grep by using backticks or equivalent $().

If you want to keep the intermediate list, use tee

grep -l oranges $(find . -name "*txt" | xargs grep -l apples | tee apples.txt)
share|improve this answer
    
Thanks! grep -l "othersearchtext" $(find . -iname '*txt' | xargs grep "searchtext" -sl) –  wormhit Nov 23 '11 at 10:04

You can chain xargs grep …:

find . -iname '*txt' | \
    xargs grep -l "searchtext" | \
    xargs grep -l "othersearchtext" | \
    xargs grep -l "yetanothersearchtext"
share|improve this answer

This bash will search multiple strings in given folder recursively and return filenames

Usage:

findfiles /var/www/ php searchText1 searchText2 searchText3 searchText4

bash: findfiles

#!/bin/sh

DIR=$1
EXT=$2

CMD="find $DIR -iname '*.$EXT' | xargs grep -l '$3' | xargs grep -l '$4' | xargs grep -l '$5' | xargs grep -l '$6' | xargs grep -l '$7' | xargs grep -l '$8' | xargs grep -l '$9'"

eval $CMD
share|improve this answer

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.