grep: how to use grep to search history (linux)
May 31st, 2010
I often remember only part of something I did before and want to remember how I’ve done something. To easily search through the history, the glorious grep
can be invoked like this:
history | grep phrase_to_search_for
If the phrase to search for involves spaces or special characters, then quotes can be used around the phrase:
history | grep ‘phrase to search for’
To search for something with quotes, you can surround your phrase-to-search-for with the ‘other’ kind of quote.
For example,
history | grep ‘phrase to “search” for’
will match
phrase to “search” for
and
history | grep “phrase to ‘search’ for”
will match
phrase to ‘search’ for
TechStuff » linux: find out what you’ve recently typed at the command line
May 31, 2010
[…] of using grep to search through the history are here. Filed under: Command line, linux Tags: Command line, history, linuxx May 31st, 2010 […]
dromichaetes
December 8, 2011
I think there is a better way, instead of calling two processes with the pipeline, you could do:
‘grep -w “word to search” $HISTFILE’
-=GT=-
November 18, 2012
@dromichaetes
I like your string using $HISTFILE, but one drawback is $HISTFILE usually doesn’t include the current session. On my machine it’s only written when a user logs out.
So if you want history info including the current and past sessions, you have to use:
history | grep ‘phrase to search for’
Gael Lafond
February 5, 2013
I use this command all the time, it’s extremely useful. In fact, I even created an alias in my .bashrc to make it even easier to use:
alias hgrep=’history | grep’
I just have to call: hgrep “phrase to ‘search’ for”
@dromichaetes I was doing something similar before, but this do not return the same thing since the history file is saved only when the shell terminate. The command history is better, it even return current results. Btw, pipeline are very efficient, I don’t see any reason why someone would try to avoid them in this context.
Frank Taylor
March 16, 2013
ctrl-r and just type until you see your command, then hit enter to execute.
0. ! is the same as history | grep
Use the up arrow to view the previous command and press enter to execute it.
Type !! and press enter from the command line
Type !-1 and press enter from the command line.
Press Control+P will display the previous command, press enter to execute it