Combining Linux Bash Commands

Introduction

In few previous posts on Ubuntu and Linux commands, we saw few of the useful commands. In this post, we will learn few more Linux commands and also learn how to pipe these commands together to build a powerful customized tool from small simple building blocks.

But before we start combining/piping commands together, lets see some of the individual command first:

apt-get

apt-get (advance packaging tool) is the default package manager for Ubuntu and Debian.

The following command installs vim:

In similar fashion we can uninstall it:

wget

wget is a tool to download files from the Internet. For example, we can download the the Linux kernel tar archive as shown below:

wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.17.2.tar.xz

tar / unzip

we can use tar or unzip to extract files:

unzip thatfile.zip
tar -xzvf anotherfile.tar.gz

vi/vim or nano

These are text editors you can use:

apt-get install vim
vim sometext.txt

apt-get install nano
nano sometext.txt

You can learn more about vim using this interactive tutorial

Some other Commands

history

Shows history of commands.

cal

shows calendar:

Combining Bash Commands

There is lot of power in individual commands, but when you combine commands you can do much more. Linux is inspired by UNIX philosophy and commands do one thing and play nicely with others. This is a general idea of many good system designs but it is very obvious in Unix and Linux.

chmod command is a good example, it only does one thing. It’s not a swiss army knife that lets you rename a file and compress it at the same time as changing permissions, all it does is change permissions. The find command locate files and that’s all it does.

But if you want to change the permissions for set of files, you can take the output from find and pass it into chmod, so in one line you can change the permissions for a whole set of files.

You pass the output of one command as input to another command using the pipe | character.

In the command shown above:

  • find command is used to find all the files in folder1 directory.
  • print0 bit says to format the output in a neat way for the next command.
  • xargs is way to break the large input. The output of find could return thousands of files and input could be too large for the next command. So xargs take the output and executes the next command (sudo chmod a+r) once for each item in the output.

So, the first command gives us a list of files and second command change permissions for every item in the list.

Combining Commands (cat and grep)

Linux writes any important system events to the files called syslog in /var/log directory and we can list them using following command:

ls -l /var/log/syslog*

Lets combine the text of all the log files in one long string:

find /var/log/syslog* -print0 | xargs -0 cat

when executed this will output a lot of text on the terminal window:

We can also pipe this output to another command e.g. grep which look for matching lines in the input command:

find /var/log/syslog* -print0 | xargs -0 cat | grep "NetworkManager"

By using -c flag, I can get the count of these occurances:

find /var/log/syslog* -print0 | xargs -0 cat | grep -c "NetworkManager"

Combining Commands (grep and cut)

Piping command together lets you trim down the output from one command to isolate just the information that you want.

The ifconfig command tells you about network interfaces on your machine.

Here is a lot of information, but if I am only interested in IP addresses, I can do this as follows:

 ifconfig | grep "inet addr"

If I only want the first line, then we can further pipe it to head command which reads lines from top of the file with an option of -n 1, meaning just read the first line:

ifconfig | grep "inet addr" | head -n 1

If, all I want is the actual IP address, I can use cut command which splits up a line into words.

So, I can cut by colon character and just return the 2nd match, which is my IP address.

ifconfig | grep "inet addr" | head -n 1 | cut -d ":" -f 2

But there is some extra content there, so I can cut again by space character and returns the first match as shown below:

ifconfig | grep "inet addr" | head -n 1 | cut -d ":" -f 2 | cut -d " " -f 1

This looks like a long, complicated command, but actually it’s just lots of small command piped together, incrementally transforming the data to desired output.

Summary

Piping commands is easy and you can do very useful interesting things with these combinations. When you are piping commands together, its good to build them gradually. Let me know if you have some questions or comments. If you haven’t already, check some related articles in the section below. Till Next Time, Happy Coding.

Related Links

My Recent Books