Search your command history in Linux

Share:  facebook  twitter  reddit  digg  stumble  

Push Ctrl+R (Control key held down at the same time as the R key). This will display the following in your shell:


Type part of the command you're searching for, for example “ls”, bash will search for matching commands. For example, it might show:

(reverse-i-search)`ls': ls – /home/bob/

What it actually shows is going to be dependent on the commands you've previously entered.

Control + R again to cycle through the list.

When you do this, bash looks for the last command that you entered that contains the substring “ls”, in my case that was “ls …”. If the command that bash finds is what you're looking for, just hit Enter to execute it. You can also edit the command to suit your current needs before executing it (use the left and right arrow keys to move through it). If you're looking for a different command, hit Ctrl+R again to find a matching command further back in the command history. You can also continue to type a longer substring to refine the search, since searching is incremental.

Note that the sub string you enter is searched for throughout the command, not just at the beginning of the command.


The predeeding “.” indicates that it a hidden file.

If you've used bash for a while you might know that the commands you enter are saved in the ~/.bash_history when you log out. Next time you log in, bash automatically loads these history commands from the saved file and you can then use the up and down arrow keys to browse your command history and find the command you want rather than re-entering it.

DateJuly 13, 2009