I always preach about how powerful the SSH command terminal can be, and how it has helped me become more rounded as a developer. But it’s not the terminal that makes SSH so dynamic — it’s the different command libraries. Today, I want to introduce you to the grep command.


“Better Start Greppin'”

I first heard of grep on Stack Overflow after searching Google for something along the lines of “how to search through my website for a line of code”. In one answer I read, “better start greppin’.” For a moment, I thought it said “reppin” — which I thought was an odd response — but a quick search revealed the real answer.

The grep command line utility was created in the 70’s for the exact purpose we want to use it for: to search through our website (to find where we put that buggy code?).

Here is the syntax for grep:

grep "some needle" inthehaystack.php

Searching Your Entire Site With GREP

The Scenario: You update WordPress, which seemingly works without issue, but then you notice that jQuery stopped working because of an error. In this case, the error would be a conflict issue between two jQuerys being loaded at the same time (one by your theme, one by WordPress). Now, you need to find the file that contains the one loaded by WordPress to prevent it from loading and throwing the error.

Navigate into your HTTP root directory with the “cd” command, then execute the following code:

grep -l -r "jquery" *

This will tell grep to return the filename (“-l”), recurse into sub-directories when looking (“-r”), and search through everything (“*”).

If you require even more functionality, you can find tens of thousands of detailed articles online about using grep in cases other than the ones I mentioned here. Good luck!

About The Author: Allen has worked on the web for over a dozen years. Like many young entrepreneurs, he began with a small workspace in his basement, where he eventually formed Ideas and Pixels. The rest, well, is history. Allen enjoys fine wines, weight training, and beautiful, semantic code.

  • Alyssa

    Love grep! Just starting using ssh and there’s no way I’m going back.

    • http://www.facebook.com/people/Allen-Gingrich/1435959906 Allen Gingrich

      Me, too. SSH is so powerful. I mean, it is intimidating, but once you get the hang of it, it’s easier than pretty much any language lol

    • http://www.facebook.com/valvanomatt Matt Valvano

      Allen just showed me how useful it is and I was really blown away by it! We just had to search a giant eCommerce site for a single line of code and we did not build it. This method saved us hours, probably days!