Drupal Command Line Essentials: Most Useful Commands & Tools to Use as a Site Builder for Speeding Up Your Workflow

Copy your live Drupal site to a test site, update a module, apply a patch, run cron, check the Drupal version, change passwords... What if, as a Drupal site builder, you could save up to half the time you spend on mundane tasks? All it would take is for you to switch from the familiar administrative UI to the command line...

I know what you must be thinking:

  • "It's way too scary. All those never-ending command-line strings..."
  • "Why bother anyway if I already know how to complete all these tasks?"

And so, inspired by Tara King's BADCamp session 2018 — Command Line Basics for Drupal Site Builders — I decided to bring my own share to the effort of busting the discouraging myths around the very idea of working with Drupal from the command line.

To prove to you that:

  • once you strip the CLI off its intimidating aura and get to understand a command's basic syntax
  • … the CLI turns into a true (life) time-saver 

One turbocharging your workflow as Drupal developer...

Now, here's the very basics of using CLI in Drupal:

 

1. But what Is the Drupal Command Line More Precisely?

If we broke its definition down to its essential we'd get:

“A place where you type commands to the computer” (David Baumgold)

Word of caution: the “myth” that you can easily remove things when using the CLI is no myth at all. Do be cautious, for most of CLI do not have an undo function!

 

2. Why Bother Learning It? Why Should You Use the Command Line? 

In other words:

Why go through all this trouble when you already have the all too familiar Drupal administrative UI for performing all those tasks?

Because:

 

  • it's significantly faster: running a command in the CLI is “n” times quicker than accessing your admin panel, scanning through menus, tabs, folders, and files
  • it enables you to do more, to boost your productivity as a site builder
  • you get to turbocharge your workflow with Drupal specific command line tools, like Drush

     

In short: mastering the Drupal command line turns you into a (way) faster and implicitly more productive site builder.

 

3. The Syntax of a Command: Breaking It Down to its Components

Let's say you'd enter this command into your command line:

[projects]$  rm -f  foo.txt

Source: Learnenough.com.

 

What's it made of? What's the anatomy of a command?

If we are to deconstruct it to its key components we'd get:

 

a. The Prompt: [projects]$

The prompt can include: 

the name of your machine/the current director/the user.

It tells what folder you're in at the time you enter that command.

 

b. The Command: rm 

In this case here it's "rm", which stands for “removing”.

 

c. The Argument: foo.txt

It points out to the element in your system that the command should be applied on. 

 

d. The Option: -f

The element in the string indicating how precisely the command (in this case “to remove a text file”) should be executed.

To sum up a command's basic structure:

prompt + command + option (optional) + argument 

 

4. Use CLI to Navigate Your Files: Main Conventions & Useful Commands

One of the Drupal command line's “seduction assets” is:

Empowering site builders to navigate their folders and files much quicker.

And here are some of the main conventions to use in your command line for swiftly running through your directory trees:

  • .” to scan though your current directory's tree
  • . .” to navigate to the upper directory
  • /” to navigate to the home level of your system, to its root
  • ~” to get to your own specific “root” in the context of a multi-user computer context 
  • ls” to list all the files included in that particular directory
  • ls al” to list them all (-a) in long format (-l)

“But what are the most useful commands that I should learn to quickly navigate my files?” 

Here's a top 5:

 

  • cd” to change your current working directory 
  • cp” to copy files (and directories)
  • mkdir” to make a new directory
  • rm” to remove files and directories
  • pwd” to print the full pathname of your working directory

     

5. The Secure Shell or How to Run CL Commands... Remotely

What are the chances that you host your Drupal website right on your personal computer? Close to none, right?

Normally you'd use one of the available FTP/SFTP options to access the remote computer/server hosting your site: CuteFTP, WinSCP, FileZilla...

Now, in order to use your Drupal command line on your remote server, you should use: SSH (Secure Shell).

To access it, you'll be asked to provide an SSH key (ssh-keygen) for authentication (see GitHub):

Simply run the suggested command in your terminal window to generate that required ssh-keygen. 

 

6. What Is Version Control?

That's a term that you must have already come across. 

A version control system (e.g. GIT) records every single change you make to a file and empowers you to roll back to any of the stored file versions.

That said, you can easily roll back to any one of your older project versions and compare it to the current one.

Note: it's the guarantee that nothing's irreversible; if you “blow everything up”, trying to... apply a patch, let's say, you can always roll back to the “before-disaster” version of your web project.

 

7. Drush: The Ultimate Time-Saving Tool for Drupal Site Builders

What's your favorite text editor? Emacs, Vim or maybe Nano?

Now, time to choose your command line tool, as well. And here I highly recommend you to go with Drush.

Here's why:

  • it's a command shell created specifically for Drupal 
  • it dramatically speeds up your Drupal installing, developing, and maintaining workflows

“But there are so many Drush commands to learn!”

There's no need to “stuff” a huge pile of new commands into your memory all at once. Learn them gradually.

In the end, you'll see that the benefits in using this Drupal command line tool are way higher than the effort to learn them:

  • for every one of the mundane tasks in your workflow as a Drupal site builder you'll just type in a command; no need to keep scanning through the Drupal admin interface
  • installing Drupal, updating core and contributed modules, developing new modules, clearing cache, importing/exporting configurations...
  • ... all these regular time-consuming tasks turn into quick to type Drush commands

Note: pair Drush with Git in your workflow; this way, you can always roll back to the previous project version each time a Drush command breaks something on your website.

The END!

Once you've ended up mastering Drush... level up! Look for other command line tools to boost your development workflow with: Composer, Drupal Console, MySQL etc.