Shell skills for bloggers
Recently I helped someone with troubles they’d been having setting up WordPress using their blog host’s Control Panel tools. Of course, graphical user interface file transfer and management tools are always different, so for someone who has never dealt with that particular hosting company, it’s difficult to help someone with that without getting their user-name and password – which, of course, not everyone is willing to share. My host (and this person’s host) is based on Linux, and unlike Windows, Linux offers a remote “shell” (command-line) interface based on the “secure shell” system. The shell is a much faster and more powerful method of managing a file system than any graphical or web-based tool, but it relies on some cryptic commands.
Secure Shell or SSH is actually a complete set of tools for remote administration. It also includes, for example, SFTP, or Secure FTP. The main benefit of SSH is that your password is not transferred over the net in plain text, which may be intercepted. When my blog was destroyed last year by a system failure, my first thought was that it was done by criminal hackers who didn’t like the content of my blog (although people also do this just for a laugh or to work on their cracking skills). I asked my host’s not-very-technical support if they’d support SFTP to prevent future security breaches, and was told that the company did not support SFTP and had no plans to do so. It actually did, however – SFTP is part of SSH.
The shell itself is likely to be “bash”, which stands for “Bourne Again Shell”, after Steve Bourne who wrote the original modern Unix shell. Bash is the product of the Free Software Foundation, who were developing a freely-available operating system in reaction to the prohibitive licences Unix users and programmers had to work under in the early 1980s (their efforts are nowadays found in all operating systems, but particularly Linux and Mac OS X). These details, however, apply as far as I know to all Unix shells. To use SSH under Windows, you need the software called PuTTY. To use it under the Mac, you can either download Fugu or use the Terminal-based SSH program. You will get the details of what username and host (it may not be the same as for the control panel) from your ISP; type, at the Terminal prompt, ssh username@host or sftp username@host.
Before we discuss commands, you need to know some terminology. A directory is the same thing as a folder. Directories, as on Windows, can contain other directories, and where directory A contains directory B, for example, directory A is said to be “above” directory B. A dot (period or full stop) refers to the current directory; two dots represent the directory above. The tilde character – ~ – refers to your home directory; a 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; so, for example, ../wordpress refers to the item wordpress in the directory above the present one; ./wordpress refers to wordpress in the current directory, and ~/public_html or ~/htdocs are your main web directory, which is likely to be in your main home directory. (This is important; a domain shown in your web browser’s location bar refers to the main web directory, not to the owner’s home directory. For example, the blog Mind, Body & Soul (it’s just been updated – check it out!) contains “~optics” in its location, but from a shell, it would be accessed by its owner as ~/public_html.)
The forward slash ( / ) fulfils the same role it does in a web location; it shows the item after to be in (below) the directory before. A file or directory name can contain any standard character except a forward slash – including a space! However, when entering shell commands, you separate items with spaces. So, to represent a space in a file or directory name, you use a backslash in front of it, or put quote marks round the name. So a directory called My Pictures would be entered as “My Pictures” or as My Pictures. A forward slash on its own refers to the root directory, which is paradoxically at the top of the file system. (In Unix, every device is part of the same file system; there is no C-drive or D-drive, for example.)
The symbols * and ? are called wildcards, and can be used in any command which deals with files. * represents any number of characters; ? represents just one. So, the symbol * on its own refers to every file in a given space; g* refers to every file beginning with g; *g refers to every file ending with g; *.* refers to every file with a dot in it. “Wi?e” could refer to Wine, Wide, Wife or Wire; ? on its own refers to any file with a single-character name. Note that Linux filenames are case-sensitive, and the norm is lowercase. All commands are lowercase.
Commands typically accept options, which are usually given in a sequence beginning with the hyphen ( – ). If you want options, say, b, r and f, you type command -brf. You put other specifications, like file names, after the options.
Anyway, down to the commands.
ls – means list, and gives a list of what’s in the current directory (folder), or the specified directory. To list the full details, use ls -l. To display the list in pages, as you may need to if the list is long, type ls -l |less (that’s the bar which may be next to the z key, or the return button on a Mac). The bar means that the output from ls -l is submitted to the program less, which displays pages of whatever input it receives. You can press space to view the next page, or the arrow keys to go up and down. (The original page viewer was called more, and you can in fact use more for this purpose, but less, in fact, does more.) You can specify any other directory or file group to list after the options; ls -l g* would list all the files beginning with g, while ls public_html would list the contents of public_html.
The listing generated by ls -l contains the file’s type, permissions, owner, size, and date and time of last edit. At the left side you will see a series of letters and/or dashes; the first of these shows the file’s nature (d for directory, l for link, – for normal file); the others show the permssions. There are nine permission letters; the first three concern the file’s owner (you), the second his group (this doesn’t concern web host users), and the third people outside his group. The “rwx” stand for read, write, execute; if they appear, you can do these things, and if there is a hyphen instead, you can’t. On some hosts, you may need to change these permissions, which are done with the chmod command; your web host’s help page will tell you about this if necessary. Permissions are far too complex to go into here.
cd – changes the directory (put the directory name after cd). cd on its own moves to your home directory.
mkdir – makes a new directory.
rm – removes a file. To remove an entire directory and its contents in one go, type rm -rf directoryname; to do the same for everything in the present directory, use rm -rf *
Beware – once something is removed in this way, it is gone forever. To remove an empty directory, type rmdir directoryname.
exit ends your shell session. If you use this on a Mac running OS X, you will get another shell prompt – this time from your own Mac! OS X is a Unix-type operating system, as Linux is.
mv means move, and doubles as rename. You type mv file new-location. If the new-location is a directory, the file is placed, with its current name, in that directory; if not, and it doesn’t clash with an existing filename, it is given to the file you want to rename. You can, in fact, rename and move with one mv command, as in mv some-old-file ~/public_html/blog/new-file-name
cp means copy. To copy a directory and its entire contents, use cp -rf.
ln means link. The links which concern us are symbolic links or symlinks, which are files which “point” to other files or directories. If you access the link file in whatever way, you are actually accessing the original file. For example, my first WordPress blog was called Malden Life, and sat in a directory called maldenlife. When I decided to start using WordPress for my main blog, I simply imported all my entries into that blog and renamed it Indigo Jo Blogs. People access it through the symlink “blog”, which you can see in the location of this blog. If you want to change the location of your WordPress blog, the easiest way to do this is to type ln -s actual-file link-file. People will then be able to access the blog through either the file’s or directory’s actual name, or through the link name; in the “Options” page in WordPress admin, you can set the “Blog address (URI)” to show the link rather than the original name.
tar and unzip are used to decompress “archive” files. Archives are compressed bundles of files which are commonly used for distribution purposes (they were originally used for backup, hence the name). unzip is used for files ending in .zip; tar is used for files ending in .tgz or .tar.gz. If the file is a .tar.gz archive, type tar -zxvf file.tar.gz; if it’s a zip archive, you often need to make a directory in which to unzip it, or else all its files will appear in the current directory; the WordPress archive latest.zip is not like this, however (I just checked); you can just unpack latest.zip in your public_html directory and then rename it with mv.
wget downloads a file; you put a web location after it. So to download the latest wordpress release into the current directory, type wget http://wordpress.org/latest.tar.gz.
Finally, here’s a task which uses many of the commands described in this article: doing a minor WordPress upgrade.
(1) You need to know exactly where on your filespace your blog is actually located. This assumes it is at ~/public_html/blog
(2) In your home directory, download the WordPress archive, as shown above.
(3) Enter the new directory which was established when you unpacked the archive: cd wordpress
(4) Remove wp-config.php: rm wp-config.php
(5) Remove the entire wp-content directory: rm -rf wp-content
(6) Copy your own wp-config.php to the new directory: cp ~/public_html/blog/wp-config.php . (don’t forget the dot)
(7) Copy your own wp-content directory to the new directory: cp -rf ~/public_html/blog/wp-content . – this contains all your themes and plugins.
(8) Remove the entire contents of your blog directory: rm -rf ~/public_html/blog/*
(9) Finally, copy the entire contents of the new directory to your main blog directory: cp -rf * ~/public_html/blog