0

This question already has an answer here:

On at least CentOS systems and previous versions of Fedora, I could run the following command and get a list of files with the corresponding grep matches:

$ find -name "version.php" -type f | xargs grep "^\$wp_version" site.com/wp-includes/version.php:$wp_version = '4.7.3';

Apparently though on at least Fedora 22 and newer, running that same command no longer includes the path and filenames:

find -name "version.php" -type f | xargs grep "^\$wp_version" $wp_version = '4.7.3';

I know I can include -l to show just the path, but is there a way to include the path without dropping the match? I don't see anything in the man file.

marked as duplicate by don_crissti, Jeff Schaller, sam, GAD3R, countermode Mar 16 '17 at 21:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

CentOS/Fedora, so assuming GNU grep, which has the following switch:

-H, --with-filename
   Print the file name for each match.  This is the default 
   when there is more than one file to search.

In other words, the difference in behaviour might be just that in the first case, find catches more than one file, making grep list the file names. If it matches only one file in the second case, the names would be missing.

So, add -H manually to force it.

  • thanks, any idea why that is not in the man grep? By the way, it works without the -H on CentOS, it's Fedora where it wasn't working. – Michael VanDeMar Mar 17 '17 at 2:10
  • Or you could just slip in /dev/null to grep to make the filename+match to always be printed: find ... | xargs grep version /dev/null – user218374 Mar 17 '17 at 8:08

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