In this lesson, we will be looking at ways to improve your productivity using your shell. The Linux command line is great because it has a variety of tools we can use. But what makes it even greater is the fact that we can chain those tools together to form greater, more powerful tools that will make us even more productive. We will not get into basic shell commands and instead we will be looking at some cool pipe and sub shell combinations that can make our life easier. Let's start with a basic pipe. For example, counting the length of the current path.
Pwd, vertical bar WC minus C. pwd as you probably know stands for print working directory. The vertical bar is the pipe symbol. And what it does is send the output of the command on the left to the company. On the right, in our case, pwd is sending its output to WC minus C, which counts the number of characters worked out. The cool thing about pipes is that you can create a chain of any number of pipes. Let's do another example for finding used space on a drive.
This is the command Oh my god. Okay, so the first part before the first five gives us the disk usage in a human readable format, that's minus h. One minus h stands for actually. Okay, then after the pipe, it's the grep slash home which shows only the home directory then it's tr, which substitutes multiple spaces for just one space. And the cut command in the end selects the second column using space as the delhi delhi tally meter as the delimiter fund Thank you, English speakers for correcting me in your mind. As you can see, the command printed out 173 gigabytes is the size of the home partition. This is a common use case of chaining when chaining multiple commands, each command reducing the output until we get the desired information and nothing else.
In our case, the use disk space. All right. to count all the directories in a folder we can use the following command. The basic idea is to count all the lines that end in slash. and here we can see we have only one directory pipes are a great option to find and kill processes. Say we want to find the process ID of now Deluce and kill all running instances for this We use discount.
The first part before the pipe prints all processes with bid. The second part, find the ones matching now to lose the third one, inverted the graph. It's an inverted graph to exclude the graph process. The fourth one, it's to select the second word in the line, which is the pin. And the last one xx is used to distribute each beat to a Kill Command. Basically, it is used for commands that do not read arguments from standard input.
And now we've killed notice. This was purely a demonstrative example. There are other ways of doing this. If you want to better understand this command, just write the first part. See what happens then the first part by the second part, see what happens until the full command Now let's open our device again and send it to the background by hitting on reset, followed by the background command. Then we run B grep.
Now to lose to see all the bits of Nautilus and be killed now the DOS to send the kill signal to all those processes. Now it's time for some networking. You probably know the IP config command which is used to print information about the network interfaces to get the IP address of a specific interface. In our case, the wireless interface. run this command the first part Prince networking information from the interface. The second one gets the line with the IP address the third one, select the second word in the line.
We could have also used cut, then the last one cut to only print the second word. And now we see on the screen or private IP address a common Use Case that might also arise is counting the word frequency in a fight. We have here a standard lorem ipsum text contained in file dot txt. In order to get the word frequency, we use the following command. And if you want to appear smart, just try to read it. So the first part we get the file the second one we transform each space into a newline character.
Third, eliminate empty lines, fourths, eliminate the comma and the.to select only the words fifth sword the result alphabetically sixth, only show unique lines and last but yet last actually sorted them by numerical value. Go now a band grep minus w ID to find the frequency of the word ID or grep w minus w for to see all words that appear four times. Now let's move on to our first sub shell example. Sub shells can be written by either enclosing them in dollar parentheses or using backticks. backticks are usually found under the Escape key on your keyboard. And during our examples, we will be using the first form because it's easier to read.
Our first example is listing all the folders in our current folder, LS dollar parentheses, close parentheses, the LS subshell returns the files and folders in the current directory and the LS from outside the sub shell will list those individually showing additional details. Another example is counting all files and directories in the current directory. Given the fact that dot and dot dot are hard links that marked the current and parent directory We need to count all entries minus these two. This can be done with the following command. Here the stop shall return the number of entries. In our case, five.
The number we are looking for is the number of entries minus the special folders dot and dot dot. Notice that the sub shell contains a pipe. The great thing is that we can combine pipes and sub shells in any way in order to obtain the desired result. Imagine pipes and sub shells as Lego pieces for your shelf. they expand way beyond its capabilities and enable you access to new possibilities with infinite combinations. In the end, it all depends on your imagination and how well you learn to use them.