14 December 2025

On Ubuntu, how to run one of the commands I ran before again?

To rerun a command from your history, use ! followed by its number (e.g., !123), or use !! for the very last command, !-2 for the second-to-last, or press Ctrl+r and type part of the command to search for it, then hit Enter. 
By History Number
  1. View History: Type history and press Enter to see a numbered list.
  2. Re-run: Type ! then the number of the command (e.g., !567 to run command #567). 
bash
# Example:
history
# ... (list of commands with numbers) ...
!123 # Runs the 123rd command
Shortcuts
  • !!: Runs the immediately preceding command.
  • !-2: Runs the second-to-last command.
  • !string: Runs the most recent command starting with string (e.g., !ls runs the last ls command). 
Reverse Search (Ctrl+r) 
  • Press Ctrl+r to enter interactive search mode.
  • Type part of the command you're looking for (e.g., ssh).
  • Press Ctrl+r again to cycle through older matches.
  • Press Enter to run the command, or Esc to exit search without running. 
  • If you have cycled through some older matches, now you want to cycle
    through some new 
    matches, you can add the following line to your  ~/.bashrc file:
stty -ixon
Starting now, you can press   Ctrl+s to cycle through new matches.


No comments:

Post a Comment