When we think about “using a computer,” we usually think in terms of graphical user interfaces, or GUIs: interfaces (like Microsoft Word, the Mac OS, or the WordPress admin interface) that are specially and attractively designed, that have programs and windows and tabs that visibly open and close, and that let you click things and drag things and drop things and hover things.
For some development tasks, the command line is still both significantly faster and more powerful than any GUI tool.
It wasn’t always this way. The original manner of interacting with a computer is through the terminal, also called the command line: a stripped-down, usually mouse-free interface for entering programming commands directly.
Well, the terminal’s not just a piece of history. For some kinds of tasks, the command line is still both significantly faster to use and more powerful than any GUI tool that exists. This is especially true for a WordPress developer, thanks to one of the coolest pieces of programming ever to hit the WordPress community: the WP-CLI project, which hugely simplifies all kinds of WordPress tasks—everything from “delete the 100 oldest comments” to “update all plugins” to “export the entire database.”
This course is a gentle and thorough introduction to the magical world of the terminal, and to the hypermagical world of WP-CLI. Let’s dive in!
1. What is the Command Line? CLIs from First Principles
Key Point: Defining the Command Line
The command line, or the terminal, is the original way, and still a very useful way, of interacting with a computer.
The command line, command line interface (CLI), or terminal is the original way, and still a very useful way, of interacting with a computer. It does not use a graphical user interface (GUI), and generally does not use a mouse. Terminal commands’ other differences from GUI applications include:
- Terminal commands are simple programs running once.
- Unlike GUI programs which are always waiting for you, most terminal applications run quickly and quit.
- Terminal commands are often dense with meaning and short, because people wanted to type as little as they could get away with.
The fundamental advantage of the terminal/command line is that a competent programmer can often work much more quickly and precisely than if she were clicking and dragging GUI objects. The terminal is also generally the primary tool that gives you the most direct access into the inner workings of a given machine.
Key Point: Windows Users Will Need to Work Harder for a Unix-y Shell
Microsoft Window’s shell is the “odd one out” from the BASH terminal that most other operating systems (all of which are based on or similar to Unix) use. If you use Windows, your best route is to install a Unix-y terminal, but results doing so may vary.
2. Quick Guide: Navigating the Filesystem from the Command Line
Key Point: The Three Essential Commands for Navigating a Unix Command Line
Navigating a filesystem in the Unix terminal makes heavy use of three fundamental commands:
pwd
, which finds your “present working directory”ls
, which displays the files in your current directorycd
, which changes your current directory
3. Quick Guide: The Basics of Manipulating Files from the Command Line
Key Point: File and Folder Manipulation from the Terminal
This Quick Guide introduces a number of Unix terminal commands for creating and modifying files and folders:
mkdir foldername
: make a new directory.touch file.ext
: create a new file.cp original.txt copy.txt
: copy a file from an old location to a new one.mv original-name new-name
rename a file or folder.rm file.ext
: delete a file.
4. 10-ish Unix CLI File Commands Every WordPress Developer Should Know
10-ish Unix CLI File Commands Every WordPress Developer Should Know
Key Point: Breadth of Command-Line Options
This article reviews in detail some of the initially introduced terminal commands, such as mkdir
, touch
, cp
, and rm
.
It also introduces the possibility of editing the contents of files using a command-line-compatible text editor such as nano
. Finally, it introduces command-line version control using git
or svn
.
5. Mastering the Unix Terminal: Working With Permissions, Networking, and Other Key Concepts
Mastering the Unix Terminal: Working With Permissions, Networking, and Other Key Concepts
Key Point: Working with Permissions
User file permissions control which of a machine’s users can do particular actions to particular files and folders.
User file permissions control which of a machine’s users can do particular actions to particular files and folders. Three useful commands for navigating permissions from the Unix terminal include:
sudo
: temporarily makes the current user the most powerful “root” user, generally after correctly inputting a “sudo password.”chmod
: changes file and folder permissions using a three-number system that varies from 000 (most restrictive) to 777 (most permissive).chown
changes file and folder ownership between the machine’s users.
Key Point: Network Utilities For the CLI
Command-line network utilities relate to connectivity between machines.
This section quickly reviews command-line network utilities: functions that relate to connectivity between machines.
ssh
: securely connect to other servers. (Requires first generating SSH keys, a separate process.)rsync
: remotely transfer files to and from another server.curl
andwget
: download content from the internet without SSH authorization.
6. Quick Guide: How and Why to Make a Bash Alias
Key Point: BASH Aliases are Keyboard Shortcuts for the Command Line
A BASH alias creates keyboard shortcuts for commonly typed commands, which can be as short as one letter. This video guide covers the multipart process to writing a BASH alias.
7. The What, How, and Why of WP-CLI: WordPress in Your Terminal
Key Point: What WP-CLI Is
WP-CLI is a program that allows you to use WordPress on the command line.
WP-CLI stands for “WordPress Command Line Interface.” It is a program that allows you to use WordPress on the command line. The core advantages of WP-CLI include:
- Speed: a keyboard-only interface is faster for experienced users than a mouse-and-keyboard graphical interface can be.
- Automation: GUI-less commands are much easier for machines to execute. WP-CLI can be used to, for example, very quickly run a single command on hundreds of WordPress installs.
- Specific powers: WP-CLI has a number of useful tricks that don’t have GUI counterparts.
Key Point: WP-CLI Tricks
The following are some early uses of WP-CLI that show some of its usefulness:
wp core update
: Quickly updates the WordPress version on a specific install.wp core download --force
: Re-downloads the core WordPress files, even if the WordPress install is up-to-date.wp plugin install plugin-slug
: Quickly install a plugin.wp plugin activate plugin-slug
: Quickly activate a plugin.wp plugin update --all
: Update all installed plugins.wp theme update --all
: Update all installed themes.wp db export []
: Export the WordPress database, optionally specifying a filename.wp search-replace
: Find-and-replace in the WordPress database.wp media regenerate
: Regenerate all image thumbnails.
8. Quick Guide: Installing WP-CLI
Key Point: WP-CLI Installation Steps
This video quick guide walks through getting the WP-CLI software onto your machine. The steps are as follows:
- Open BASH.
- Paste in the command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
and hit enter. This will download the file from the URL onto your computer and into your current directory. - Make sure the above command worked via a run of `php wp-cli.phar –info`. If you get output, keep going. If not, redo step 2.
- Make the
phar
executable via `chmod +x wp-cli.phar`. - Rename and move the executable into your PATH, so that you can type
wp
to run the command, withsudo mv wp-cli.phar /usr/local/bin/wp.
- Confirm that everything worked by running
wp --info
.
9. Anatomy of a WP-CLI Command: wp database import
Key Point: WP-CLI Commands Can Hook in the Methods of Entire PHP Classes
Registering WP-CLI commands uses PHP syntax that is quite similar to WordPress’s existing hooks system. However, the WP-CLI registration process has one major innovation: it can register in all public methods of a PHP class at once.
The article gives this code example:
WP_CLI::add_command( 'db', 'DB_Command' );
This hooks all the public methods of the (previously registered) DB_Command
PHP class into WP-CLI. So the DB_Command
class’s public import()
method can now be run from WP-CLI with wp db import
, and similar things are possible for all the class’s public methods.
Key Point: WP-CLI Commands Are Specially Documented
The article describes a way of commenting the PHP class’s functions that is especially helpful to command-line users. The documentation for the DB_Command
class’s import()
function begins as follows:
/**
* Import a database from a file or from STDIN.
*
* Runs SQL queries using `DB_HOST`, `DB_NAME`, `DB_USER` and
* `DB_PASSWORD` database credentials specified in wp-config.php. This
* does not create database by itself and only performs whatever tasks are
* defined in the SQL.
This text is actually what will show up in the terminal if the user types wp db --help
.
Wrapping Up
This course should offer you an accessible introduction to one of a serious developer’s most powerful tools—the speed, simplicity, and directness of the command line—and to the WordPress superpowers of WP-CLI specifically. Thanks for reading, and we’d love to hear thoughts or questions in the comments below!
Great command line information. I use the command line for many of the commands you mention in the article. My question is when you do WP-CLI specific commands like ‘wp plugin update –all’ do you have to be in a certain directory? What if you are on a shared server with multiple domains with WordPress installed? Do you have to be in ‘plugins’ directory to do the ‘wp plugin activate plugin-slug’ command?
Tom, WP-CLI operates on the directory that you are in. If you keep your WP sites in /var/www/html and you have two sites /wp1 and /wp2, cd to the wp1 directory and all WP-CLI commands will apply to that site. Then cd to the wp2 directory and all WP-CLI commands will apply to the second site. No need to cd to /wp-content/themes or /plugins to run WP-CLI commands on themes or plugins.
Fred and David:
You guys produce some really great content. I’m moderately competent on the *nix command line, but I’m still finding my way around WP-CLI, though I’m loving it.
My question is probably beyond the scope of these articles, but I thought I’d ask if you could point me in the right direction. With WP-CLI, is it possible to perform actions on a subset of posts. Just as an example, can I use WP-CLI commands to do something like: get all posts where fred is the author and change the author to david, or get all posts where the post status is pending and the post date is in May and set the post status to published. Thanks for any help you may be able to offer.