fromJune 2014
Article:

Watch Over My Shoulder

Learning Command Line Utilities Through Observation
0

Pottery on wheel

One of the best ways to learn useful tricks at the command line is to sit with someone and watch what they do. Due to the distributed nature of the Drupal community, we don't do nearly enough pair programming. Too often we work in isolation and then push our work on others when we finish. In this article I invite you to sit down beside me and watch over my shoulder as I explore Drupal 8 from the command line.

Navigating Drupal in the Bash Shell

The instructions in this article will work for OSX, and Linux systems, such as Ubuntu, but not Windows.
When reading command line instructions, there are two important characters we need to know about: $ and #. When applied to the beginning of a line, these refer to the prompt. We don't type these characters when issuing our command. $ signifies the command should be run as a regular user; # signifies the command is run as the administrative user (“root”).

As a themer, the first thing I want to explore is, of course, the themes. Let's begin by navigating to our Drupal folder. I start by opening up a terminal application. At the command line, I type cd, and then, using Finder, locate my Drupal folder. I then drag this folder onto the terminal application. It will automatically paste the path to the Drupal folder into my bash prompt. I press return, and bingo – we have navigated to the Drupal folder!

Let's take a peek inside the core folder of themes: we’ll navigate to the folder core/themes and then list (or ls) all files.

$ cd core/themes
$ ls

There should be four things listed. See them all?

Next, let's navigate into the Stark theme, and take a look at the new configuration file used for themes using the file viewer, less (which is different from the CSS preprocessor, Less).

$ cd stark
$ less stark.info.yml

My screen has just filled up with a configuration file. I can wander around this file using the up and down arrows. When finished, I’ll exit the screen by pressing q.

To go back up to the root directory of our Drupal folder, we'll want to change directories again, but we need to climb out three levels (stark, themes, core):

$ cd ../../../

[Note: The remainder of this article will include the commands, but not the step-by-step navigation instructions.]

Counting Things You Can Find

After taking a peek at the files with less, I want to get some quick stats about the file, so I use the word count command (wc) and the -l parameter to count the lines instead of words.

$ wc -l core/themes/stark/css/layout.css

How many lines in this file did we find? At the time this article was written there were 118 lines in Drupal 8's stark layout file. The increase from Drupal 7 was mostly due to the addition of media query-related styles.

Next I want to compare the number of template files between Drupal 7 and Drupal 8. For this I'll use the command find. This is similar to Spotlight in OSX, but I can have it do more calculations for me.

$ find . -name '*.html.twig'

The find command looks in the current directory and below (the period signifies current directory), for files with the name ending in '.html.twig'. The wildcard character (*) allows us to search for any filename with a matching extension.

And there are a lot listed! Let's add this command to our previous command, wc and add up all those lines. We can combine commands using a pipe (“|” character).

$ find . -name '*.html.twig' | wc –l

How many Twig files did we find? When I wrote this article, I was able to find 119 Twig files, and in Drupal 7 I was able to find 55 tpl.php files. Why so many more? Because every single HTML snippet in Drupal is now only available as a Twig template...and we know how to find all of them. How delicious is this?!

Searching Inside Files

The next command that I use all the time when playing sleuth at the command line is grep (Globally search with a Regular Expression and Print...I had to look it up). You can spend a lifetime learning all the options for regular expressions. I typically start with the following parameters:

  • -n display the line number where the pattern was found.
  • -r search recursively through directories.
  • -i ignore the case in the pattern, and the text you're searching.

Let's say I wanted to find all the code which references a Twig file.

$ grep -nri html.twig *

Whoa! That was a lot of references! Let's slow it down by combining grep with the pager, less. This will allow us to scroll through the matches using the up and down arrows.

$ grep -nri html.twig * | less

Let’s pause for a second while my computer compiles the list. The screen will fill up. Now I’ll use the space bar (or the up and down arrows) to paginate through the results. When I have finished looking at the list, I’ll press the letter q to return to the shell prompt.

Wrapping Up

To exit the command line, I simply close the Terminal application, or type exit and press return.

Take every opportunity you can to watch what people do at the command line. Drupal meetups, camps, conferences, and even Google Hangouts are great for this type of sharing. If you're too nervous to ask, you should start by watching over Addison Berry's shoulder with her free videos on how to use the command line.

Resources

For the longest time I kept a text file in my home directory called HELP.txt. It listed all the commands that I'd discovered in online tutorials and found useful. It was my lifeline. You should make your own cheat sheet. Here are a few resources to get you started:

Image: ©iStockphoto.com/sun_apple