Piping in Linux System
Tags: linux, ubuntu, beginners, computerscience
Published: 2020-06-27 11:42:32

In the previous post, we explored one of the main functionalities of Linux System, **I/O redirection**.

Today we are going to learn how to use and amplify the power of **I/O Redirection** using **Piping**.

What is Piping?

The `Pipe` command connects **STDOUT** of one command to **STDIN** of another command

Piping is what allows us to use multiple commands together i.e use one command then pass on the output to the next command without having to save the output into a file.

The Syntax of pipe 

![piping-syntax](https://dev-to-uploads.s3.amazonaws.com/i/6qofgnko522q67ozohg4.png)

In the above syntax the `|` represents the `pipe command`. The command1 output is provided to command2 as input and command2 output is provided to command3 as input.

The `pipe` command looks very similar to the `>` and `>>` but the difference is that pipe command redirects from one command to another and `>` and `>>` only works with the file.

Let's take an example get a better understanding

- We need just the day from today's date

Let's try to get the day using what we have learned so far.

But first, let's check out the output of date command

![date-output](https://dev-to-uploads.s3.amazonaws.com/i/0efexmktqx5yz6l5h0wc.png)

The date command gives the day, date, and time along with timezone. We need just the day from the above output.

To get only a specific column from the output we will use the `cut` command

![cut-date](https://dev-to-uploads.s3.amazonaws.com/i/rfinvod3tws5oj4shmdc.png)

In the above commands, we first get the date and save it to file.

Then we read the date from file and apply cut command that is used to divide a line of text into columns based on a delimiter in our case 
its **space(" ")**. 

After that, we got the value of field 1 since the day is mentioned in the 1st field.

Now let's try to get the same output using piping

![piping-cut-date](https://dev-to-uploads.s3.amazonaws.com/i/70dmq1l20p7t51r0i1zr.png)

In the above example, we use piping to perform the same action but we performed in a single line.

The above example gives us a little idea of how powerful piping is.

Let's take another example where we first need to save the current date in a file **"currentDate.txt"** then use the file as an input for `cut` command and then save the output of the `cut` command in a file **"currentDay.txt"**.

Okay, this seems like a little too much even with piping this has to take multiple commands. Right?

No, with the use of piping we can get this done in one single line.

To perform this we take help from the **`tee`** command.

The `tee` command is used to save the output to a file.

- When we need to save the date in the file before passing it to cut command

![date-save-cut-save](https://dev-to-uploads.s3.amazonaws.com/i/26n4ze2pglkvxmfo6d30.png)

In the above example, we first call the `date` command to get the current date 
then use the `tee` command to create a T junction which passes the value horizontally to cut command but also passes the data vertically into a file namely **"currentDate.txt"**.

After that, we cut and get the current day and use the `tee` command to save the day in the file **"currentDay.txt"** and pass it along to Standard Output i.e. **terminal**.

To check whether the files were actually saved or not we used the `ls` command to check the directory contents. 

Let's take one last example in which we read a file that contains names of files that needs to be deleted and then delete these files.

Wait, is that even possible using piping?

Let's check it out

-  When we need to remove the files after reading from file

So to use this delete command in our pipeline we will need to convert our standard output into a command argument since the `rm` command only takes command line argument.

To do this we use `xargs` command which is used to convert the standard output into command line arguments.

![delete-files-xargs](https://dev-to-uploads.s3.amazonaws.com/i/zr510pa3kq33zjk4xp25.png)

In the above example, we used the `cat` command to read file contents and then used `|` to pass the output as an input to `rm` command and we used the `xargs` to convert our Standard Output into command line arguments.

In this post, we saw the following new commands

1. `date` - print or set the system date and time.
2. `cut` - remove sections from each line of files.
3. `xargs` - convert our Standard Output into command line arguments.

This was all about Piping in the Linux System. Please, let me know if there are any questions and suggestions on what you want us to explore next.