How to Use the history Command to Re-Run Previous Commands in Ubuntu

Working in the Linux terminal often involves typing out incredibly long, complex commands featuring absolute file paths and multiple flags. If you spend three minutes carefully typing out a command to compile a software package, and then realize you need to run that exact same command again 15 minutes later, manually retyping it is both frustrating and prone to typos.

Fortunately, the Bash shell (the default command-line environment in Ubuntu) has an excellent memory. It automatically records every single command you type into a hidden text file. You can easily access this log to review your past actions and instantly re-execute complex commands without typing a single letter.

This guide explains how to use the history command in the Ubuntu terminal.

Step 1: View Your Command History

To see a list of the recent commands you have executed, simply type the following word into the terminal and press Enter:

history

The terminal will output a numbered list of your past commands. By default, Ubuntu stores the last 1,000 commands you have run across all your sessions.

 498  cd /var/www/html/
 499  ls -l
 500  sudo nano wp-config.php
 501  sudo systemctl restart apache2
 502  history

Step 2: Re-Run a Specific Command Using the Exclamation Point

Now that you can see the list, you can use a shortcut called “bang” (the exclamation point symbol !) to tell the terminal to instantly execute a specific line number from the history list.

Looking at the example above, if you want to restart the Apache web server again, you do not need to type the full systemctl command. You just need to reference line 501.

Type the following and press Enter:

!501

The terminal will immediately echo the full command (sudo systemctl restart apache2) to the screen and execute it simultaneously.

Step 3: Search Your History Using Grep

If you are looking for a command you ran three days ago, scrolling through 1,000 lines of history is inefficient. Instead, you can “pipe” the output of the history command directly into a search tool like grep.

For example, if you know you ran a command that involved the word docker, but you cannot remember the exact syntax, you can search your history like this:

history | grep docker

The terminal will filter the massive list and only display the specific lines from your history that contain the word “docker”, complete with their line numbers. You can then use the !number trick to execute the one you need.

Alternative: The Reverse Search Shortcut (Ctrl + R)

The absolute fastest way for power users to search their history is to use the interactive reverse search tool. You do not even need to type the history command.

  1. At a blank terminal prompt, press Ctrl + R on your keyboard.
  2. The prompt will change to read (reverse-i-search)`':.
  3. Start typing a fragment of the command you are looking for (e.g., type apt).
  4. The terminal will instantly auto-fill the most recent command you ran that contains that fragment (e.g., sudo apt update).
  5. If that is not the right command, press Ctrl + R again to cycle backward to the next match.
  6. When you see the correct command, press Enter to execute it immediately, or press the Right Arrow key to drop the command onto the prompt so you can edit it before running it.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.