Data - Technology - Leadership

On The Line - History Lesson

Andrew McGee - 27 May 2024
Command Line

Lost In Space Splash

We can learn so much from history and our command history in the shell is no exception. For those infrequent commands with lots of switches I can never remember I will turn to command history to resurface them and bring them back into the light.

Everyone knows the quickest way to retrieve previous input in bash is the up cursor key. Keep pressing up to walk back through the history. Down walks forward.

If you want to see your previous commands all together use the history command to see them in order (last is most recent). Let's pipe it through less to get a screen at a time.

history | less

Remember in less the spacebar scrolls a screen forward and the B key scrolls a screen back. Q key to quit.

Your bash command history is stored in ~/.bash_history. You can cat this file and see all the commands in order without the dates.

cat ~/.bash_history | less
Grep the History

I have a couple of alternating borg backup scripts I like to run so I regularly use histroy to remind me which one I ran last time.

history | grep borg

If you get a lot of results you can pipe the output from grep through less again.

history | grep borg | less

Or pipe it through tail to just get the last n results.

history | grep borg | tail -n 5

This is great and easy for simple one word filters but you can get more sophisticated with your grep of course and use regex. Or you could use...

Ctl + R

Pressing ctrl + r gives you a more powerful interactive search through your command history. Say you have a very long directory path you need to cd to several times and don't want to type it out each time. You could hit the up arrow but if you have a lot of commands in between you will have to keep walking back to find the right cd command.

Press ctrl + r and then start typing your command. You will see the suggestions appear and change as you keep typing and narrow the search filter. You can stop typing and press ctrl + r continuously to cycle through your matched results.

Once you find your command don't hit enter yet - that will execute the command. Use your left and right cursor to edit the line if needed before submitting.

Hack your .bashrc

Finally another way to conveniently search your history is to modify your up and down cursors to search your history to match what's on your command line. We can do this with bind in our .bashrc file.

Edit your .bashrc file:

vim ~/.bashrc

Add these lines that use bind to change your up and down cursor keys:

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

Save the file and then refresh your bash with:

source ~/.bashrc

Now for example if you wanted to walk back through all your commands that begin with 'sudo' you can type 'sudo' on the command line and press the up and down keys.

Incidentally, if you need to find the character sequence for keys such as the cursors or function keys you can press ctrl + v on the command line and then press the key.

If I press the down cursor key I get ^[[B. The ^[ is ESC and the characters following correspond to the key you pressed. So to bind to the down cursor key I can use \e[B

So I hope you enjoyed this history lesson about bash command history and how we can take advantage of it.