Search Bash History

history | grep ssh

To see the most recent commands:

history 7

Executing Commands from Bash History

Record 89 in your history

!89

Two commands ago

!-2

Rerun the previous command

!!

Rerun the previous command as sudo

sudo !!

Run the previous command and make a substitution

!!:s/WORD1/WORD2

Update your record keeping

Open your ~/.bashrc file and change or add these settings:

HISTSIZE=5000
HISTFILESIZE=10000
shopt -s histappend
alias h="history"

This will keep the last 5000 commands in memory, and the last 10000 lines on disk. It will also append to the Bash history file instead of overwriting it. The new h alias means we can just type h | grep ssh intead of using the entire command.

Make searching more intuitive

History searching with arrow keys

"\e[B": history-search-forward
"\e[A": history-search-backward

Shortcut for grepping

Grep for a word or phrase and remove previous instances of searching

alias h="history | grep -E -v '^ *[0-9]+ *h ' | grep "

Use an additional tool

https://github.com/dvorka/hstr

Perhaps a way to keep EVERYTHING

HISTFILE="${HOME}/.history/$(date -u +%Y/%m/%d.%H.%M.%S)_${HOSTNAME_SHORT}_$$"