Have you ever wanted to do a grep search on all files for a variable such as $_REQUEST, $_GET or $_POST but couldn’t figure it out? I have tried to do this a few times but end up just giving up because I don’t usually figure it out. This time I wanted to jot down my solution and figured I may as well put it here so everyone can see it. Skip to the end to see the right command to use.
Basically I wanted to recursively search through all files in the current folder including all sub-folders. In this case I was looking for $_REQUEST[‘ID’] in some PHP scripts. However when I tried to do a regular old grep -r on it…
grep -r '$_REQUEST['ID']' .
… well obviously we can’t have the extra single quote around the ID part. So I tried escaping them:
grep -r '$_REQUEST[\'ID\']' .
Unfortunately that also doesn’t produce anything (ctrl+c out of that > prompt). OK, so there is a special way to escape single quotes on a shell. You have to break out of the single quote and then escape it. Let’s try that:
grep -r '$_REQUEST['\''ID'\'']' .
Well, that didn’t work. Now we have blankness again.
So I tried:
grep -r '$_REQUEST[' .
And it tells me grep: Invalid regular expression
Ok, so it must be using the [ as a regular expression. Alright well then let’s escape that too and see if we get our list of files yet.
grep -r '$_REQUEST\['\''ID'\''\]' .
Jackpot! That gives me a list of files, and the line of code with $_REQUEST[‘ID’] highlighted and that’s exactly what I am after. Note that there are no double quotes, they are all single quotes.