Sunday, November 20, 2016

searching bash history

Something I didn't know about until recently: For a reverse search of the commands in bash history, press the ctrl+R key combination. Pressing ctrl+R again takes you to the previous command in bash history. "Enter" to run the command. ctrl+G quits the search.

Perhaps even more useful, use grep to find each command in bash history that includes a given string. For example:

$ history | grep pacman

Or better yet, if you need to quote a string:

$ history | grep 'sudo pacman -S'

Then you can run the desired command from the resulting list by using this simple trick:

!n: Expand to command with history number "n"

For example, the following will find command No. 70 in my bash history (see screenshot, above) and run sudo pacman -Syu:

$ !70

No comments: