4

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?

1
  • It's fine if the answer is below. Please don't add an answer to your question. Thanks!
    – slhck
    Nov 23, 2011 at 9:59

3 Answers 3

5

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)
1
  • Thanks! grep -l "othersearchtext" $(find . -iname '*txt' | xargs grep "searchtext" -sl)
    – wormhit
    Nov 23, 2011 at 10:04
1

You can chain xargs grep …:

find . -iname '*txt' | \
    xargs grep -l "searchtext" | \
    xargs grep -l "othersearchtext" | \
    xargs grep -l "yetanothersearchtext"
0
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .