grepfindpathname

How to show the full path for a file found using the find command and given to grep to match a string


I have a lot of config.php files in subdirectories below where I invoke the find command and I want to list the path names of only those for which a grep string match is found. Here is what I've tried so far.

find `pwd -P` -name config.php -print -exec grep 'assetBasePath.*cloudfront' {} \;

It partially does the job because find does list the paths for all the config.php files it found, and grep prints the matching line in the file if the pattern does indeed match one.

What I want to achieve is an output similar to the above, but where only the pathnames for the files that have a grep match are shown.


Solution

  • Move the -print to the right-hand end so it is "gated" by the outcome of exec:

    find `pwd -P` -name config.php -exec grep 'assetBasePath.*cloudfront' {} \; -print