Quick tip: searching your shell history

My boss recently shared a great tip for performing a reverse search of your shell history. The next time you’re trying to remember a command you recently typed in terminal. Try the key stroke combination of Ctrl + r.

Typing Ctrl + r in your terminal will prompt you for a search string, and the first result it matches in your history will be returned. If the match is not correct, you can add more characters to make the search more specific, or type Ctrl + r again, and the next valid match will be displayed.

Note: The filtered search results may include the string in any portion of a history log entry, and are not limited to the beginning characters of each log entry. This also means that if multiple matches exists in a single log entry, Ctrl + r will move the search cursor to the next match in the same entry.

Quick Tip: Exploring Your Bash History

Typically when you type a shell command its recorded in the bash history. If you ever needed to see what you typed in the past, a useful tool is the history command. By typing history in the shell it will display a list of commands (commonly the last 500) in the shell.

Example:
user@computer:~$ history
1 ls
2 mkdir testdir
3 cd testdir/
4 vi testdoc
5 ls
6 cd ..
7 rm -r testdir/
8 history

One of the benefits of the history command is if there is a command in your history you wish to execute again. You can do so by typing !<history number>.

Example:
Using the history list above, typing !2 would execute the command mkdir testdir again.