Remember that one command that solved your problem? Was it cat, less, more, wc or something else? When we’re at the terminal, we can issue dozens of commands to solve a problem and in the background our Linux OS is recording these commands to a history file.
In this how-to we’ll look at various ways of searching and re-using our command history. Whilst you become accustomed to these commands it’s important to double check you don’t unintentionally reissue a command that could cause problems. Take your time using these new techniques and double check the details before pressing enter!
All the commands in this how-to will work on most Linux machines. We’ve used an Ubuntu install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t or by searching for the terminal app in your Applications menu.
The simplest way to look through your recent commands is to use the up and down arrow keys on your keyboard to scroll through the previous commands. If you want to reissue a found command simply press the enter key.
Viewing Your Command History in Linux
The history command, in its most basic use case, lists and annotates the last 1000 commands issued in the terminal emulator. Each command has a number associated with it.
1. Run the history command to see a list of the last 1000 commands. You’ll see that all the listed historical commands are given a unique reference number.
history2. Reissue the history command but constrain the amount of results to a specific number. This is useful if you know roughly when a command you are looking for was issued. You should see only the last 20 results listed.
history 20Reissue a Previous Command
Now we can use history to view our previous commands, we can choose and reissue a command using the number assigned to the history results.
1. Run history 20 to create a list of commands, choose a command to reissue making sure that the chosen command is safe to run. Choosing a simple command like cd Music (1660) is a good safe example. Note there is no space between the exclamation mark and the command number.
history 20 !1660Enhanced Linux History Search using Grep
By piping the output of history into grep we can perform a search of our command history returning results for a specified term or string. This is an excellent approach for finding a partially remembered command.
1. Search for a specific term using history and grep. We used the example search term “silhouette” as we recalled we had issued some commands to rectify a problem with a silhouette vinyl cutter. Replace that search term with something suited to your machine.
history | grep silhouetteUsing a Reverse Search of Linux Command History
Another handy approach to retrieve previous Linux commands is to use the reverse search function built into the terminal. To enter this mode you simply press ctrl and r. You can then enter a search term and use repeat presses of ctrl and r to step back through the list of previous commands containing that term. When you find a command you want to reissue press enter.
1. Press ctrl and r enters the reverse search mode, you should see the prompt now reads (reverse I search)`':
2. Type a search term and you should see the last command issued that contained this term. For example we added the search term sudo to show the previous commands issued with sudo privileges.
3. Repeat pressing ctrl and r to step through other results.
4. Run a previous command by pressing enter or quit the reverse search by pressing esc .
Quickly Reissuing the Previous Linux Command
Often we will want to simply rerun the last command we issued. We can achieve this simply using the !! command.
1. Run the ls command to set this as the example to test.
ls2. Reissue the last command using !!. Note that the previous command is listed and performed.
!!Sometimes we may try to reuse a command that requires elevated privileges, for example editing a file outside of our home directory. To do this we can preface the previous command with sudo. In the following example we append the first ls command to be reissued with sudo.
sudo !!Hiding Your Commands from Linux History
There may come a time where you need to keep a command out of your history, and if that scenario ever occurs, all you need to do is preface the command with a single press of the spacebar.
For example here are two ls commands, the second has a single space, hiding it from the history file.
ls lsWith a little practice all the above approaches become quite instinctive to use and can make your terminal session more powerful and efficient. The ability to locate and reissue commands is extremely useful, especially when recovering a rarely used command or a command that was hard to create in the first instance.