A Quick Guide to the Terminal and Command Line Prompt for WordPress
The terminal is one of those things that seems scary for the uninitiated. It’s all text based and spits out responses you don’t understand and feels like you’re hacking the system.
The reality is, it is extremely simple and straightforward and can shave hours off each of your work days, I kid you not.
In this article, I’ll introduce you to the terminal, show you how easy it is to use, and teach you the basics you need to know to work with great products that can automate everything from tidying your CSS to migrating your WordPress site with a single command.
What is the Terminal / Command Line Prompt?
The terminal is an interface in which you can type and execute text-based commands. Command prompt is the name of the terminal in Windows, otherwise, it is pretty much the same.
OK, so that was a little bit of a white lie.
The operating principles of command prompt (let’s use cmd from now on) and the terminal are the same, but various commands might not be. The same thing happens with graphical user interfaces. In Windows, you would close windows in the top right corner, while in OSX you close programs in the top left. The principle is the same, just the element is simply found elsewhere.
The goal of this article is to show you how commands work with some hands-on examples. Armed with this knowledge you’ll be able to use any command line tool!
From now on, I’ll refer to both command prompt and the terminal as “terminal” unless I am highlighting specific differences between the two.
A Note for Windows Users
If you’re using Windows, I recommend Installing Cygwin. Most tutorials that contain command line references work with an environment like Ubuntu or OSX where some command names are different. Cygwin adds many of these commands to Windows giving you a uniform experience.
This tutorial will also mostly use Unix-style commands so following along will be easier with Cygwin. If you don’t want to install it and get a “command not found” error when trying an example simply search for the Windows alternative in Google.
What Can the Terminal Do?
No doubt you are used to basic operations like creating a folder by right-clicking and selecting “New Folder,” then typing a name.
In the terminal, you would issue the following command:
mkdir foldername. And that’s it.
This is a lot faster and many commands that do the same things as your GUI can take a bunch of parameters for more flexibility, more on that later.
Your operating system already a huge list of commands you can use for everything from creating files to optimizing images. New commands can be installed that allow you to do all sorts of cool things.
Here are just some things I do every day:
- Start and manage local WordPress development environments
- Automatically convert Sass to CSS
- Concatenate and minify multiple JS/stylesheet files
- Resize multiple images at once in seconds
- Gain access to remote computers – my live sites usually – and issue commands there
- Install multiple WordPress plugins with a single command
- Search and replace within my WordPress database
- Use version control to manage my code
Upon opening the terminal, you’ll be faced with a screen and a cursor. Here are two examples, one from OSX and one from Windows.
Current Working Directory
The first thing to be aware of is that you are always in a folder in the terminal. This is a little bit more apparent in Command Prompt – it is shown in the beginning of the line. On OSX, it is shown at the end of the line – in the screenshot it says
~ has special meaning, which is your home directory. On your local machine, this is probably
Why is this important? Many issues commands will have an effect on the directory they are in. For example, the
mkdir command creates a directory. By default, the new directory is placed in your current directory:
This would create a directory named
newdirectory in your current directory.
Absolute and Relative Paths
If you are required to type a directory name – like in the
mkdir example – you can use relative or absolute paths. By typing a simple name I used a relative path, telling the command that I want to create “newdirectory” within the current directory.
If I use an absolute path I can create a directory anywhere, regardless of my current working directory.
Let’s assume we are in our “pictures” folder but we want to quickly create a backup folder in our home folder. We could do it like so:
Sometimes you’ll see a path that begins with ./. This is also a relative path ./ indicated the current directory.
The Anatomy of a Command
Commands generally have four parts: the command itself, operands, flags and parameters. The command itself is the first string you type.
The command itself is the first string you type. Operands follow the command (and any flags if given) and usually provide information about what you want to perform the command on.
The command above will copy the file referenced in the first operand to the location specified with the second operand. Some commands take a single operand, some take two or more, search Google for the command you are using to find documentation for it.
Flags modify the behavior of the command. A quick example is the
-i flag applied to the copy command. It indicates that the command should ask for confirmation if the copy operation would result in a file being overwritten.
Flags always consist of just one letter, but take care – they are case sensitive. Multiple flags can be added if available in one of two forms. To copy the file with a confirmation and more information displayed we would use the
-v flags. You could write them in any of the two ways below:
Parameters are unique to each individual command and you’ll mostly see them on advanced commands that perform more elaborate functions.
This command is from a package named WP-CLI which automates common WordPress tasks. It fills the config file with information about our database. Technically
wp is the command and
core config are two operands. However, WP-CLI uses operands to group various commands. There is a
core install and various other commands.
Parameters are at the end, you can input information you want to send along with the command using these.
That’s Really All There is to Know!
There is quite a bit more you can do in the terminal but in most cases you’ll end up issuing commands. If you want to put yourself to the test I recommend reading our Using Gulp To Speed Up WordPress Development. It contains plenty of commands to issue and the end result is well-worth the effort.
Look out for more posts on this topic soon. In other tutorials, we’ll look at editing files within the terminal, creating bash scripts that allow you to run multiple commands and once and using SSH to run commands on remote machines.
Do you have some experience with the terminal? What’s your favorite way of saving time with it? Share your thoughts in the comments below.
WIN a Share of $5K
Subscribe to our blog this #hostingmonth for a chance to win one of 5 prizes of $1,000 WPMU Dev credit! Learn More.