The Joy of UNIX

23rd September, 2008 @ 1:33pm UTC

Development, Linux, Mac OS X, Reference, Server, UNIX, Web / 8 Comments

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.

Like this post? Digg it or Del.icio.us it!

Comments (8)

Skip to the comment form…

  1. Gravatar Image Yoan September 23, 2008 @ 5:08 pm

    Just in case you’re stuck on a Windows box on day, try cygwin. I know it’s old, but always good: http://diveintomark.org/archives/2003/08/04/xp When did you plan to move to Linux to be ever closer to your production environment? GNU/Linux is guaranteed gluten-free .

  2. Gravatar Image Stuart Colville September 23, 2008 @ 6:06 pm

    Hey Tim, the unix programming environment is my favorite geek book of all time. I like how the learning curve takes you from introductions to basic commands all the way to writing your own programming language.

    As few other tips here’s a few off the top of my head:

    Ctrl + r presents a reverse search of your history. Start typing to pull up a command in your history or press ctl + r again to search further back without typing more characters.

    Var substition is also a very nice built-in feature of bash probably replaces the need to resort to sed or other commands when all you need is a simple substitution (see man bash for the full list of options):

    ${var/pattern/replacement}

    e.g:

    $ TEST=winna
    $ echo ${TEST/a/er}
    winner

    cd - (cd minus) takes you to the previous directory you were in. The previous dir is available in the variable $OLDPWD

  3. Gravatar Image Brad Wright September 23, 2008 @ 9:47 pm

    Try this little keyboard shortcut on OSX:

    Ctrl + R

    then type a fragment of any past command - full text history search. See the idiot version [1] for more on this.

    [1] - http://tinyurl.com/35avds

  4. Gravatar Image Steve Marshall September 23, 2008 @ 9:53 pm

    To add to your list of useful keyboard shortcuts:
    - esc followed by b: Moves the cursor to the previous whitespace character in the line
    - esc followed by f: Moves the cursor to the next whitespace character in the line

  5. Gravatar Image Brad Wright September 23, 2008 @ 9:54 pm

    Oh also:

    cd [enter]

    just by itself usually returns you to your home directory too. One less character than:

    cd ~

  6. Gravatar Image Tim September 24, 2008 @ 8:44 am

    Cheers chaps; these are all exactly the sort of thing I was looking for. :)

  7. Gravatar Image emilk October 3, 2008 @ 4:39 pm

    cd -
    is a nice way how to return to previous dir.

  8. Gravatar Image Renoir Boulanger September 1, 2009 @ 6:35 pm

    Wow. This is pretty straightforward tutorial.

    I enjoy really much your articles about OOP introduction and I just found that we have lots in common about interests :)

    You definetly gets into my blogroll… Anyway when I blog, I do write in french. So if it comes that I find your subject interresting I might make it into french :)

    See you bro!

Leave a comment





Categories

Syndication

Technorati

© 2010 Tim Huegdon, All Rights Reserved / Website design and development by Nefarious Designs

Powered by Wordpress / Log in

Caution: May contain traces of the undead.