Using Bash History More Efficiently
If you've used bash for a while you probably know that the commands you enter are saved in the file ~/.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.
However, there are more effective ways to use bash's history: you can use Ctrl+R (Control key held down at the same time as the R key). This will display the following in your shell:
If you know type some substring found in the command you're searching for, for example "ls", bash will search for matching commands. For example, it might show:
(reverse-i-search)`ls': lsof -nP -p 3930
What it actually shows is going to be dependent on the commands you've previously entered.
When you do this, bash looks for the last command that you entered that contains the substring "ls", in my case that was "lsof ...". 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 substring you enter is searched for throughout the command, not just at the beginning of the command.
Practical books for the most technical people on the planet. Newly available books include:
- Agile Product Development by Ted Schmidt
- Improve Business Processes with an Enterprise Job Scheduler by Mike Diehl
- Finding Your Way: Mapping Your Network to Improve Manageability by Bill Childers
- DIY Commerce Site by Reven Lerner
Plus many more.
- diff -u: What's New in Kernel Development
- Server Hardening
- Giving Silos Their Due
- What's New in 3D Printing, Part III: the Software
- 22 Years of Linux Journal on One DVD - Now Available
- Controversy at the Linux Foundation
- Don't Burn Your Android Yet
- February 2016 Issue of Linux Journal
- Firefox OS