Common Linux Command Line Tools – grep


Welcome to the first in what I hope to be a regular series of  the basic usage of my most commonly used Linux command line tools.

As some of you may know, I’m a sysadmin, not a programmer. You will hear me repeat this endlessly — not so-much as to convince others of this fact, but more to convince myself. Unfortunately, my job often requires that I dive into piles of PHP code to try and find out why something is working, or not working, the way it is. Since I’m often-times unfamiliar with the code behind the website, I’ll have to search through dozens, or even hundreds of files for certain keywords.

Here’s where grep comes in handy.

Often times I’ll be moving a website from one server to another, and I’ll have to change a MySQL address or password. Most publishing platforms like WordPress, or forums like vBulletin will have this info stored in a flat config file which makes it easy to locate. However, often I’ll have to move a custom site where the programmer wasn’t so nice. I’ll usually start with something

grep mysql_connect *.php

This will search through all .php files for “mysql_connect” and print out the line containing the username and password.

mysql_connect("localhost","username","password");

Sometimes you won’t get so lucky, and it will instead print out something like this:

db_fuctions.php:mysql_connect($server, $user, $password);

But don’t feel frustrated! This just means you’ll need to grep for where the variables are stored, then parse through that data manually.  For example:

grep \$user *.php

Note the backslash (\) used here. That’s because the dollar sign ($) is normally used for other things via regular expression code (regex), so we have to “escape” it with a backslash to tell grep that yes, it should actually be searching for that dollar sign.   With any luck, grep should spit out something like this:

config.php:$user = username;

The first part, before the colon (:) is the file name the line is contained in. The second part is, of course, the line you’re looking for.  All you have to do is open that file in your favorite editor, and change whatever needed to be changed!

But what happens if you’re not so lucky as to have all the files in one place? Well, worry not young CLI newbie, grep has a solution to that as well! For this example, we’ll assume that there are a few sub-directories, and many  .js, .css, .jpg, gif, ,png, and .txt  files in the directories as well.  So, how do we search through all of the .php files recursively (“recursive” means to repeat the search in all directories, and all directories of directories, etc.) without having to search through non-php files?  Like this:

grep -r --include=*.php \$user ./

-r means “recursively”

--include=*.php means to look in all directories only for files that match a pattern, in this case, *.php

\$user is what we’re searching for, in this case $user with an escape before it.

./ means the current directory (if you only use / it will search through the root directory, and take quite a long time!)

This will probably take some time if you have a lot of files to search through, eventually though, you’ll see output similar to the above, but with path info in front of it like this:

func/inc/config.php:$user = username;

This is of course a very, very basic real-world demonstration of how grep can be useful, and there’s many other ways to use grep, and quite a few of them are very, very complex.  I’d suggest checking out the man page for grep (man grep), or hitting up Google for more complex usages.

%d bloggers like this: