Tech stuff and info dump

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


Filed under: Command line,linux
May 31st, 2010 11:43:58

linux: find out what you’ve recently typed at the command line

May 31st, 2010

If you open a terminal window (in Ubuntu 10.04, go to Applications->Accessories->Terminal) and type

history

this will print out a list of things that have been recently typed at the command line.

If you’re curious, the history is stored in a file called .bash_history which you can view as a file. In Ubuntu, for example, you can open it with gedit by opening a terminal window and typing

gedit .bash_history

at the command line.

Note: if you have two (or more) terminal windows open and are typing commands in all of them, the history of one terminal window won’t know about what you’ve typed in the other window. If you close, say terminal window A, leave terminal window B open. and then open terminal window C, typing history in terminal window C will display what you’ve typed in A but not in B.

Examples of using grep to search through the history are here.


Filed under: Command line,linux
May 31st, 2010 11:43:53