Remember that one command that solved your problem? Was itcat,less, more, wcor 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!

A DeLorean car with Tux the Penguin

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 aRaspberry Pi. All of the how-to is performed via the Terminal. You can open a terminal window on most Linux machines by pressingctrl, altandtor by searching for the terminal app in your Applications menu.

Scrolling Through Your Previous Commands in Linux

The simplest way to look through your recent commands is to use theupanddownarrow keys on your keyboard to scroll through the previous commands. If you want to reissue a found command simply press theenterkey.

Viewing Your Command History in Linux

Thehistorycommand, 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 thehistorycommand to see a list of the last 1000 commands.You’ll see that all the listed historical commands are given a unique reference number.

Command History in Linux

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

Get Tom’s Hardware’s best news and in-depth reviews, straight to your inbox.

Command History in Linux

Reissue a Previous Command

Now we can usehistoryto view our previous commands, we can choose and reissue a command using the number assigned to thehistoryresults.

1.Runhistory 20to create a list of commands, choose a command to reissue making sure that the chosen command is safe to run.Choosing a simple command likecd Music(1660) is a good safe example. Note there is no space between the exclamation mark and the command number.

Command History in Linux

Enhanced Linux History Search using Grep

By piping theoutput ofhistoryintogrepwe 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 usinghistoryandgrep.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.

Command History in Linux

Using 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 pressctrlandr. You can then enter a search term and use repeat presses ofctrlandrto step back through the list of previous commands containing that term. When you find a command you want to reissue press enter.

1.Pressctrlandrenters the reverse search mode, you should see the prompt now reads(reverse I search)`':

Command History in Linux

2.Type a search termand you should see the last command issued that contained this term. For example we added the search termsudoto show the previous commands issued with sudo privileges.

3.Repeat pressingctrlandrto step through other results.

4.Run a previous command by pressing enter or quit the reverse search by pressingesc .

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 thelscommand to set this as the example to test.

2.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 firstlscommand to be reissued withsudo.

Command History in Linux

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.

Jo Hinchliffe

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

Jo Hinchliffe is a UK-based freelance writer for Tom’s Hardware US. His writing is focused on tutorials for the Linux command line.