Monday, July 22, 2024

How to look at your command history list in macOS’s Terminal

How to look at your command history list in macOS’s Terminal

With the Terminal in macOS, you can save lots of time and type less by using the command history functions built into the Terminal shell. 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.

You can check to see what commands are in your history at any time by running the history command. When you type history and press Return, Terminal displays a list of the commands in the history list. For example:

1 ls

2 cd ..

3 ls

4 cd kirk

Each line includes the number of the command (from the first to the last) 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 the history list

There are several ways of displaying your history list. 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 tells the shell to display the commands starting with the fifth one in the history list. You can enter any number as an argument for the history command; if you enter a number that’s invalid (most likely, your list isn’t as long as the number you entered), Terminal will respond with fc: no such event.

Time-saving shortcuts

As discussed in a separate article, 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 locate Walden

330 history

331 ls -l

If you want to re-execute 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.

Relative numbers: Another way to specify a previous command is by using a relative number, or the nth command back from the end of the list. For example, if your history is 200 entries long and you want to enter the fifth command back from the 200th entry, enter !-5.

Characters: You can 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 !loc (with no space after the exclamation point).

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 characters. 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.

Editor’s note: Parts of this article were adapted from The Mac OS X Command Line: Unix Under the Hood, by Kirk McElhearn (2004; reprinted by permission of Sybex). It has been updated to reflect the changes in the Terminal for macOS Big Sur.

Similar Posts