Skip to main content

How to View Your Command History in Linux

Command History in Linux
(Image credit: Tom's Hardware)

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 a Ubuntu 20.04 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.

Scrolling Through Your Previous Commands in Linux

(Image credit: Tom's Hardware)

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

(Image credit: Tom's Hardware)

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.

history

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.

history 20

Reissue a Previous Command

(Image credit: Tom's Hardware)

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
!1660

Enhanced Linux History Search using Grep

Command History in Linux

(Image credit: Tom's Hardware)

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 silhouette

Using a Reverse Search of Linux Command History

(Image credit: Tom's Hardware)

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

(Image credit: Tom's Hardware)

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.

ls

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 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
ls

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.  

  • clsgis
    I use that reverse history search all the time. But not the way you showed it. Control-r is an emacs thing. I've been using vi (elvis, nvi, vim...) since it was new. Its visual command language is in muscle memory. I'm never gonna learn emacs.

    So the first thing I do with a new Linux system is add this line to /etc/inputrc
    set editing-mode vi
    Any program that uses the GNU Readline library will now read lines in vi mode, not emacs mode. The Bash shell, the Python interactive interpreter, the mysql "monitor," even the lowly FTP client. Each of those things will remember your typing-into-them history.

    When the shell is reading lines in vi mode, you just hit escape and your shell prompt turns into a one line vi window. Most vi "visual" commands work as you would expect. You can cut and yank and paste. But instead of :x or :wq to save and quit, you just hit enter when the command looks right.

    To try it, type
    set -o viinto the shell, to put it in vi mode temporarily. Now you can go
    escape k k and you are scrolling up in your command history. because k is the vi command to go up a line. Hit enter when you see a command you want to run again. Or change it with vi commands, and run the changed command. To find the last time you ran ps,
    escape /ps enterbecause / is the search command in vi.


    link
    Reply
  • clsgis
    clsgis said:
    So the first thing I do with a new Linux system is add this line to /etc/inputrc
    That's a lie. The first thing is change the default passwords. Second thing is update anything that was stale in the installation medium. Third thing is /etc/inputrc 🆒
    Reply