Every time you type a command into the Linux Bash terminal and press Enter, that command is saved in a hidden file called .bash_history. This is incredibly useful because it means you do not have to memorize long, complex commands. If you typed a massive, 50-character rsync command three days ago, you can simply pull it back up from your history instead of typing it from scratch. While many users know they can press the “Up” arrow key to cycle through their previous commands one by one, this is painfully slow if the command you want was executed 100 lines ago. The most efficient way to find a past command is to use Bash’s built-in “Reverse Search” feature, triggered by the Ctrl+R keyboard shortcut.
Invoking Reverse Search
Reverse search allows you to search your entire history backward in time by typing just a few characters of the command you are looking for.
- Ensure your terminal is at a clean prompt.
- Press and hold the Control key, then press the letter R (
Ctrl+R). - Your standard terminal prompt (e.g.,
user@server:~$) will disappear and be replaced by a special search prompt that looks like this:(reverse-i-search)`':
Searching for a Command
Once the search prompt is open, you simply start typing any part of the command you remember.
- Begin typing the keyword. For example, if you are looking for that old
rsynccommand, simply typersync. - As you type each letter, Bash will instantly search your history file backward and display the most recent command that matches what you have typed so far. The output will look something like this:
(reverse-i-search)`rsync': rsync -avz --progress /var/www/html user@backup-server:/backups/website - If the command displayed is exactly the one you want to run, simply press Enter. The command will execute immediately.
Cycling Through Multiple Matches
Often, the first result Bash finds is not the exact command you are looking for. For instance, if you run sudo apt update every day, typing “apt” into the reverse search will simply pull up your most recent update command, not the specific apt install nginx command you ran three weeks ago.
- Press
Ctrl+Rand type your keyword (e.g., “apt”). - When the wrong command appears on the screen, do not press Enter.
- Instead, press Ctrl+R again.
- Bash will skip the first result and jump further back in time to the next most recent command containing “apt”.
- Keep pressing Ctrl+R repeatedly to cycle backward through your history until you find the exact command you are looking for.
Editing the Found Command
Sometimes you find the old command, but you need to change a minor detail (like changing the destination folder) before you execute it.
- Once the desired command is displayed in the reverse search prompt, do not press Enter.
- Instead, press the Right Arrow key or the Left Arrow key.
- This immediately exits the search mode, dumps the full command onto your standard terminal prompt line, and places your cursor there so you can edit the text.
- Make your necessary edits, and then press Enter to execute the modified command.