Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
CentOS System Administration Essentials

You're reading from   CentOS System Administration Essentials Become an efficient CentOS administrator by acquiring real-world knowledge of system setup and configuration

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher Packt
ISBN-13 9781783985920
Length 174 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrew Mallett Andrew Mallett
Author Profile Icon Andrew Mallett
Andrew Mallett
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Taming vi 2. Cold Starts FREE CHAPTER 3. CentOS Filesystems – A Deeper Look 4. YUM – Software Never Looked So Good 5. Herding Cats – Taking Control of Processes 6. Users – Do We Really Want Them? 7. LDAP – A Better Type of User 8. Nginx – Deploying a Performance-centric Web Server 9. Puppet – Now You Are the Puppet Master 10. Security Central 11. Graduation Day Index

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image