I have a script that I'm running from the home directory to search for all files called "script.sh" that contai开发者_开发百科n 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
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.
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
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
精彩评论