Find Some Commands Fast

Q: Locating Previously Used Commands

I was looking at a man page yesterday, but I can't remember the name of the command I was reading about, and I didn't write it down. How do I get the man page back?

A: Searching .bash_history.

Chances are good the command you used is stored away in a file called .bash_history. By default, this file stores away the last 500 commands you typed at the shell prompt.

You can glimpse the history of your commands by typing history at the shell prompt -- but the results will speed by quickly.

Another way you can view .bash_history is with a pager such as less, by typing less .bash_history at the shell prompt. To move forward a screen, press the Space; to move back a screen, press the B key, and to quit, press Q.

But paging through to find a command can be tedious. A useful alternative is to search the file for keywords using grep, a powerful search utility.

If you can recall part of any command you'd typed at the shell prompt, and if that command is still in .bash_history, you can find it fast by piping the results of the history command through the utility called grep.

Let's say you'd been reading the man page the day before, but can't recall its name. To search for the command, then, type:

history | grep man
		

In no time, you'll see all of the commands you'd typed which have the word man in them -- both the relevant and irrelevant.

There are plenty of ways to make use of command history. For other tips and tricks, see the section called Tips on Using Command History.