0

I'm trying to find out in what files some specific identifiers are being used. I know the file's name and know the identifier, therefore I use the following command to achieve my desired result:

find ./ -name "configuration.php" -print | xargs cat | grep db_userXYZ

The only issue is that, I'm losing the file name. I see that db_userXYZ is being used somewhere, but the command above, does not tell me in which file. The output would be something like:

$this->db_user = 'db_userXYZ';
$this->db_name = 'db_XYZ';

Any suggestions?

3

Try:

find ./ -name "configuration.php" -exec grep db_userXYZ /dev/null {} +

POSIX defined find -exec utility_name [argument ...] {} +:

If the primary expression is punctuated by a plus sign, the primary shall always evaluate as true, and the pathnames for which the primary is evaluated shall be aggregated into sets. The utility utility_name shall be invoked once for each set of aggregated pathnames. Each invocation shall begin after the last pathname in the set is aggregated, and shall be completed before the find utility exits and before the first pathname in the next set (if any) is aggregated for this primary, but it is otherwise unspecified whether the invocation occurs before, during, or after the evaluations of other primaries. If any invocation returns a non-zero value as exit status, the find utility shall return a non-zero exit status. An argument containing only the two characters "{}" shall be replaced by the set of aggregated pathnames, with each pathname passed as a separate argument to the invoked utility in the same order that it was aggregated. The size of any set of two or more pathnames shall be limited such that execution of the utility does not cause the system's {ARG_MAX} limit to be exceeded. If more than one argument containing only the two characters "{}" is present, the behavior is unspecified.

If your grep supports -H option (-H is not defined by POSIX grep), you can use:

find ./ -name "configuration.php" -exec grep -H db_userXYZ {} +

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.