Today’s TWEAK is another builtin bash trick – the ability to look back at the
history of your console commands from most recent to oldest.
You may not know it, but bash keeps a record of your commands. You can view up
to the last 1000 commands entered in a console window with the command
history in any terminal screen. Using this option, you’ll see the most
recent command listed last. It’s not a terribly efficient way to look through
your command line history. Usually, when utilizing the history function, you
have a specific command in mind. The rest are irrelevant and, in fact, a bit
cumbersome when shown all in the same screen.
bash, however, has the capability to search for specific commands from the most
recent to the oldest. For instance, if I was to search for the entries in the
bash history related to snort, I’d use the following:
What I’ll see in the console window is:
and whatever I enter, in this case ‘snort’. The most recent snort entry
completes the command line. Using the [Ctrl-R] key combination repeatedly
scrolls backward through the history, bringing up each reference to the string
you’ve entered. Again, in this case, it will show all the different snort
commands. Once you’ve found the command you’re looking for, the right or left
arrow keys will place the command on an actual command line. From there, you can
simply press [enter] to execute the command.
If you’re using the command line at even just a moderate level, you’ll
eventually find the reverse search function of bash to be very useful. I also
found it surprising. When you use the command line a lot, as I do, it doesn’t
take long to accumulate a thousand entries in the history. That makes the
[Ctrl-R] reverse search function even more useful.