How to Re-run Last Executed Commands in Linux?

Re-running commands in the Linux terminal is a common task that saves time and effort. Linux provides several built-in methods to execute previously run commands without retyping them. Understanding these shortcuts improves productivity when working with the command line.

Viewing Command History

Before re-executing commands, you can view your command history using the history command. This displays all previously executed commands with line numbers ?

history

The output shows numbered commands from your session history ?

1 perl -v
2 sudo apt update
3 cal
4 ls -l
5 curl -s https://ipvigilante.com/122.175.62.177
6 curl -s https://ipvigilante.com/104.80.62.56
7 pwd

Using Arrow Keys

The simplest method is using the Up and Down arrow keys to navigate through your command history. Press Up to go to previous commands and Down to move forward through the history.

Using !! (Double Bang)

The !! command executes the most recently run command ?

ls -l
!!

This re-runs the ls -l command ?

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

Using ! with Command Prefix

You can execute a specific command from history by typing ! followed by the beginning of the command. This searches backwards and executes the most recent command starting with those characters ?

!nsl

This executes the most recent command starting with "nsl" ?

nslookup www.oracle.com
Server: 127.0.1.1
Address: 127.0.1.1#53

Non-authoritative answer:
www.oracle.com canonical name = ds-www.oracle.com.edgekey.net.
Name: e870.dscx.akamaiedge.net
Address: 104.80.62.56

Using ! with Line Numbers

Execute a specific command by its line number from history ?

!5

This executes command number 5 from the history list.

Common History Shortcuts

Command Description
!! Execute last command
!n Execute command line n
!string Execute most recent command starting with string
!?string Execute most recent command containing string

Conclusion

Linux provides multiple efficient ways to re-execute commands: arrow keys for browsing, !! for the last command, and ! with prefixes for specific commands. These shortcuts significantly speed up command line work and reduce typing errors.

Updated on: 2026-03-15T17:25:40+05:30

354 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements