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

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!
Dan

4 thoughts on “How to search for specific text within your website’s PHP files using SSH

  1. One thing you could add if you’re simply looking through php files could be

    grep -r -i -l “keyword” *.php

    Since the * is a wild card and .php falls after, it will check for anything ending in .php

    I was working on an assignment, and I came across several cool related things like this.
    Say you have a directory of .mp3 files, and you want the words in the file name to be separated by and underscore rather than a space

    rename ‘s/ /_/g’ *.mp3

    If you wanted to recursively rename all files from uppercase to lower case you could use

    cd /directory
    find . -name ‘*[A-Z]*’ -type f -print0 | xargs -0 rename ‘y/A-Z/a-z/’

Leave a Reply

Your email address will not be published. Required fields are marked *