Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Linux Command Line and Shell Scripting Techniques
  • Toc
  • feedback
Linux Command Line and Shell Scripting Techniques

Linux Command Line and Shell Scripting Techniques

By : Vedran Dakic, Jasmin Redzepagic
4.4 (5)
close
Linux Command Line and Shell Scripting Techniques

Linux Command Line and Shell Scripting Techniques

4.4 (5)
By: Vedran Dakic, Jasmin Redzepagic

Overview of this book

Linux Command Line and Shell Scripting Techniques begins by taking you through the basics of the shell and command-line utilities. You’ll start by exploring shell commands for file, directory, service, package, and process management. Next, you’ll learn about networking - network, firewall and DNS client configuration, ssh, scp, rsync, and vsftpd, as well as some network troubleshooting tools. You’ll also focus on using the command line to find and manipulate text content, via commands such as cut, egrep, and sed. As you progress, you'll learn how to use shell scripting. You’ll understand the basics - input and output, along with various programming concepts such as loops, variables, arguments, functions, and arrays. Later, you’ll learn about shell script interaction and troubleshooting, before covering a wide range of examples of complete shell scripts, varying from network and firewall configuration, through to backup and concepts for creating live environments. This includes examples of performing scripted virtual machine installation and administration, LAMP (Linux, Apache, MySQL, PHP) stack provisioning and bulk user creation for testing environments. By the end of this Linux book, you’ll have gained the knowledge and confidence you need to use shell and command-line scripts.
Table of Contents (19 chapters)
close

Going through the advanced Vi(m) settings

In the first part of this chapter, we learned some basic Vim operations, which were moving around, copying and pasting, saving, and exiting. Let's take care of some more advanced operations, such as working with find and replace, regular expressions, and similar concepts.

Getting ready

We need to leave our CLI virtual machine running. If it's not powered on, we need to power it back on.

How to do it…

Finding content in Vim is a multi-step process, and it depends on a couple of things. First, it depends on the direction that we want to take, forward or backward, as there are different key sequences for these operations. Let's open the /root/words file again to find some text:

Vim /root/words

Let's start by finding the word fast. For that to work, we need to use the / character from normal mode, as it tells Vim that we're about to use the search function. So, /fast will search for the words fast forward from our cursor. This is the expected result:

Figure 2.14 – Finding a word in Vim

Figure 2.14 – Finding a word in Vim

If we now press Enter and then the n key, we will search for the next appearance of the word fast. This is the expected result:

Figure 2.15 – The next appearance of the word fast

Figure 2.15 – The next appearance of the word fast

However, if we want to find the 10th appearance of the word fast, we need to either press the correct key sequence or use a regular expression. Let's start with a key sequence, which is going to be (again from normal mode) 10/fast. This is the expected result:

Figure 2.16 – Finding the n-th appearance of a word

Figure 2.16 – Finding the n-th appearance of a word

If we want to find the previous appearance of our word (basically, search backward), we need to press the N key (capital N). This is the expected result:

Figure 2.17 – Finding a word backward from the previous cursor

Figure 2.17 – Finding a word backward from the previous cursor

Let's now do a bit of search and replace. Let's say that we want to find all appearances of the word airplane and change them to metro, starting from the beginning of our file. The key sequence used for that would be gg (to go back to the file beginning) and then :%s/airplane/metro/g, followed by the Enter key. This is the expected result:

Figure 2.18 – Replacing all appearances of a word with another word

Figure 2.18 – Replacing all appearances of a word with another word

This syntax presumes the automatic replacement of all occurrences of the word airplane with the word metro placed anywhere in the file. If we just wanted to replace the first appearance of a string in any line, we need to first find that word by using the /word key sequence. Then, we need to use the :s/word1/word2/ key sequence to only change the first appearance of word1 with word2. Let's use the word airship for that example and change that word to ship. If we type in /airship, followed by the Enter key, Vim will position us to the first next appearance of the word airship. If we then use the :s/airship/ship/ key sequence followed by the Enter key, we should get this result:

Figure 2.19 – Replacing one appearance of a word in a specific line with another word

Figure 2.19 – Replacing one appearance of a word in a specific line with another word

It's a subtle difference, but an important one.

We could also use many more commands in vi – for example, using a dot sign (.). That can be used to repeat the last change made in normal mode, which you might also find to be very useful.

We're going to stop here, as we will cover more advanced text search patterns by using regular expressions in Chapter 7, Network-Based File Synchronization.

How it works…

String replacement in Vim works by using an external command called sed, a stream editor. This command is regularly used by system engineers all over the world to quickly replace simple or complex text patterns of any given file (or multiple files) to another complex text pattern. It uses regular expressions as a basis (explained in Chapter 7, Network-Based File Synchronization), which means that, by default, doing search and replace in Vim is quite powerful, albeit a bit complex, as we need to learn the ins and outs of sed and the way Vim treats it as a plugin.

That being said, most of us focus on the quite powerful part of the last paragraph, as using a Vim/sed combination to quickly replace complex text patterns yields fast and precise results – as long as we know what we're doing, of course.

There's more…

Using these concepts requires a bit of extra reading. So, we need to make sure that we check the following additional links:

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete