-4

grep -r --include=\*.php "find me" ./ does not return results from the PWD. This Question does not provide an answer.

This may not be an issue with grep, but with file globbing statements, as helpfully explained in this Answer on Unix.

I'm looking to know whether there a flag or some way to search both the PWD and recursive directories in one command, or if I need something complex such as | or &&.

Or, perhaps there is a simple globbing statement that matches, for example, all .php files in both the PWD and in recursive subdirectories. All statements I've found so far only match one or the other.


grep searches recursively, looking both in files and directories defined by the file argument. Designating a file extension in the file argument also means grep will only search in directories containing that file argument.

Say I have these files containing the phrase "find me":

./file1.js
./file2.php
./inc/file3.js
./inc/file4.php
./inc.php/file5.php

But, I am only looking for results from .php files

grep 1:

grep -R "find me" *

Returns:

./file1.js
./file2.php
./inc/file3.js
./inc/file4.php

grep 2:

grep -R "find me" *.php

Returns:

./file2.php
./inc.php/file5.php

...because grep wants to match both directories and files containing ".php"; so it doesn't match ./inc/file4.php

grep 3:

grep -R "find me" */*.php

Returns:

./inc/file4.php
./inc.php/file5.php

...because file2.php does not have something/ in front of it, it's in the $CWD

I want the output:

./file2.php
./inc/file4.php
./inc.php/file5.php

Does grep have a flag or simple file argument to do this in a single command using grep or do I need two commands (grep 2 & grep 3 in the examples above)?

Jesse יִשַׁי
  • 483
  • 1
  • 6
  • 22
  • No! This dup does not answer my question. `grep -r --include=\*.php "find me" ./` does not return results from the $CWD. And, I want a flag, not a `|` pipe to another command. This is NOT a dup. – Jesse יִשַׁי May 15 '22 at 05:17
  • Reopen because there is an Answer: `grep -r --include=\*.php 'find me' ./` (from the false dup) does not work, but `grep -R --include="*.php" 'find me' .` (not an answer to [my other question](https://stackoverflow.com/questions/72246463)) does work and is the only place I found an Answer for this. People are closing it because they are reading too fast. – Jesse יִשַׁי May 15 '22 at 17:25
  • 1
    It's still not a programming problem, but a "read the man-page of a single Linux command one" and doesn't deserve reopening. – tink May 15 '22 at 17:30
  • @tink okay, we need to read the manual. It just says `--include=GLOB`, but doesn't explain why the code from my first comment above doesn't work, but your solution (in my second comment above) does work. So, it goes back to globbing, but this Question is about how to express the glob for `grep` specifically. I'd like you to put your Answer here so I can mark it correct. That is the only place I've seen it anywhere. – Jesse יִשַׁי May 15 '22 at 17:35
  • 1
    It might be an interesting exercise to figure out **why** your own incantation didn't work for you - in my environment `grep -R --include=\*.php "Starting" *`, `grep -R --include=\*.php "Starting" .` `grep -R --include=\*.php "Starting" ./` alld produce the desired result. Same if I replace `-R` with `-r` ... but again: that's not a job for [so]. – tink May 15 '22 at 17:41
  • @tink, goodness! They both do! And, there is such an [Answer](https://stackoverflow.com/a/35280826/10343144) for all directories and one file extension. I owe you 10,000 Beremie points for patience. – Jesse יִשַׁי May 15 '22 at 17:54

0 Answers0