views:

32

answers:

3

I have a script that I'm running from the home directory to search for all files called "script.sh" that contain the string "watermelon". It's not finding anything but I can clearly see these scripts in the subdirectories. Could someone please suggest a change to the command I'm using:

find . -name script.sh | grep watermelon
+1  A: 

You need to use xargs:

find . -name script.sh | xargs grep watermelon

xargs will modify the behavior to search within the files, rather than just search within the names of the files.

Amit Kumar
+2  A: 

find returns the filename it finds by default. If you want it to search within the files then you need to pipe it to xargs or use the -exec and -print predicates:

find . -name script.sh -exec grep -q watermelon {} \; -print
Ignacio Vazquez-Abrams
+1  A: 

use -type f to indicate file

find . -type f -name "script.sh" -exec grep "watermelon" "{}" +;

or if you have bash 4

shopt -s globstar
grep -Rl "watermelon" **/script.sh
ghostdog74