Drush: the Drupal shell utility

Drush while not strictly a module, is listed on drupal.org as a module for lack of a better classification. Drush is a set of shell scripts that can be used to better manage drupal installations because it actually does a limited bootstrap of the drupal environment, connecting to the database that it finds available in the nearest settings.php file. If you have a multisite install it will use the settings file in default unless you are in a subdirectory of one of the other sites, then it will use that site's specific settings and connect to that database on startup and each subsequent call.

Installing drush can be done in many different ways
For windows systems there’s an installer here.
If you have pear installed you can simply issue these commands to istall it

$ pear channel-discover pear.drush.org
$ pear install drush/drush

For Macs and linux what I've found (and many others agree) is that it's best to install it in /usr/local/lib and symlink to it in /usr/local/bin as that’s usually in every users path on linux systems and can be on Macs.

$ cd /usr/local/lib
$ wget http://ftp.drupal.org/files/projects/drush-All-versions-5.x-dev.tar.gz
$ tar -xzf drush-All-versions-5.x-dev.tar.gz
$ cd ../bin
$ sudo ln -sfn /usr/local/lib/drush/drush

now you can navigate to your webroot and issue the command

$ drush status

if everything is working properly you'll see something like

 Drupal version         :  7.15                        
 Site URI               :  http://default                       
 Database driver        :  mysql                                
 Database hostname      :  localhost                            
 Database username      :  drupal                               
 Database name          :  drupal7                              
 Database               :  Connected                            
 Drupal bootstrap       :  Successful                           
 Drupal user            :  Anonymous                            
 Default theme          :  bartik                               
 Administration theme   :  seven                                
 PHP configuration      :  /etc/php5/cli/php.ini          
 Drush version          :  5.7                        
 Drush configuration    :                                       
 Drush alias files      :  /home/wes/.drush/aliases.drushrc.php 
 Drupal root            :  /var/www/d7                          
 Site path              :  sites/default                        
 File directory path    :  sites/default/files

If not then check your path

$ echo $PATH

to make sure that /usr/local/lib is in it, if not you'll have to add it in your .bashrc or .profile in you home directory, depending on your particular OS

drush dl  or 

will download the appropriate version of the module according to the version of drupal it finds, so in a drupal 7 site it will download the 7 version of the module you ask for. If there are multiples available it will ask you which one you prefer. Drush will also know which is the proper directory to put the module in, so if best practices are followed and there is a contrib directory in sites/all/modules, it will place it there (sites/all/modules/contrib). It also knows to put themes in the sites/all/themes directory.
I think I should mention here that updating modules is quite direct, backup the database, then

$ drush dl 

multiple modules can be downloaded by separating them with a space
if updating a module then

$ drush updb

(run updates to the database)
So now you can do things like enable the module or theme you just

$ drush pm-enable 

and lots more.
Another useful utility that can be used with drush is registry_rebuild. go to your .drush directory and drush dl registry_rebuild and you can issue the command

$ drush rr

this can be done when you get errors like

PHP Fatal error:  Class 'EntityAPIControllerExportable' not found in ...sites/all/modules/rules/includes/rules.core.inc on line 11

or after reorganizing your modules

You'll find lots of time savings using drush Vs. the web interface. You can find all the drush commands here

Topics: