All these shortcuts and efficiencies today leverage one key utility in the Linux command line world – history. The history command will display a chronological list of every command issued by the current user through the command line.
Each command is preceded with a number – these numbers are the foundation to most of these tips. You can quickly re-issue a command by using the ! or bang character and then the number of the command.
This is nothing fancy and properly something already known to you. The real power with history is around the substitution of arguments or command line parameters. Perhaps only oddly to me, but this situation happens all the time – I want to execute a second command using the same parameter as the first command.
Here I’ve done an ls on a directory and now wish to cd into it. Simple enough, use the !$ substitutor.
Interested in using just the first of multiple parameters as input to a second command – !^ it is.
The history listing can become quite lengthy where it becomes too cumbersome to find the previously executed command you are looking for. In this situation you’ve got a couple options to help locate what you want. It’s actually super simple – just prepend bang with a keyword.
!vi executes the the last vi session utilized.
But lets say you were editing a file yesterday in vi but today it’s proving problematic in locating where exactly that was. No worries, you can perform a reverse search using CTRL+R within the command line. Then begin typing a few characters that match anywhere within the command in question.
Clearly these examples are ultra simplified and on the surface don’t really provide true value or efficiency, but once you come to grips with the basic understand the possibilities will quickly rise to the surface.
In parting, one last tip – it’s possible to have history ignore duplicate commands. In my usage, it’s not hard to accumulate dozens upon dozens of single ls commands. Rather than be inundated with multiple ls listings, history can be instructed to record unique commands only with HISTCONTROL=ignoredups.






















