An Introduction to the Unix Command Line

1 Conversation

Unix is a multi-user multi-tasking operating system, and if you were to install and run a bare-bones no-frills Unix system, you would most likely find yourself in front of this:

Welcome to Unix.

localhost login:

Or something very similar. It is here that you type your username, and then when prompted, you type your password, and then you get this:

/home/username>

Or if you're lucky, this:

[email protected] username $

You might even be treated to a flashing cursor, waiting expectantly for your commands. If you have not run out of the room screaming by now, calm down. It is not as scary as it at first may seem. You have merely landed up at a full-screen Command Line Interface (CLI), or Command Line User Environment (CLUE).

"But wait a bit," I hear you cry, "That's outdated now. We have cursors and graphical menu systems, buttons and desktops. I can ignore the CLUE nowadays." Yes, you can ignore it, but you are making a common mistake in your line of argument: the CLUE is not outdated; it was never superseded by the Graphical User Interface (GUI); the two are merely two different ways of using a computer: both are still valid, both are still used today, for they both have their advantages and disadvantages depending on your purpose. Leave your computer on for a moment and nip down to your local supermarket or other moderately sized store. Buy something to go on your mantelpiece and, when you are paying, take a look at the machine at the checkout. Look at the screen. Do you see a cursor? A menu? Buttons? Does the cashier use a mouse to control it? No, because she has a CLUE instead.

Even a fair amount of universities use a CLUE instead of a GUI on their campus computers.

Why?

There's one big point that really isn't hard to grasp: GUIs need more power to run them; while CLUEs are lightweight. You can see this easily just by looking at the system requirements of, say, DOS or Unix, and comparing them to something like Windows XP. Whip out that ancient pre-Pentium beast you have in the attic, and you can be sure Unix will run on it, and that can be a big advantage, because it means no computer is ever too old to have some use in it.

At universities this can be a blessing, or indeed at any establishment where large numbers of computers are needed to serve large numbers of people. The computers are likely to act as 'thin clients' to cut costs, and that's not to say that they've been on a diet, but rather that they don't have a hard disk or any software of their own, because they run it all directly from a central server. So CLUEs are less power-intensive and hence save enormously on hardware costs. And the vast majority of Unix operating systems and their variants are free of charge anyway, so you also save enormously on software. If network users only need to send and receive email, or store their data, or do non-GUI programming, then thin clients with CLUEs are ideal.

Those aren't the only advantages. Security is one other. Although 'viruses' are not of concern in Linux and Unix derivatives, the X Windowing System (or X11), which is the standard way of getting a GUI on Unix-esque systems, is likely to be the root cause of the huge majority of threats to the computer, especially if misconfigured. Computers that are networked, running off servers, or 'clustered' together to harness all their processing power, are not 'used' by people sitting in front of them, so they don't need a graphical interface. It is possible to connect to them and control them via a command line very quickly and securely, including over the Internet.

Finally, a lot of people claim the command line is more powerful than a GUI. Remembering a command is similar to remembering a nested menu structure, after all. Many graphical programs provide a 'macro' feature for repeating common tasks, and the idea of batch processing is inherent to command line programs. If you've got 500 JPEG images and you want them to be half the size and converted to PNG at the same time, typing just one command is sensible and efficient, and certainly quicker than opening them all individually in a photo editor.

Where do I start?

Well, to be perfectly honest, you don't need to start if you don't want to. There may be big advantages to a command line environment but for the general desktop user, benefits are slim, though not non-existent. You won't be able to play any commercial games, for a start, and even most word-processing tasks would be redundant. You may think such basic things as image viewing and manipulation, and watching DVDs, are suddenly out of the window too. That's not strictly true. A command line is a line where you type commands (called standard input) and get output (standard output), which is usually in the form of text. But if you learn the command for DVD-watching1, you can just as easily do that, except that full-screen command line interfaces (on what's called the 'framebuffer console') won't give you anything near the quality of image you expect. Yes, image editing is also possible2, using a series of commands.

Chances are you'll just find living under a CLUE a complete bore, which is why it's best reserved for its forté in universities, supermarkets and on webservers (so this might be helpful if you are thinking of hosting a website on your computer). However, if you are interested, or are studying these areas of computing, a guide follows that will get you through the basics of the Unix command line.

But I don't have Unix!

Don't you? If you are using Windows, you're right, you don't3. But most operating systems4 are built first with a command line, and then add a GUI on top. In most cases, your GUI is just a program that runs over the command line interface. In the case of Windows 98, for example, Windows is just a front-end running over its ancestor, DOS. You can still access a DOS prompt in Windows if you go to Start > Run and type 'cmd' in the box, then press Enter. But the commands for DOS are different to those for Unix, so unfortunately there's no use in your following the rest of this article.

So, who's left? Chances are, you're now using a Unix-based operating system. If you're on Linux, you'll probably know by now that you can get a 'terminal emulator' or console, such as xterm or Eterm, which provides a command prompt in a little window, similar to what our departed Windows friends were looking at just now. Mac OS X users are also in luck. Remember that your operating system is also a Unix derivative - heard of Darwin? That's the open source core of Mac OS X, and it looks and feels like any old Unix. Go to Applications in the Finder, root out Utilities and launch Terminal, or just put Terminal in a search bar. Notice the greeting?

Welcome to Darwin!

[email protected] username $

If you've still not been accounted for, you must be using a BSD or Solaris system, so launch a terminal in the usual way. The program running inside the terminal - whichever OS you're using - is called the 'shell', because it is the outer layer of the operating system at which the user can interface with the inner layers (the core layer is the kernel).

A nice way to start is to type the 'date' command5. Press Enter. How nice; we now have the current date and time. Check if you have time to continue reading and we'll carry on.

What is a computer? It's just a glorified counting machine. Ever seen your computer do its seven-times table? No? That's shocking isn't it? Computers should know their tables like the rest of us! So type this:

seq 0 7 95

This uses a program called 'seq'6. It's easy to see what this command means. From left to right, the three numbers mean: start at '0', count up in '7's, finish at '95'. Marvel at how well your computer knows its 7-times table.

The 'jot' program does a similar thing for those of you who don't have seq. Try this:

jot 15 0 91 7

Which means 'Start with 0 and add on 7 fifteen times until you reach 91'.

If you're jumping ahead, you may be thinking that your computer can't do its 8-times table in reverse (i.e. if you typed 'seq 96 8 0'). So now it's prime time to introduce the concept of pipes in order to solve the problem.

Pipes

A pipe is a hollow tubular affair, usually used for conducting fluids from one place to another, but what of a Unix pipe? Let's approach this carefully.

Commands have three components: their input (standard input, or stdin), their output (standard output, or stdout) and their errors (stderr). Stdin is the keyboard. Stdout is the screen. Stderr is also the screen, in most cases. A pipe allows the stdout of one program to go straight into the stdin of another program; in other words, you redirect a command's output away from your screen temporarily, and into another command. Imagine each command as a segment of a real pipe; you can connect the pipes together to get a pipeline. You can imagine the inside of each pipe as having some sort of widget that manipulates the contents of the segment in a certain way, before passing on to the next segment, and hence the next widget.

If you have Mac OS X Tiger you will probably have heard about its 'Automator' feature. This program is a GUI front-end for doing simple pipes, used for automating repetitive processes. If you have it, it's a good visual aid for seeing how Unix pipes work.

There is another standard Unix command called 'tac', which takes a file, or standard input, and prints its lines in reverse order7. This would be useful in the case of the reversed 8-times table, because we could do 'seq 0 8 96', for example, then pipe it to the 'tac' widget to get the lines in reverse, couldn't we?8 Here's how:

seq 0 8 96 | tac

The 'pipe' character is the '|' symbol, and there's no practical limit to how many of them you can use. Suppose you now want to impress a friend at your computer's ability to do its 8-times table in reverse. We'll change the ending number to something much higher so they don't suspect you wrote it yourself, and do this:

seq 0 8 960 | tac | mail -s'Look at what my computer does!' [email protected]

Unfortunately your friend is the sort of person who looks at the scrollbar of an email message and if it looks too long, puts it aside for later. Let's tidy up the output of seq:

seq -s', ' 0 8 960 | mail -s'Look at what my computer can do!' [email protected]

or

jot -s ", " 100 0 8 960 | mail -s'Look at what my computer can do!' [email protected]

It may look as though it's getting scary, but we'll pause to examine this bit by bit:

  1. seq and jot are just the names of programs.
  2. The '-s' bit is what is called an 'option' to the program. Each option modifies the output of the program in some way. Here, '-s' changes the text that separates each number from the next. By default, it puts a new line; now, it will put a comma followed by a space. We could have put seq -s' and then ' to get a rather convoluted sentence.
  3. The '0 8 960' are the 'arguments' to the program (its stdin) - in other words, what's going into the pipe.
  4. 'mail' is just the name of another program, one that sends email messages.
  5. '-s' is an option again, but it's an option to a different program, so it will do a different thing. In this case, it changes the subject line of the message that 'mail' sends.
  6. [email protected] is the argument for the mail program.

That friend of yours will give you a funny look when you next see her. Tell her you're expanding your horizons.

Let's have a really meaty pipeline now:

du -s * | sort -nr | head -10 | cat -n | sed G > tenlargestfiles

Come on, get out from behind the sofa. Look, it's not as scary as that! Type it in to your command line, wait a little while for the prompt to reappear, and then type 'cat tenlargestfiles'. What do you see? It's a list of the ten largest files in your current directory (your home directory), numbered, and indented from the left, starting with the largest.

  1. 'du' is the 'disk usage' program; it just finds out the sizes of files.
  2. '-s', in this case, only counts files that are given as arguments, and doesn't go into subdirectories.
  3. '*' is the argument. You probably don't have a file called '*', but that's beside the point. '*' is a special character that translates as 'all the files in the current directory'.
  4. Once we have that output, we pipe it to the 'sort' program, which puts things in alphabetical order...
  5. But we've told it to put it in numerical order with the '-n' option...
  6. And we've told it to reverse its output at the same time with the '-r' option.9
  7. We then employ 'head', which gets the first ten items in the list and discards the rest. See the steam coming out of the vent in the pipe?
  8. The 'cat' program has nothing to do with small furry mammals; it's short for 'concatenation'10. Basically, 'cat' is an empty pipe, it takes its input and shoves it straight into the next segment of the pipe. But here, we give it the '-n' option, forcing it to number every line it shoves through the pipe.
  9. For readability, we use the formatting swiss army knife program 'sed' to double space the output: 'sed G'.11
  10. The last thing we do is to use the redirect character - '>' - to redirect all this output to a file, for safe-keeping. 'tenlargestfiles' doesn't need to exist; if it does, it'll be overwritten, so you could use '>>' instead, which just plonks the output of the pipe at the end of the existing file ('appends the output').

Phew! Not too bad is it? A good exercise is to deconstruct that pipe and run it first with just the first command, then add the next and see how it changes, then add the next one, and so on.

But what if you want to keep a tab on the largest files every so often? Remembering all of that is a bit of an inconvenience isn't it? You don't have to remember it.

Over and Over Again

Depending on what it is you have in mind, there are a number of options for repeating a command.

Command History

Most shells keep a history of all your commands. Press the 'up' arrow on your keyboard and you should notice that the last command you typed has suddenly reappeared. Keep pressing 'up' and you can cycle far back into the past; just press Enter and you can perform the chosen command again. If you have a specific command in mind that you typed recently, you might prefer to use the 'history' command. Type it now, and watch as your life flashes before you. If it doesn't all fit on the screen you can usually use Shift+PgUp to scroll up the screen. There are lots of tricks you can do from here.

Type an exclamation mark followed by the number of the command in the history that you wish to re-execute, and it will be done. Or, if you remember that it was 10 commands ago that you executed it, you can type '!-10' to go straight to it.

Watching a Command

On Linux or BSD, if you press Ctrl+Alt+F12, you usually get a live look into the system log, to see what's going on behind the scenes. Suppose you could press Ctrl+Alt+F2, for example, and get a live look at the ten largest files in your home directory? Could be useful, but if it's not, you can do this with any command that suits you.

Just type 'watch' followed by the command you want to watch, which you should put in double or single quotes.12 What you get is the output of the command, updating every 2 seconds. If you want to change the interval between updates, add the '-n' option followed by your desired interval in seconds.

Make an Alias

In certain cases it may be appropriate to make a shortcut command for one larger one. This is called an 'alias' for that command. Type this:

alias gettenlargestfiles="du -sc * | sort -nr \

head -10 | cat -n | sed G | sed 's/^/ /'"

Now type 'gettenlargestfiles' and you'll notice it does the same thing as du-s * | sort -nr | \

head -10 | cat -n | sed G | sed 's/^/ /', only with much less cursing.

Note that if you want your alias saved for future sessions you need to put that line that you typed in your shell's start-up file. If your shell is bash, for example, you need to open the the '.bashrc' file in your favourite text editor. This file lists commands to be executed every time you start the shell.

Tab Completion

'gettenlargestfiles' is a bit of a mouthful in itself isn't it? You could make a shorter alias, or you could use tab completion. Type 'getten' and then press tab. Wow! It knows what you want to type! This works with all commands and all files and directories at your current level: type part of its name, and the shell will complete it if possible when you press tab.

Cron

What if you want the command executed every other Friday at 6am though? That's where cron bursts onto the stage. Cron allows you to schedule tasks to be done at regular intervals. It would far beyond our scope to go into details right here, but scout around for information, if you want, and you'll find it's not too hard.

File Management

You may be used to dragging and dropping files from place to place, and double-clicking folder icons to navigate your computer's hierarchy13. On the command line, it's a tad different. Notice that at every command prompt, your shell probably tells you the current directory. If you want the full path of where in Torvalds' name you are, use the 'pwd' command.

Now type 'ls' to see all the files and directories in your current folder. Find one that looks friendly and type 'cd' followed by the directory's name in order to access it. Remember that if it's a long name, you can use the tab button to complete it part way through.14 The command prompt should change to reflect your new position in the filesystem. To get back to where you came from (your 'parent' directory), type 'cd ..' ('..' is shorthand for the parent directory and '.' is shorthand for the current directory, just like '*' was short for 'all files in the current directory).

Moving and Renaming

Let's make a new directory in which to work. Type 'mkdir h2g2', or give it any name you want. Now 'cd' into that directory, where we'll create two files. If you just want to create an empty file, as we'll do here, you use the 'touch' command. Do this:

touch something

touch thatthing

Type 'ls' to see those two files in the directory. Now we shall rename 'something' 'Fred', and 'thatthing' we shall call 'Gertrude'.

mv something Fred

mv thatthing Gertrude

Type 'ls' again and notice that you no longer have 'something' and 'thatthing', but 'Fred' and 'Gertrude'.

Create a new directory: 'mkdir House'. Now we'll put Gertrude inside the House. This is achieved, oddly enough, with the same command, like this:

mv Gertrude House

Type 'ls' and see that you now only have Fred in the current directory, and another directory - a subdirectory - called 'House'. Type 'ls House' to see the contents of it, hence finding Gertrude doing the washing-up.

The Only Command You Must Remember

You might be thinking 'how am I supposed to know all these commands and their options and arguments?' Well, Unix has a built-in manual that documents all the commands. You can access it by typing 'man' followed by a command. For example, type 'man uniq' to get the manual page for the 'uniq' command. Type 'man man' to get an introduction to the manual.

Some incarnations of Unix also allow you to use the 'apropos' command to search for a keyword and find a command that does something related to that search. If you come across a command and don't know what it is, you can sometimes also use the 'whatis' command to decipher it. Try 'whatis grep', for example.

Dropping Down

If you are starting to feel comfortable with the command line in your terminal emulator, you can feel free to 'drop down' to a full-screen CLUE. This is easy on Linux and BSD: just press Ctrl+Alt+Fn, where n is any number from 1 to 615. On Mac OS X, you'll need to go to System Preferences, then Login Options (you'll probably need to type your administrator password too). Change the viewing preference to 'Name and password' and then log out. In the username field of the login screen, type '>console'. No need to put a password.

In these methods you will 'drop down', as they say, to a console. From here, use your everyday login details to log in. To get back to the login screen of Mac OS X, type 'exit' or 'logout' or press Ctrl+D. In Linux/BSD, press Alt+F7 to return to X.

Multi-tasking

Remember the definition of Unix at the beginning? A 'multi-tasking' operating system, it said. That means you should be able to run two commands simultaneously. You can run a command in the background simply by putting an ampersand (one of these: &) at the end of the command. Let's try it. Enter:

pico Fred &

You should get a few numbers appearing. This is because the program 'pico' (a text editor) is now in the background. To see it, type 'fg', which brings it to the foreground. Type a few words in the Fred file, then press Ctrl+X. Press 'y' to confirm that you wish to save the file, and you'll end up back at the shell. Now enter:

pico House/Gertrude &

pico Fred &

Now, two files are open with pico. Typing 'fg' will bring the most recently backgrounded job to the foreground, i.e. Fred. We want Gertrude this time. Type 'jobs', and you will see a list of the tasks running. Each has a number. Gertrude is probably number 2 in this case, so we type 'fg %2' to get the file called 'Gertrude' to come up and see us.

There's an even better way to multi-task on the command line. You can use a 'window manager' called GNU screen. Just type 'screen' to launch it, if you have it installed. You can then type 'screen' again to get a new 'window', and then use Ctrl+a followed by a press of the spacebar to switch between windows. Press Ctrl+a then ? for a quick list of other commands you can give screen. You can, for example, get a selectable list of all your windows by typing Ctrl+a then ".

Multiple Users

Remember the definition of Unix at the beginning? A 'multi-user' operating system, it said. That means you should be able to have more than one person using the computer. On Linux and BSD, that's simple. Just use the Ctrl+Alt+Fn key combination again to access a nice fresh console (called a 'virtual console' or 'virtual terminal'). Here, another user can log on. On Mac OS X, best to use the built-in user-switching mechanism, because you don't have virtual terminals.

You can also have more than one user using the computer simultaneously. This happens when Unix is being used in a server environment, where lots of computers ('clients') are using the same operating system, which is stored on the server. In this case, each of the clients is like a virtual terminal, only they are really a real terminal. Chances are that your computer is not a server, but if you have a local network you can set up something like Telnet or SSH to use your computer in a command line fashion from another remote computer, even if somebody else is currently sat at, and is using, your computer.

Scripting

If you are in the habit of executing long strings of commands one after the other on a regular basis, don't be. Make a script. A script is basically an executable file - a command in its own right - that lists a raft of other commands in the order that you want them executed. The script goes through this list executing them. In fact, scripting is like fully-fledged programming, because you can incorporate 'if's and 'else's and set variables and do all sorts of other tricks to automate processes that you often do.

The Forgotten Advantage

For an introduction, that just about covers it, except to mention the one advantage we did not mention before. The command line offers a huge amount of power for controlling your computer. You can use it for web browsing16, smart downloading17, sending and receiving email18, and even image, sound and video manipulation. You can sort, search and organise your files, and perform fairly advanced text processing and formatting tasks, as well as numerical manipulations, all of which can have their uses. In short, the command line means power; it means control over your computer; it means knowing about how your computer works on a more fundamental level.

And that can't be a bad thing.

1Use mplayer2Use ImageMagick.3Options include: booting up a LiveCD like KNOPPIX; investigating the GnuWin32 project; or seeing if your web host provides Telnet or SSH access.4Notable exception being Mac OS classic.5Don't put the quotes in any of these commands, of course.6If you don't have the seq program (ie it says 'command not found'), then we'll use the 'jot'program). Seq is included with the GNU coreutils package (in the shellutils component).7It seems Mac OS X does not come with the 'tac' program. If you don't have the program, replace all instances of it with 'sort -r', which will do the same thing, but uses the 'reverse output' option to the 'sort' command instead.8Using the 'jot' command instead of 'seq' is left as an exercise for the reader.9The same effect can be achieved with the 'tac' program.10And notice that the 'tac' program is 'cat' backwards.11To save your sanity, we won't go into what the sed commands mean here.12For now, use double quotes if the command you're executing contains, in itself, single quotes; and vice versa.13Thank you, Tiger users, who laugh in the face of people who navigate through their computer hierarchy to find things. If that's you, take a look at the 'mdfind' command. That'll keep you amused for a while.14Also, spaces need to be 'escaped'. If you have a directory called 'My Brilliant Guide Entries', you'd type this as 'My\ Brilliant\ Guide\ Entries', i.e. put a '\' before each space. Same with apostrophes.15Though distributions may vary.16Try the 'links', 'lynx' or 'elinks' command to get a CLUE web browser17'wget' and 'curl' are good commands that allow you to download a web page and save it. Type 'wget http://www.bbc.co.uk', for example, to save the BBC's home page in your current directory. Like any other command, you can use wget and curl in pipelines, in scripts, in aliases and so on, making for some very powerful HTTP interfacing.18'pine' and 'mutt' are examples

Bookmark on your Personal Space


Entry

A5253455

Infinite Improbability Drive

Infinite Improbability Drive

Read a random Edited Entry


References

h2g2 Entries

External Links

Not Panicking Ltd is not responsible for the content of external internet sites

Disclaimer

h2g2 is created by h2g2's users, who are members of the public. The views expressed are theirs and unless specifically stated are not those of the Not Panicking Ltd. Unlike Edited Entries, Entries have not been checked by an Editor. If you consider any Entry to be in breach of the site's House Rules, please register a complaint. For any other comments, please visit the Feedback page.

Write an Entry

"The Hitchhiker's Guide to the Galaxy is a wholly remarkable book. It has been compiled and recompiled many times and under many different editorships. It contains contributions from countless numbers of travellers and researchers."

Write an entry
Read more