How to search for specific text within your website’s PHP files using SSH

Here’s a quick little tip for anyone that needs to plow through a zillion PHP files to find a specific line of code or a specific keyword.   I recently needed to scan the entire P2L folder for a specific word within any PHP files in the main public_html and subfolders.  The alternative would be to download everything to your local PC and then run a search locally on your PC.  That’s fine and dandy if you have a small site that would only take seconds to transfer, but it’s a different story when you have dozens of folders and subfolders and hundreds of pages.

So, let’s put the magic of Linux to work shall we?  Open up Putty or whatever SSH program you use and log in and make your way to the public_html folder of the account you want to search.

Here’s the command you want to run:

grep -r -i -l "keyword" *

Hit enter and the quary will run… be patient as it will take some time to complete.  It will list any files found that contain your keyword as the query runs.  Here’s a breakdown of the command flags used:

-r recursive (will search folder and subfolders)

-i case insensitive (ignores UPPERCASE or lowercase)

-l list only (displays names of files found only)

Hope that helps!