A Beginner's Guide To Drush: The Drupal Shell
Drush is an awesome shell interface for managing Drupal right from your cloud server command line. It is a very useful tool as it helps you perform various admin tasks using just one or two commands in the terminal, replacing the need for many clicks and page refreshes in the UI.
This tutorial will go through some of the basic Drush commands and work with a standard Drupal installation to illustrate them. It assumes then that you already have Drush installed on your cloud server and you have a working copy of Drupal on it. If you don't know how to do this, please refer to an earlier tutorial explaining all the steps.
Note: All the following commands need to be performed from within the directory of a Drupal installation. Doesn't really matter where you are located, as long as you are somewhere within the root folder.
To begin, first navigate your Drupal folder directory:
Some General Commands
One of the most simple Drush commands is:
This will give you an overview of your Drupal site. Version, URI, database location, file paths, default theme etc. If you use this command and you do not see this information, it means you are in a wrong folder and Drush does not know which Drupal site you are referring to.
If you are a Drupal developer, you know that flushing the cache is something you very often do. With Drush, it's almost fun to do it:
This will ask you which kind of cache you want to delete for more granular control but if you run the following command, you will directly clear all the caches:
drush cc all
Let's install Views (I'm sure you know about this module). To do this, you first have to run the following command:
drush pm-download viewsor
drush dl views
Either one of these commands will download Views and place it in the appropriate sites/all/modules folder. The second command is a short version of the first one. You'll notice that many commands have short versions for an even faster experience. For instance, the long version of the drush status command is drush core-status, so don't be surprised if you see others using that one.
Now that Views is installed, enable it with the following command (the long version of which being pm-enable):
drush en views
You'll see that Drush immediately tells you that Views requires Ctools to be downloaded and enabled and asks you if it should perform those actions as well. If you select yes, it will download Ctools and will ask you once more to confirm whether you want Views and Ctools to be enabled. Say yes again and it's done. Now, how many clicks and URL copies have you saved with just this one command?
If you want to disable Views, run the following command (the long version of which being pm-disable):
drush dis views
And if you are looking to uninstall Views, you can use the following command:
drush pm-uninstall viewsThis will also remove the database tables belonging to that module. Note: the module names you should use with Drush are the machine names. If you have doubts about what is that name, you can easily retrieve it from the module project page URL: drupal.org/project/views. The last URI segment is what you are looking for. Additionally, you can specify multiple module names separated by one space if you want Drush to perform the actions on all of them. For instance:
drush pm-uninstall views ctools
Installing modules is therefore simple. But what about themes? Well it's exactly the same procedure. Themes are also drupal.org projects that you can dowload and enable using the same commands.
And if you want to have an overview of all the projects on the site, you can run the following command (the long version of which being pm-list):
This will return a list of all the projects on your site. If the list is too big to manage, you can pass various arguments for filtering based on what you need. For instance, if you want to see all the non-core projects, you can run this command:
drush pml --no-core
Do you deal with many user accounts on your site? Drush can help managing that with ease.
You can create a new user with the following command:
drush user-create username --mail="firstname.lastname@example.org" --password="password"
The terminal will then display some information about the newly created user. Want to delete this user? Use the following command:
drush user-cancel username
And it's that simple. Want to change your password? Or any other user's password for that matter? Run the following command:
drush user-password admin --password="new_pass"
Drush comes very handy when it comes to updating Drupal projects or Drupal core itself.
Let's first go through your options for updating. You have to update modules, themes and core, both the codebase and the database tables. As you know, in the UI this happens in two steps, first the codebase is downloaded and then you run the update.php script for the pending database table updates. In Drush, this is also separated into two commands, but you can also have them under one single command.
But first, you can check if there are any available updates using the following command:
drush up -n | grep available
This is the short version of the pm-update command, but that automatically answers "no" to any questions (so as to prevent actual updates being done) and that shows only terminal rows which contain available updates (so as not to have a cluttered screen).
If you want to update everything at once (not recommended), you can just run the
drush up command. But it's preferable to install updates one by one, or by group of modules so you're able to keep track if something breaks down in your site.
To upgrade only Views (both the codebase and the database), in case it appeared in the list of available updates, run the following command:
drush up views
If you want to update the codebase and database separately, start off by updating the codebase with the following command:
drush upc views
Then, to update the database tables, you can either run the
update.php script in the browser or use the following command:
drush updatedb views
That will run the database update script. And you are done. Updating Drupal core happens the same way. If you only want to update Drupal core, run the following command:
drush up drupal
As you can see, using Drush makes life easier for Drupal developers and site administrators alike. These are just a few of the more common and basic commands that you can use everyday. But there are many more advanced commands that are really cool and worth learning.