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.
grep
“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!
Love grep! Just starting using ssh and there’s no way I’m going back.
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
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!