How to Run Previous Commands in Linux Using the History Command

The Linux command line is incredibly powerful, but it requires typing long, complex strings of text. If you frequently connect to a server via SSH, you will inevitably find yourself executing the same lengthy commands repeatedly—such as restarting a specific web service, executing a complicated SQL query, or mounting a specific network drive with multiple flags.

Instead of memorising these long commands or constantly pressing the “Up” arrow key to scroll through your recent activity, you can harness the power of the Bash history command. This built-in feature records everything you type, allowing you to instantly search for and re-execute complex commands with a few keystrokes.

How to View Your Command History

By default, the Bash shell saves the last 1,000 commands you have executed in a hidden text file (~/.bash_history). To view this list, simply open your terminal and type:

history

Press Enter, and the terminal will print a massive list. Every single line will have a unique index number on the left side, followed by the exact command you executed on the right side.

Because the list is usually too long to read comfortably, it is best practice to filter the results. For example, if you know you want to find a complex systemctl command you ran last week, you can pipe the history output into the grep search tool:

history | grep systemctl

This will only display historical commands that contain the word “systemctl”.

How to Re-Run a Command by its ID Number

Once you locate the specific command you want to repeat in the history list, look at the index number on the far left (for example, 452).

To execute that exact command again instantly, type an exclamation mark (also known as a “bang” in Unix terminology) immediately followed by the index number, with no spaces.

!452

When you press Enter, the terminal will first print the command to the screen so you know what is happening, and then execute it immediately. This saves you from having to copy and paste the text with your mouse.

How to Re-Run the Very Last Command

The most common use case for the history buffer is realising you forgot to use sudo (administrator privileges) after you press Enter and receive a “Permission denied” error. Instead of typing the whole command out again with sudo at the front, you can use the double-bang shortcut.

Typing two exclamation marks tells the terminal to execute the very last command in the history buffer.

sudo !!

This translates to “run the command I just typed, but with sudo privileges.”

How to Search Backwards Interactively

While viewing the list and using ID numbers is reliable, power users prefer the “Reverse-i-search” feature. This allows you to search your history interactively without ever printing the list to the screen.

  1. Press Ctrl + R on your keyboard.
  2. Your command prompt will change to (reverse-i-search)`':.
  3. Start typing a fragment of the command you are looking for. For example, type mount.
  4. The terminal will instantly display the most recent command you executed that contained the word “mount”.
  5. If it is the wrong command, press Ctrl + R again to jump to the next most recent match.
  6. When you see the correct command on the screen, simply press Enter to execute it.

Mastering the Ctrl + R shortcut is one of the fastest ways to improve your efficiency when navigating the Linux terminal.

Get the best tech tips delivered straight to your inbox.

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