Linux Terminal 101: How To Use History

Today we're learning and using the history command.

All of your old commands are stored in this file called .bash_history, which you can search via the terminal. We're going to use lots of tools we've learned up til now to really get into the history.

First off, check out that file with: history | less

This will store the last 500 commands. If you want to check a specific listing, you can change this with grep: history | grep /usr/bin

In this example, the number at the beginning is where it falls in the history list. To shorten this line, you can type !thenumber. Bash expands this line item for you.

To search incrementally, which kind of reminds me of a Google search, you can hit CTRL-R, then type in your search query. For /usr/bin we get a result. You can use this result by pressing enter, or copy with CTRL-J. You can also search farther back in history with CTRL-R. To quit, use CTRL-G or C.

Here are some handy shortcuts for History: CTRL-P, N move to the previous or next entry. ALT->, < move to the beginning or end of the list. CTRL-O can run the current command then move on to the next one.

We're back with history! There are some expansion commands that you can use for history, and they start with a !. Some include: !! to repeat the last command, !number to run that listing item number, !string to repeat a list starting with string, and !?string to repeat a list containing string. Don't use the last two unless you are sure you know what you're doing. Check out the bash man page for history expansion for more!