The Joy of UNIX

Published 21:33 on 23 September, 2008

One of the best things about developing from a Mac is the fact that it is a UNIX-based operating system. This means that my development environment is a far closer match to my production hosting environment (Linux) than it was when I was developing under Windows.

The net result of this shift in paradigm is a rapidly expanding knowledge of the command-set and OS structure. The exposure to UNIX has done me the world of good; particularly in regard to web development and server management. Sadly, the relative shelter I was accustomed to at Rentokil Initial, thanks to the inclusion of immensely talented and helpful server admins within our team, had hindered my development as a web developer, and provoked ridicule at the hands of my fellow Y! EU developers (in the nicest possible way, of course).

With all that in mind, here’s a bunch of stuff I’ve learnt that might be a useful reference resource…

Books

The first book I’d recommend reading for a good grounding in UNIX is “The UNIX Programming Environment”, by Brian W. Kernighan and Rob Pike. Although this is book is over 20 years old, it’s still highly relevant; Kernighan and Pike both worked at Bell Labs during the development of UNIX and contributed greatly to the OS and its school of thought. The book covers a huge expanse of subjects, from the basics through to full program development.

The second book, as is so often the case, is published by O’Reilly. “UNIX in a Nutshell”, by Arnold Robbins, is a great, up-to-date examination of the operating system, including a vast array of information perfect for beginners and experienced users alike.

Both of these books should provide enough knowledge for you to feel safe getting down and dirty with UNIX; so let’s dive in with a look at some useful keyboard and command line shortcuts:

Keyboard and Command Line Shortcuts

The following are common keyboard and command line shortcuts for use in UNIX-based systems. Please note that all UNIX variants differ and some of these shortcuts may not be available to you, or may require different syntax or keystrokes.

Keyboard Shortcuts

To begin with, here’s a list of useful keyboard shortcuts:

  • CTRL + A — Moves the cursor to the beginning of the line.
  • CTRL + E — Moves the cursor to the end of the line.
  • CTRL + U — Erases the complete line.
  • CTRL + B — Moves the cursor backward one character.
  • CTRL + F — Moves the cursor forward one character.
  • CTRL + H — Erase one character. Similar to pressing backspace.
  • CTRL + W — Deletes the last word typed.
  • CTRL + C — Cancels the currently running command.
  • CTRL + D — Logs out of the current session.

Most of those are self-explanatory, but you’ll wonder how you lived without them soon enough.

Command Line Shortcuts

Next, let’s take a look at some useful command line shortcuts, starting with directory navigation:

  • ~ — References the current user's home directory.
  • . — References the current directory.
  • .. — References the parent of the current directory.

Here’s a quick demonstration to hopefully make their use a little clearer (the names have been changed to protect the innocent). Note, this example uses several UNIX commands; cd = change directory, pwd = print working directory, ls = list contents of directory, and cp = copy files:

$ pwd
/Users/timbo
$ cd /var/www/nefarious/
$ pwd
/var/www/nefarious
$ cd ..
$ pwd
/var/www
$ ls
a-website   another-website   nefarious
$ cd /var/www2
$ pwd
/var/www2
$ ls
$ cp /var/www/* ./
$ ls
a-website   another-website   nefarious
$ cd ~
$ pwd
/Users/timbo

Now that we can effectively navigate the directory structure, let’s have a look at the incredibly useful history utility and its command line shortcuts:

  • !! — Re-run your last command.
  • !10 — Re-run line number 10 in the history.
  • !-n — Refer to the current command line minus n.
  • !?str? — Refer to the most recent command containing str (string).
  • !str — Re-run the last command ran that started with str (string).
  • !$ — Re-use the parameters from the previous command.

I won’t go into detail on each one of those, but here’s an example of the last one — arguably the most useful — in action:

$ pwd
/Users/timbo
$ ls projects/django/
cms    coltrane    davis
$ cd !$
cd projects/django/
$ pwd
/Users/timbo/projects/django

Summary

This is just a quick reference to some useful shortcuts; I could have delved deeper into setting up aliases in your profiles and making your prompt look pretty, but I thought I’d save that for a possible follow up post.

I hope somebody out there finds this useful; and feel free to add your own tips and tricks in the comments.