Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Repeat Your Last Command in Linux?
Linux terminal allows us to execute a variety of commands, and often we need to repeat a previously executed command. There are several efficient methods to recall and re-execute the last command without retyping it completely.
For demonstration purposes, let's assume we previously ran the command ls -ltr and now want to repeat it using various methods available in Linux.
Using Arrow Keys
The most basic approach is to press the UP arrow key on your keyboard. This retrieves the last command from your command history, allowing you to press Enter to execute it again or modify it before execution.
Repeating the Last Command Using Exclamation Marks
The !! command is a powerful bash history expansion that repeats the last executed command:
!!
When you type !! and press Enter, the terminal displays the last command and executes it immediately:
root@server:~# !! ls -ltr total 0 -rw-r--r-- 1 root root 0 Dec 15 10:30 file1.txt -rw-r--r-- 1 root root 0 Dec 15 10:31 file2.txt
Using History Position References
You can reference previous commands by their position in history using !-n where n is the number of commands back:
!-1
This command executes the last command (same as !!). You can use !-2 for the second-to-last command, !-3 for the third-to-last, and so on.
Keyboard Shortcuts
Using Ctrl + P
Press Ctrl + P to navigate backward through your command history. You can press it multiple times to traverse through previously executed commands. Once you find the desired command, press Enter to execute it.
Using Ctrl + R (Reverse Search)
Press Ctrl + R to initiate a reverse search through your command history. Start typing part of the command you want to find, and bash will display matching commands from your history. Press Enter to execute the displayed command.
(reverse-i-search)`ls': ls -ltr
Using the 'fc' Command
The fc (fix command) utility can be used to re-execute the last command:
fc -s
This command automatically executes the last command from your history. The fc command offers additional options for editing and executing commands from history.
Comparison of Methods
| Method | Command/Shortcut | Advantage | Use Case |
|---|---|---|---|
| Arrow Key | UP arrow | Simple and intuitive | Quick access, allows editing |
| Double Exclamation | !! | Fast execution | Immediate re-execution |
| History Position | !-1, !-2 | Access specific positions | When you know exact position |
| Ctrl + P | Ctrl + P | Navigate through history | Browsing multiple commands |
| Ctrl + R | Ctrl + R | Search functionality | Finding specific commands |
| fc Command | fc -s | Advanced history control | Scripting and automation |
Conclusion
Linux provides multiple efficient methods to repeat your last command, from simple arrow keys to advanced history expansion with !! and fc. Choose the method that best fits your workflow arrow keys for quick edits, !! for immediate execution, or Ctrl + R for searching through command history.
