Grep My Drupal

Grep My Drupal

Quite often I find myself searching for a string or a command line in an entire drupal installation. If you've ever poked around the files and directories of your Drupal site, you appreciate the complexity of all the nested directories and all the files that together make up a typical installation. It turns out that searching for a specific string is quite time consuming and tedious work. So if you equip yourself with any SSH program that gives you command line, you’ll be able to search for any string or substring without knowing the exact location of the files that include such strings. The linux command ‘grep,’ is a powerful search tool that allows one to search for any string, substring among files, directories and subdirectories in your linux server, including the content of your drupal installation.

So, assuming that you downloaded and installed a SSH program such as ‘PuTTY’ on your local computer, now you can use PuTTY to connect to your server. Download PuTTY here. Before showing you a simple grep command, it is worth mentioning a few other basic linux commands that allow you to find your way around the SSH command line.
pwd: this command displays the current directory. Example:
# pwd
/home/ml
ls: displays the list of directories and files in the current directory
# ls
./ .cpanel/ mail/ tmp/ .trash/ .www
cd www: changes directory to ‘www’ directory
cd .. moves you up one directory
Now that you can find your way around the directories, let’s look at a grep example: In my case, since I know that the files and directories that make up my Drupal site are located in www/sites/all, I change my directory to this location before doing the search.
# cd www/sites/all # Now that I changed my directory to the desired location, I type in my grep command to search for the desired string: # grep –r ‘string to search for’ directory-name I explain how this works. grep: is of course the search command, -r: is optional yet very useful. It enforces a recursive search in that it enables you to search in all the nested subdirectories. ‘string to search for’: is pretty obvious. Just make sure to leave the single or double quotes around your string So let’s look at a more specific example: # grep -r ‘view all’ modules This command returns the following result:
modules/ml/mlpages.module:
$va = l(‘view all’, ‘news’, array());
modules/ml/mlmedia/mlbrowse.module
l(t(‘view all’), ‘articles’, null);

So this grep command searched for the string 'view all' in all the directories under modules and returned the files that include the string ‘view all.'
If you want to learn more about grep, you can take a look at the manual for grep on your server by typing in your SSH command line: # man grep It gives you a comprehensive list of options including more complex regular expression search.

Related Posts

Yet another simple Amazon S3 backup script for Drupal

Randall Knutson
Read more

Thoughts from BADCamp: A Shift From Drupal Services To Drupal Products

Brent Bice
Read more

Drupal Gardens will make Drupal easier to use

Frankie DeSoto
Read more

Drupal In a Box - Creating really custom functionality within Drupal 7

Ahmad Kharbat
Read more

Dallas Drupal 7 Release Party!

Kayla Wren
Read more

Learning Through Drupal Case Studies

Sean Keeley
Read more