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)?