How to grep a $_REQUEST, $_GET or $_POST variable in all files recursively

by | Mar 9, 2015 | Other | 0 comments

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.

Hosting Recommendation

Hostinger - The Secret Weapon of Successful Site Owners

There's a reason I recommend Hostinger. After testing dozens of hosting providers, only Hostinger delivers the perfect combination of:

🔥 Blazing-fast load times
(essential for performance)
🛡️ Rock-solid uptime
(your sites earn money 24/7)
🔒 Free SSL certificates
(builds trust instantly)
💾 Automated backups
(your business is always protected)
👨‍💻 Expert support
(problems solved in minutes, not days)
⚙️ One-click WordPress setup
(seamless integration guaranteed)
💰 Starting at just $2.99/month
(premium hosting that won't break the bank)

Use my link or referral code
MILLIONDOLLARS to get 20% off !