2

I'm looking for files that contain a string. My command:

grep -rwl '/path/' -e "string"

I want to limit the result to paths that contain a different string. I can do it like this:

grep -rwl '/path/' -e "string" | grep 'path_string'

I would like to optimize the execution, if possible, by only looking at paths with the 'path_string' to begin with. How can I accomplish this?

2

You could use find with -path e.g.

find /path -path '*path_string*' -type f -exec grep -qw 'string' {} \; -print

This will search under /path for file names matching1 shell pattern *path_string* and run grep only on those files, printing their names if successful.


1: the pattern match is applied to the whole file name starting from /path/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.