How to Search Command History in Linux
Find a command you ran before using history, reverse search and grep.
When you know you ran a command but cannot remember the exact syntax, Bash history is faster than rebuilding it from memory. Search the saved command list first, then inspect the exact command before running it again.
Step by step
1 Search history
$ history | grep "docker" Filters the numbered history list for commands containing docker.
2 Search interactively
$ Ctrl+R Press Ctrl+R and start typing. Press Ctrl+R again to move through older matches.
3 Show recent commands
$ history 20 Displays the last 20 commands from the current shell.
4 Run a command by number
$ !1234 Runs the history entry with that number. Inspect it first when the command can change or delete data.
Tips worth knowing
- Use `history | grep -i "pattern"` when capitalization may differ.
- Do not blindly replay destructive commands from history.
- History settings and retention vary by shell configuration.
Frequently asked questions
How do I search Bash history?
Use `history | grep "text"` or press Ctrl+R for interactive reverse search.
How do I search history without running anything?
Both `history | grep` and Ctrl+R only search; they do not execute a command.
Where is Bash history saved?
Interactive Bash commonly saves it in `~/.bash_history`, subject to shell and history configuration.
The commands behind it
List, search and re-run previous commands — plus Ctrl+R, !! and the tricks that save hours.
grepFind lines matching a pattern in files or piped input — the workhorse of text search.
catPrint file contents, number lines, and join multiple files together.
More how-to guides
See how much space is left, and find out what is using it.
Extract a .tar.gz archiveUnpack tar.gz, tar.bz2, tar.xz and zip archives from the command line.
Free a port that is already in useFind which process holds a port and stop it cleanly.
Find and replace text in filesReplace text in one file or across a whole project, safely.