Type Less in Terminal

EDITOR'S NOTE: This article is an excerpt adapted from The Mac OS X Command Line: Unix under the Hood , by Kirk McElhearn (2004; reprinted by permission of Sybex).

Many people stay away from the command line because of the tedium of typing in long, precise commands. But you can save lots of time and type less by using the command history functions built into the shell you work with. The shell keeps a record of the commands you run, and you can access this history with a few simple keystrokes to run commands again or edit them, so you don’t have to retype them.

The command history is saved in last-in, first-out order, which means that the first command in the history list is the last command issued. If you want to run a command that you’ve already typed—say you want to rerun the last command you just typed—all you have to do is press the up-arrow key, and the shell displays the last run command at the prompt.

If you want to run a command you ran earlier, press the up-arrow key several times. As you do this, the shell cycles through your last commands, displaying each one at the prompt. If you decide that you don’t want to use a command in the history list, just press the down-arrow key until you get back to an empty prompt, and then enter a new command.

Rewriting History

Using the command history, you can rerun previous commands, but you can also modify commands and run them with different arguments. One of the simplest ways to do this is to use the

!!
command to run the previous command, appending new information to it. Let’s say you want to list the contents of a directory, like this:

$ ls ~/Library/Preferences/ByHost

After reading the list, you decide you want to save this list to a file. You can run the following:

$ !! > list.txt

Terminal replaces the

!!
shortcut with the last command you ran, so this is the same as typing this command:

$ ls ~/Library/Preferences/ByHost > list.txt

But this shortcut saves you a few keystrokes.

Edit the Commands

In addition to moving up and down the command history by pressing the up- and down-arrow keys, displaying each command at the prompt in Terminal, you can also edit the commands that display, or add arguments to them. This saves time when you need to run a command that is very similar but not exactly the same.

Say you want to copy a file; you type the following command:

$ cp /Users/kirk/Pictures/P101068.jpg /Users/Shared

Terminal says the file doesn’t exist, but you’re sure it does. When you go to the directory and check, you see that there is indeed a file with a similar name, but you left out a zero. Using the command history, you can press the up arrow to display the command again. Use the left arrow to move the cursor to the location where you need to add the zero, type

0
, and then press enter. (You don’t need to move the cursor back to the end of the line.)

You can use command editing to change commands and run them on different files, for example. If you have several files in your Pictures directory, you can use the up arrow to redisplay the previous command, change the file name, and run the command again on a different file.

Change Arguments and Options

Use the same trick to change a command’s arguments or options. In the previous example, I copied pictures into my Users/Shared directory. I could easily press the up arrow to redisplay the command and then alter the command to copy one of the files to a different location. There’s no need to retype the entire line.

By the same token, say you list the files in a directory and then decide you want to use the

-l
option to display a long list. Press the up arrow to redisplay the command, and then move the cursor to the left to add
-l
(for example,
$ ls -l /etc/periodic
).

Quick Access to Commands

You can check to see what commands are in your history at any time by running the following command:

$ history
. Terminal displays a list of the commands in the history list; for example:

1 10:13 ls

2 10:13 cd ..

3 10:13 ls

4 10:13 cd kirk

Each line includes the number of the command (from the first to the last), the time you ran it, and the command itself. Terminal includes all commands, whether or not they were successful. This means that erroneous or misspelled ones will appear in the history.

Limit Your List

The

history
command displays the entire history list by default; this list is limited to a certain number of commands, according to your shell settings. When this list gets very long, you’re better off not displaying it in its entirety. There are several ways of displaying just a part. In most cases, you want to see the most-recent commands. One way to do this is to run the
history
command with an argument that says how many commands you want to display—for example:
$ history 5
.

This tells the shell to display the last five commands in the history list. You can enter any number as an argument for the

history
command; if your history list is shorter than the number you specify, the shell will display the entire list.

Time-Saving Shortcuts

As discussed previously, you can move up or down your history list by pressing the arrow keys. This is the easiest way to rerun a command you executed recently. But if your command is further back in the list, there are quicker ways to tell the shell which one to run.

Say you have displayed your long history list, and part of it looks like this:

329 10:47 locate Walden

330 10:49 history

331 10:50 ls -l

If you want to reexecute the command

locate Walden
, type
$ !329
.

The exclamation point (

!
) is a shortcut for a command in the history list. If you enter a number after it (with no space between), Terminal runs the command that has that absolute number in the history.

Another way to specify a previous command is by using a relative number, or the nth command back from the end of the list. If you want to run the fifth command back, enter this:

$ !-5
.

You can also tell the shell to run the last command that begins with a specific string of characters. For example, another way to run the same

locate Walden
command would be to type the following (again, with no space after the exclamation point):
$ !loc
.

Enter as few characters as you want after the exclamation point. The shell will stop at the first occurrence of a string that matches these charac-ters. In the example just mentioned, I could have typed

$ !lo
since there were no other commands that began with those letters. But if I had merely entered
$ !l
, the example would have run command 331, the
ls -l
command, because this would have been the first match.

Enter as few characters as you want after the exclamation point. The shell will stop at the first occurrence of a string that matches these charac-ters. In the example just mentioned, I could have typed

$ !lo
since there were no other commands that began with those letters. But if I had merely entered
$ !l
, the example would have run command 331, the
ls -l
command, because this would have been the first match.

[ Kirk McElhearn is the author of several books, including The Mac OS X Command Line: Unix under the Hood (Sybex, 2004). His blog, Kirkville , talks about Macs, using the command line, iPods, and much more .]

Sidebar: Oops—I Forgot
sudo
!

There are many commands and areas of your computer you can’t access without root user privileges. If you have administrator rights for your computer, you can use the

sudo
command to prompt you for a password. But it’s annoying to type a long command only to have Terminal dourly reply “Permission denied.” If you forget to prefix a command with
sudo
, just type
$ sudo !!
—this command tells the shell to execute the previous command again, this time prefaced by
sudo
. Enter your password at the prompt and then press enter, and the command will run. There’s no need to type it all over again.

Subscribe to the Help Desk Newsletter

Comments