Skip over navigation

The Lost continent of

You've found a bug on my site!

The fear of capitalism has compelled socialism to widen freedom, and the fear of socialism has compelled capitalism to increase equality.

Ariel Durant

The Command Line

Where the real power is wielded...

Finding it
Using it
Arguments 1: Behaviour
Arguments 2: Targets


In this point-and-click world, the command line is becoming a bit of a lost art. Those coming from a Windows background, as I did, often have unpleasant DOS prompt memories and consider the command line a relic of the past.

The command line is a lot more easier to use, fast, and powerful (and therefore fun) than you may think. I'm writing this little tutorial because being able to use the command line is such a fundamental part of many of the things I talk about in other articles. It's worth getting to know...

Yes, but what is it?

A command prompt, or shell, is simply a place where you type commands for your computer to follow. It's actually a program in it's own right, albeit one with the simplest possible user interface.

Do I have one on my computer?

It really depends on the operating system, and version, you are using. Hunting around your systems application menus should find it. It's also known variously as a DOS prompt, a terminal, an emulator, the command line, or a shell.

GNU/Linux Users

The command line has a long and noble history in the UNIX/Linux world, going back to before I was born (and I'm starting to get old). This screenshot is from KDE's Konsole. Other popular terminals are xterm, and Eterm.

You are looking for something like this...

Windows Users

Mac OS X, true to it's UNIX roots comes with some powerful shells. You can get more opensource programs to use with it from the excellent Fink project.

You are looking for something like this...

Windows Users

Under Windows, a so-called DOS prompt is almost always available, but it's next to useless. You can download a free shell and collection of opensource programs to go with it from

You are looking for something like this...

Tell your computer what to do. Boss it around a little!

When using a shell the basic sequence of events is as follows:
You type commands, the shell runs the command (which is usually just a small program), then shows you any output. When the command finishes running you return back to the shell. For example:

$ ls
diagram.png   resume.tex   subdir/
$ _

Let's have a look at what's going on here. We run the command ls. The shell runs the ls program, and displays the output. We can see that the output is a directory listing (ls is shorthand for 'list' — it's the directory listing command). When ls finishes we are returned to the prompt and the shell waits for us to give it it's next command.

Why ls, why not just list?

The short answer: because it's faster to type. Once you have learned the basic set of commands, using the command line is very efficient indeed, but until then it all seems rather obtuse.

Arguments: Changing the Behaviour of Commands

Often we want to change the way a command behaves. Say for example we want the ls command to show us the file sizes as well as the file names. We can do that using arguments (text typed after the command name). Arguments can do different things, but when the are prefixed by a hyphen (-) they tend to modify program behaviour:

$ ls
diagram.png   resume.tex   subdir/
$ ls -s
total 1332
1308 diagram.png    20 resume.tex     4 subdir/
$ ls -s -h
total 1.4M
1.3M diagram.png   20K resume.tex  4.0K subdir/
$ ls -l
-rw-r--r--  1 leonov leonov 1334262 Dec  7 12:58 diagram.png
-rw-r--r--  1 leonov leonov   17224 Dec  7 12:58 resume.tex
drwxrwxr-x  2 leonov leonov    4096 Dec  7 12:58 subdir/

The -s argument shows file sizes, the -h formats file sizes for 'humans', and the -l argument gives us the 'long' file listing. Arguments can be combined:

$ ls -l
-rw-r--r--  1 leonov leonov 1334262 Dec  7 12:58 diagram.png
-rw-r--r--  1 leonov leonov   17224 Dec  7 12:58 resume.tex
drwxrwxr-x  2 leonov leonov    4096 Dec  7 12:58 subdir/
$ ls -lh
1.3M -rw-r--r--  1 leonov leonov 1.3M Dec  7 12:58 diagram.png
 20K -rw-r--r--  1 leonov leonov  17K Dec  7 12:58 resume.tex
4.0K drwxrwxr-x  2 leonov leonov 4.0K Dec  7 12:58 subdir/

Tip: If you use the same arguments all the time, you can save some typing by setting up aliases. For example, on my computer if I type ls the command that the shell actually executes is ls -shF --color=auto...

Arguments: Tell Commands what to work on

The other class of arguments tell the command what to work on, and are not prefixed with a hyphen. For example, our ls command will list the contents of the current directory by default, but sometimes we want to list the contents of some other directory instead:

$ ls
diagram.png   resume.tex   subdir/
$ ls /home
leonov/  library/  websites/
$ ls /home/library
distfiles/ games/  it_reference/  movies/  music/  wavs/

Tip: When you are typing in a filename you don't have to type the whole thing. Just type the first few letters then press the <Tab> key. Auto-completion will do the rest!

That's the Basics!

We have covered just the very basics of command line usage. It's probably just enough information to convince you that the command line is a bad idea! Never fear, it gets more and more useful the more you use it. I'll leave you with a couple of useful, real life examples.

Some examples from my own workday today

Counting the number of words of every file in a folder

$ wc -w *
   6034 amy.tex
   5296 arrival.tex
   4442 bath.tex
   6250 dog.tex
   3160 headingwest.tex
   6881 highland.tex
   3938 hightailing.tex
   6824 interior.tex
   4870 jenny.tex
   5940 linda.tex
   7339 london.tex
   5876 work.tex
  66850 total

Create thumbnails of every single jpeg file in a directory

$ mogrify -format jpg -thumbnail 110x90 '*.jpg'

Print the filename of all files in my website that contains the text '@todo'

$ grep -lr @todo *

Do the same, but instead of printing the filenames, open them all for editing. Very useful!

$ grep -lr @todo * | sort -u | xargs kate

Play a movie, full screen with the sound muted, looping forever

$ mplayer -nosound -loop 0 -fs sample.mpg

Checking the load on our webserver (on the other side of the city)

$ ssh -C mongo w
 14:01:40 up 60 days,  7:41,  1 user,  load average: 0.09, 0.03, 0.01
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT
leonov   pts/0    Mon14    0.00s  0.21s  0.06s  w