How to Re-run Last Executed Commands in Linux?


Re-running commands in the command line is a regular task which all of us go through when working on the Unix systems. In the below article we will see various ways how we can rerun the commands we have already executed this helps save time and it helps reasoning longer commands easily without retyping them.

Before we get into how to re-execute previous command let's see how we can look at the list of all the commands. There is a command call history which lists down all the executed command for a specific time period configured by the system. Below is an example of how to execute the history command and see its results.

$ histrory

Running the above code gives us the following result −

$ history
1 perl -v
2 sudo -apt update
3 cal
..
..
231 curl -s https://ipvigilante.com/122.175.62.177
232 curl -s https://ipvigilante.com/104.80.62.56
233 ls -l

Up and Down Arrow Keys

In the terminal on pressing the up and down arrow keys we can revisit the command already executed repeatedly pressing them helps us cycle through all the commands that we have executed going forward and backward among each of them.

Using !!

This command only execute the previous command./p>

$ !!

Running the above code gives us the following result:

ls -l
total 60
drwxr-xr-x 2 ubuntu ubuntu 4096 Dec 13 19:35 Desktop
drwxr-xr-x 2 ubuntu ubuntu 4096 Dec 13 01:58 Documents
drwxr-xr-x 2 ubuntu ubuntu 4096 Dec 13 19:40 Downloads
……….
…………

! with Specific Command

If you want specific command from to be executed but you only remember a part of that command then you can use the! With part of the command I am the full command will get executed.

$ !nsloo

Running the above code gives us the following result −

nslookup www.oracle.com
Server:127.0.1.1
Address:127.0.1.1#53
Non-authoritative answer:
www.oracle.comcanonical name = ds-www.oracle.com.edgekey.net.
ds-www.oracle.com.edgekey.netcanonical name = e870.dscx.akamaiedge.net.
Name:e870.dscx.akamaiedge.net
Address: 104.80.62.56

Updated on: 03-Jan-2020

191 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements