Searching Drupal code

Often I find myself in the position of having to find something in the Drupal codebase. It's easy to do; from the command line:

cd drupal5
grep -rn contact .

This recursively searches the code for Drupal 5 and returns all occurrences of the word "contact", along with the filename and line number. Suppose I'm searching for the places where the contact database table is updated in Drupal. I can pipe the results into another grep:

grep -rn contact . | grep UPDATE
./modules/contact/contact.module:227: db_query('UPDATE {contact} SET selected = 0');
./modules/contact/contact.module:242: db_query("UPDATE {contact} SET category = '%s', recipients = '%s', reply = '%s', weight = %d, selected = %d WHERE cid = %d", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected'], $form_values['cid']);

If you find yourself doing this frequently, it's helpful to make a shell script. I use OS X, and in my user directory I have created a directory called bin in which my scripts live. Of course, I've had to modify my /Users/john/.profile file to include that path when OS X is searching for executables by adding the following line:

PATH=$PATH:/Users/john/bin

I created a file at /Users/john/bin/f that contains the following line of code:

#!/bin/bash
grep -rn $1 .

Now instead of typing grep -rn contact . I can simply type

f contact

Quick 'n' easy. For more shortcuts, see Steven's handy tips.

[ Submitted by John on Tue, 2007-03-13 08:36. | | ]

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question tests your humanity.