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