How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried:

grep -lr "this string" *

but I do not believe that is doing what I want.

find myDirToStartWith -type f | xargs grep 'this string'

Do you think you could explain that line for me?

find myDirToStartWith -type f --- means go to the specified directory and look for a regular file? So if I start my grep search from within the directory where I want the search to begin than I can omit this line correct?

| --- means OR in my mind but I am assuming that in this instance it just separates two different commands?

xargs --- This will execute a command recursively? grep in this case

find myDirToStartWith -type f | xargs grep 'this string'

Starting at directory 'myDirToStartWith' find recursively all the files of type file.
"pipe" (|) all the output of find as input to xargs grep 'this string' which greps for string 'this string' in each of it's input files.

Hope it helps.

1 Like