Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

UNIX Shell Skills

Note: This article covers an advanced topic.

If you have recently moved to WordPress from a hosted blogging solution like Blogger or TypePad, you may be unfamiliar with the tasks of managing a webspace as well as a blog. WordPress is based on PHP; many hosting services use servers running Linux and Apache; many web hosts offer not only web-based control interfaces to their customers but also shell access or command-line interfaces. Many WordPress users may not be familiar with using the shell to manage their file systems. This article explains the more commonly-used UNIX commands and demonstrates how a routine administrative task, namely a minor WordPress upgrade, is carried out in the shell.

wp-cli

wp-cli is "a command line interface for WordPress". It's a great tool that can save you a lot of time if you know more about using shells.

The bash shell

The shell offered by your web host is likely to be bash, which stands for "Bourne Again Shell" after Stephen Bourne who wrote the original modern Unix shell. bash is distributed by the Free Software Foundation. There are a number of different Unix shells. The commands briefly described below apply to almost all of them.

ssh, the secure shell

The Secure Shell (ssh) is a complete set of tools for remote administration of your website. It includes SFTP (Secure File Transfer Protocol) for the uploading of files to your host. The main benefit of using ssh to interface with a remote server is increased security. With other server access tools, your login password may be transferred over the Internet in plain text, which may be intercepted.

To use ssh under Windows, use PuTTY or Cygwin, a UNIX-like environment for Windows. The FileZilla ftp client also supports sftp via ssh-2.

To use ssh on a Mac running OS X, use the Terminal-based SSH program supplied with Mac OS X or download Fugu.

On Linux or UNIX-based operating systems, you can use the text-based SSH and SFTP clients from terminals such as Konsole, Gnome Terminal or xterm. Graphical FTP clients such as KBear and gFTP also support sftp.

Your hosting company will provide the username and host details (the "shell account") necessary to use these. They may be different from the ones you use with your control panel. To use, at the Terminal prompt type
ssh username@host
or
sftp username@host
If the SSH server you're connecting to is running on a non standard port, use the -p argument, followed by the port number. So, for example, if the remote server SSH was running on port 1000 you would type
 ssh username@host -p 1000 
To use sftp to connect to a remote server on a non-standard port use the -oPort= argument. So, for example, to connect to a remote server using sftp on port 1000 you would type
sftp -oPort=1000 user@host

Terminology

Before we get started with some UNIX commands, let's review some terminology.

Directories? Folders?
A Directory is the same thing as a folder. Directories can contain subdirectories.
  • A dot (period or full stop) refers to the current directory; two dots represent the directory above the current directory.
  • The tilde character (~) refers to your home directory.
  • The tilde followed by a user's name (as in ~username) refers to that user's home directory.
Both the dot symbols and the tilde can be treated as directory names. For example, ../config.php refers to the file config.php in the directory above the current one. The directory ./functions.php refers to functions.php in the current directory. And ~/public_html or ~/htdocs are (usually) the names of your main web directory, which is likely to be a subdirectory of your home directory. The domain URI shown in your web browser's address bar refers to the main web directory, not to the site owner's home directory.
The forward slash (/)
The forward slash fulfils the same role it does in a website URI: it shows the item after to be in a subdirectory or the directory before it. A forward slash on its own refers to the root directory, which is at the top of the file system.
Directory Name Character Rules and Spaces
A file or directory name can contain any standard character except a forward slash — including white spaces. However, when entering shell commands, you separate commands, parameters, and files with spaces. If you have a space in a file or directory name, you need to use a backslash in front of it or put quotation marks round the name. A directory called My Pictures would be entered as
"My Pictures"
or as
My\ Pictures


The * and ? Symbols: The * and ? symbols are called wildcards and can be used in any command which deals with files. The * represents any number of characters. The ? represents just one. Note that Linux filenames are case-sensitive — the norm is lowercase. All commands are lowercase.

Command Options: Commands usually accept options or parameters, which are given in a sequence beginning with a hyphen (-). If you would want to set options b, r and f for a command, type
command -brf
Other specifications, like file names, go after the options.

Shell Commands

Here's a brief introduction to some useful UNIX shell commands.

ls

This command displays a listing of a directory's contents. Unmodified, the command displays the contents of the current directory; you can also specify another directory's content to list. Some useful options are:

  • ls -l
If the list is long, you can display the list in pages. To do this, type
ls -l