Finding an old command in the shell history
Solution 1
History is what you are looking for.
Run 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.
The 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_history
or, e.g.gedit ~/.bash_history
- Opening Nautilus, Ctrl+h, and searching for the
.bash_history
file.
Both history
and ~/.bash_history
behavior can be changed by adding or modifying the options in ~/.bashrc
, these are a few examples:
Append
export HISTCONTROL=ignoreboth:erasedups
or modify the existingHISTCONTROL
line, to remove duplicate commands, i.e. if one runsecho Hello
immediately afterecho Hello
, only one will appear inbash_history
.Modify
HISTSIZE=1000
to extend or reduce the amount of commands shown withhistory
Modify
HISTFILESIZE=2000
to extend or reduce the amount of commands stored inbash_history
. SetHISTFILESIZE=-1
for unlimited.Append
HISTFILE=/path/to/file
to save the history somewhere else.
Sources and further reading:
Credit to comments from Byte Commander
Solution 2
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
(reverse-i-search)
mode.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.
grep
, likehistory | grep 'appengine'
.More info about the
history
built-in command of Bash can be found by typinghelp history
. -
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.
Simply type
!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
!?string
instead.Or if you want the last command-line containing "string" anywhere, type
!?string?
.More info about history bang expansion can be found by typing
man history
.
Related videos on Youtube
saviour123
Updated on September 18, 2022Comments
-
saviour123 over 1 year
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.
-
George Udosen about 7 yearsRun
history
in terminal or if u remember some parts of the commandhistory | grep <command-parts>
, but note if you have made other commands and ure history settings in.bashrc
is not large then you might never see it. -
Rick about 7 yearsNot sure it's worth a full answer... but you can also hit the ~/.bash_history file in your editor of choice ie:
vim ~/.bash_history
and search inside the file/editor. -
wjandrea about 7 years
-
-
Byte Commander about 7 years
grep apt*
does not what you think it does. First, theapt*
will be processed by Bash's filename globbing, so if you have any files in your current directory that start with "apt", the glob will be replaced with them andgrep
would only search for those full filenames. To prevent globbing, wrap the pattern in double- or better single-quotes:grep 'apt*'
. -
Byte Commander about 7 yearsBut this is still not what you want, because
grep
treats the pattern as regular expression, and in regular expressions, a*
means "match the previous character any time, including zero" - so the pattern would match "ap", "apt", "aptt", "apttt" etc. You could writegrep 'apt.*'
, because the.
in a regular expression matches any character, but this is superfluous becausegrep
does not anchor your pattern to either end of the string, so just writegrep 'apt'
and you will be good. -
Byte Commander about 7 yearsNote also please that
~/.bash_history
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. -
Byte Commander about 7 yearsHowever, enough bashing for today. Your answer is nice other than those few points and I see you already started editing it to fix them. So have a +1 and hopefully you learned something today :)
-
M. Becerra about 7 yearsYes I am learning indeed, thanks a lot for taking that time to explain. Just added the extra info :)
-
DK Bose about 7 years@ByteCommander, re.
grep apt*
without any quotes, I made a small text file with the following strings, one per line: ap appointment appearance apt aptly aptitude aptness . Then I rangrep apt*
on that file. The only hits were (one word per line): apt aptly aptitude aptness -
Byte Commander about 7 years@DKBose Could you possibly have had any file called
apt
in your current directory? -
saviour123 about 7 yearsWow.. Tot I ask a simple question but am wow by the quality of answers.. And details about history and bash_history files.. Reverse search etc... Clearly written essays.. God bless you guys.. This has been the best responses I have ever received
-
saviour123 about 7 yearsWow.. Tot I ask a simple question but am wow by the quality of answers.. And details about history and bash_history files.. Reverse search etc... Clearly written essays.. God bless you guys.. This has been the best responses I have ever received
-
Byte Commander about 7 yearsAlways glad to help :-)
-
DK Bose about 7 years@ByteCommander, yes! That was the name I gave that small file! I will try again with another name.
-
DK Bose about 7 years@ByteCommander, I did the same thing in an empty folder but this time calling the file
test.txt
and got exactly the results withgrep apt*
,'grep apt*'
and"grep apt*"
as you predicted. Thanks! -
Rick about 7 yearsIt's not so much as "grep command_to_search" as it is "grep text_to_find" - it should search more than just "commands".
history | grep zcvf
will findtar -zcvf file.txt
. You could also throw the~/.bash_history
file into an editor and find the commands that way.