How can I find an old command I ran in my terminal?
I used an appengine command and wish to just find it in my local command history without researching it online again.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
History is what you are looking for.
history to get a list of the last 1000 commands, or run:
history | grep command_to_search
to search some pattern within the history generated list, for example:
history | grep apt
To search any
apt related command, note that
grep does not anchor your pattern to either end of the string, so no wildcards needed in most cases.
history list is generated from the last 1000 commands (by default) stored in
~/.bash_history (which stores the last 2000 by default), and such file gets only updated whenever you exit your Bash session cleanly. That means, all commands from your current session will not be in that file until you close your terminal. They will also not be written if you kill the shell process.
Therefore, the command history (2000 last commands) can also be found at
~/.bash_history, by either:
less ~/.bash_historyor, e.g.
~/.bash_history behavior can be changed by adding or modifying the options in
~/.bashrc, these are a few examples:
export HISTCONTROL=ignoreboth:erasedups or modify the existing
HISTCONTROL line, to remove duplicate commands, i.e. if one runs
echo Hello immediately after
echo Hello, only one will appear in
HISTSIZE=1000 to extend or reduce the amount of commands shown with
HISTFILESIZE=2000 to extend or reduce the amount of commands stored in
HISTFILESIZE=-1 for unlimited.
HISTFILE=/path/to/file to save the history somewhere else.
Sources and further reading:
There are many ways to find an recently executed command.
The most simple one is to just hit the ↑ key and cycle through your command history line by line until you spot what you looked for.
You can also press Ctrl+R to enter the so-called
It is a search prompt that will automatically complete what you start to type with the most recently run command that contains this string. When it shows what you looked for, press Enter to run it, or Esc to exit the search prompt while keeping the command on the prompt, so that you can edit it. To discard the result and exit search, hit Ctrl+C.
You can use the
history Bash built-in to show the complete list of recorded commands from your history.
You can filter that list for lines matching a specific pattern using e.g.
history | grep 'appengine'.
More info about the
history built-in command of Bash can be found by typing
Use bang-expansion to directly run the most recently executed command containing a string. This will replace the line you typed with the matching line from history and run it immediately, without confirmation, so be careful.
!string and it will replace that with the most recent command-line that started with "string".
If you want to run the last command that ended with "string", type
Or if you want the last command-line containing "string" anywhere, type
More info about history bang expansion can be found by typing