2 Ways to Re-run Last Executed Commands in Linux

One of the greatest features of Bash is the command history, which stores all commands a user runs, in a history file within his/her home directory (typically /home/$USER/.bash_history). This allows the user to easily recall, edit and rerun previous commands.

In this article, we will demonstrate how to re-execute a specific command from the history of commands entered to a shell. This is useful to avoid typing the same commands over and over again.

Normally, to get a command you recently run, you can use the Up arrow keys to retrieve a previous command. Pressing it constantly takes you through multiple commands in history, so you can find the one you want. Use the Down arrow to move in the reverse direction.

However, the history file may contain a lot of entries, to re-execute a specific command from the history of commands, you can run the history command.

$ history 
List Last Executed Linux Commands

List Last Executed Linux Commands

Then get the number(s) of the command(s) you want to re-execute (if, for example you want to restart PHP-FPM and view its status, you need to re-execute the commands 997 and 998) as shown.

$ !997
$ !998
Re-excute Previous Commands in Linux

Re-excute Previous Commands in Linux

You can also re-execute previously used command (sudo yum update) with '!' character followed by a few of the first characters (for instance sud or sudo) of that particular command as shown.

$ !sud
OR
$ !sudo
Re-excute Previous Command Using Exclamation Mark

Re-excute Previous Command Using Exclamation Mark

For more information about Bash history, see these following guides:

  1. The Power of Linux “History Command” in Bash Shell
  2. How to Clear BASH Command Line History in Linux
  3. Set Date and Time for Each Command You Execute in Bash History
  4. Useful Linux Command Line Bash Shortcuts You Should Know

That’s all! Bash history is a cool feature that allows you to easily recall, edit and rerun previous commands. If you know other ways to re-excute the last exucuted command do share with us in the comment section.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

6 Responses

  1. Sriram says:

    ctrl+r and some letters of the earlier commands, helps more practically.

  2. Lester Iván Arévalo says:

    Or !* if you need only the arguments of your last commands.

  3. Lester Iván Arévalo says:

    And the most important: sudo !! To execute the last command but with privileges this time.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.