Code Karate

Drush: Getting started with Drush in Drupal

Tags:
Image removed.Drush... what a wonderful web you weave. My guess is you have probably heard about this mysterious thing known as "Drush" and are looking to find out:
  1. What exactly is Drush?
  2. Is Drush something that will be useful for me?
The first one is an easy one. Drush is short for Drupal Shell and is a command line tool that makes interacting with your Drupal website a breeze. The second question is also easy, but first I have a question for you. Do you use Drupal? If the answer is yes... then Drush will probably be useful for you.

Drush: An Introduction

You might be thinking to yourself about now... "but I don't spend hours of my day behind a command prompt". In fact, you might be a little nervous about even having to open a command line window. Don't worry... we will try to take it slow. I'm convinced that if you do even a modest amount of Drupal development, spending just one hour learning how to use the command line and Drush commands will save you countless hours of time. In the next few sections, I am going to show you how Drush can be used to automate and simplify a lot of common Drupal administrative tasks.

Getting used to the command line

If the command line scares you... don't worry, you are not alone. At one point many years ago I was thinking the same thing... "why can't there just be a nice GUI for this?" If you spend a little time learning though, you will quickly find out that it's not as bad as it seems and it can save you a lot of time. If you are not familiar at all with the command line, below you will find a quick tutorial that will help you get started. If you are already familiar with using the command line, you can skip this section. Special Note for Windows Users: Sometimes it's tough to be a Windows developer... This is also true if you want the best experience using Drush. You "technically" don't need to install any additional programs to get started, but I would recommend installing Msysgit and getting it to work with Drush. This tool will provide a much better and more powerful command line experience. Msysgit will allow you to use a Bash shell (just like your Linux and Mac friends).

Opening up the terminal

It's now time to open up the terminal. If you are using Windows, open up your shiny new Msysgit program. If you are using Mac OSX, open up your Finder, select Applications, then Utilities, and find Terminal. Oh... If you are using a flavor of Linux (like I am as I write this)... wait who am I kidding. You already know how to use the Terminal... go ahead and skip this section!

Some basic commands

Don't be intimidated by the contents of the terminal window (or the lack thereof). Simply get started by typing in a simple command. Go ahead and type: ls Then hit Enter. You should now see a list of the files in the current directory. This is no different than browsing your files on your file system. Now try: pwd This command stands for "print working directory" and will print out in the terminal your current location. The next step is navigating, you can do this using the cd command. Go ahead and run the following command: cd .. This tells your terminal to "change directory". The .. tells the terminal to go up one folder. You can use the pwd command to see your current location. If you want to go back into the folder you just navigated out of, try this: cd [folder-name] Replace [folder-name] with the name of the folder you want to navigate into. Note: Once you start typing the folder name, you can hit tab to auto-complete the name for you. You will use the tab key often when you are using the Terminal to save you time. Those are the basics, but there is a lot more to learn. Here are some other commands you might want to try out. CommandWhat it does cdThis command on its own will return you to your home folder (where you started when you opened up the Terminal) touch [file-name]Create a new empty file called [file-name]. Example: touch test.txt mkdir [folder-name]Create a new folder. Example: mkdir myfolder cp [file-name] [new-file]Create a copy of [file-name] and call it [new-file]. Example: cp test.txt backup.txt mv [file] [new-location]Moves a file to a new location. Example: mv backup.txt ../ This will move backup.txt up one folder level. rm [file-name]Deletes the file [file-name]. Example: rm test.txt

Drush: Getting things Installed

The first step is to get Drush installed. Installing Drush is not an overly complicated process, but because it varies depending on the operating system your website runs on, and if you are running the site locally or on a remote server, I won’t go through all of the installation possibilities here. There is a page on Drupal.org that provides information depending on the type of platform you are installing Drush on. Typically you need to install Drush on the server that is hosting your Drupal website (you can get around this with something called Drush aliases, however, we won’t be covering that topic). For the installation instructions, go to https://drupal.org/node/1791676.

Drush: Downloading and Installing Modules

We are now ready to begin installing some Drupal modules and themes on our new Drupal website. I am going to start with one simple example. Downloading and installing the module_filter module. Drush CommandWhat does it do? drush dl [project-name]Downloads a Drupal module or theme. The name can be grabbed from the drupal.org project name. For example in https://drupal.org/project/module_filter the project name is module_filter drush en [module-name]Installs a Drupal module. Keep in mind when you download a Drupal module, it may contain multiple modules. You can get the correct module name from the output of the drush dl command. The first step is to download the module_filter module using the drush dl command: Image removed. This command will tell you where the module was downloaded (in this case sites/all/modules/module_filter) and if there are multiple modules that can be enabled/installed. Now we install/enable the module using the drush en command. Image removed. You can also use the drush en command to download and enable multiple modules at a time. Just put a space between each module name when running the command. Image removed.Image removed.

Drush: Disabling and uninstalling Modules

There may come a time where you need to disable a Drupal module. If this is the case, you can do so with the drush dis command. You can then uninstall a disabled module with the drush pm-uninstall command. Drush CommandWhat does it do? drush dis [module-name]Disables a Drupal module. drush pm-uninstall [module-name]Uninstalls a Drupal module. If you were launching your website and you no longer needed the views_ui module, you can disable the module using drush dis views_ui. Image removed. A Drupal module can be disabled, but that does not necessarily mean it is uninstalled. A Drupal module may create additional database tables in your Drupal database or add additional variables to your Drupal variables database table. If you disable the module, those database tables and variables will not be deleted. This means you are able to enable the module later without losing any of your module settings. The drush pm-uninstall command will completely uninstall your module removing any database tables and variables that the module has stored. Here we run the drush pm-uninstall views_ui command to uninstall the Views UI module that we previously disabled. Image removed.

Drush: Running cron and clearing cache

Now you will learn how to use drush to run cron and clear the cache on your Drupal site. Drush CommandWhat does it do? drush cronRuns Drupal cron process. drush ccClear the Drupal cache. drush cc allClear all of the available Drupal caches. You can manually run the Drupal cron process using Drush by running the drush cron command. Image removed. You can clear the Drupal cache using the drush cc command. After running this command, Drush will provide you an option to select which cache you want to clear. In this example we select 1 to clear all of the available Drupal caches. Image removed. You can also specify which Drupal cache to clear directly in the drush cc command. In this example we run drush cc all too clear all of the Drupal caches without the extra prompt to choose the Drupal cache to clear. Image removed.

Additional Drush Commands

Here are some additional Drush commands you might want to try out: Drush CommandWhat does it do? drush archive-backupBacks up the code, files, and database of a Drupal website. drush archive-restore [backup-path]Restores a Drupal website to a previously backed up state. drush upUpdates the modules, themes, and core Drupal code on your Drupal website. This command will also perform any necessary database updates.

Drush Wrap-up

There is a lot more you can do with Drush. You can always get a list of commands by running drush help. You can also use the helpful website drushcommands.com. If you want a helpful Drush cheatsheet, enter your email in the box at the bottom of this page and I will send you mine! I will also send you some additional Drush commands and getting started ideas.