CLI trickery – shortcuts that you will love
So before we dice into the wonderful world of text editing that is vi, we will warm up with a few exercises on the keyboard. Linux is my passion, as is automation. I am always keen to create scripts to carry out tasks so that those tasks become repeatedly correct. Once the script is created and tested, we will have the knowledge and faith that it will run in the same way every time and we will not make mistakes or miss critical steps, either because it gets boring or we are working late on a Friday night and just want to go home. Scripting itself is just knowing the command line well and being able to use it at its best. This truth remains across all systems that you will work with.
On the command line, we may try a little more black magic by executing the following command:
$ cd dir1 || mkdir dir1 && cd dir1
With this, we have used the cd
command to enter the dir1
directory. The double pipe or vertical bar indicates that we will attempt the next command only if the first command fails. This means that if we fail to switch to the dir1
directory, we will run the mkdir dir1
command to create it. If the directory creation succeeds, we then change into that directory.
Tip
The ||
part denotes that the second command will run only on the failure of the first. The &&
part denotes that the second command will run only if the first command succeeds.
The command history is a little more and hugely better than just an up arrow key! Consider the following commands:
$ mkdir dir1 $ cd !$
The !$
part represents the last argument, so in this way, the second line evaluates to the following:
$ cd dir1
In this way, we can rewrite the initial command sequence, by combining both concepts, to create the following command:
$ cd dir1 || mkdir !$ && cd !$
We can repeat the last command as well as the last argument. More importantly, we can specify the start characters for the last command. If it was merely the last command, then the up arrow key would suffice. If we were working on a web server configuration, we may want to edit the configuration file with vi, start the service, and then test with a command-line browser. We can represent these tasks using the following three commands:
# vi /etc/httpd/conf/httpd.conf # service httpd restart w3m localhost
Having run these three commands in the correct order, hoping for success, we may notice that we still have issues and that we need to start re-editing the configuration file for Apache, the web server. We can now abbreviate the command list to the following:
# !v # !s # !w
The !v
command will rerun the last command in my history that begins with a v
, and likewise with s
and w
. This way, we can appear to be terribly proficient and working really quickly, thus gaining more time to do what really interests us, perhaps a short 9 holes?
In a similar fashion to our first glance at the history using the !$
symbols to represent the last argument, we can use !?73
. This would look for 73
anywhere as an argument or part of an argument. With my current history, this would relate to the date command we ran earlier. Let's take a look:
$ !?73
With my history, the sequence will expand to and run the following command:
$ date --date "73 days ago"
Looking at my command history from the last command run to the first, we search for 73
anywhere as a command argument. We make a note that we exclusively look for 73
, meaning we are looking for the character 7
followed by the character 3
. We have to then bear in mind that we would also match 273
or 733
if they existed in my history.
Having mastered a little of the Bash shell history functions, we should practice to make this second nature.