loading...
Cover image for Piping in Linux System

Piping in Linux System

yashsugandh profile image yash sugandh ・3 min read

linux for begineers (12 Part Series)

1) Linux Navigation For Beginners 2) What are Commands in Linux? 3 ... 10 3) Exploring Linux System 4) Wildcards in Linux System 5) Creating Files and Directories in Linux System 6) Deleting Files and Directories in Linux System 7) How to Copy, Move and Rename Files and Directories in Linux System 8) Locating Files and Directories in Linux System 9) Finding Files and Directories in Linux System 10) I/O Redirection In Linux System 11) Piping in Linux System 12) Commands for Viewing and Sorting Files

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

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

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

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

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

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

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.

linux for begineers (12 Part Series)

1) Linux Navigation For Beginners 2) What are Commands in Linux? 3 ... 10 3) Exploring Linux System 4) Wildcards in Linux System 5) Creating Files and Directories in Linux System 6) Deleting Files and Directories in Linux System 7) How to Copy, Move and Rename Files and Directories in Linux System 8) Locating Files and Directories in Linux System 9) Finding Files and Directories in Linux System 10) I/O Redirection In Linux System 11) Piping in Linux System 12) Commands for Viewing and Sorting Files

Posted on Jun 27 by:

yashsugandh profile

yash sugandh

@yashsugandh

A software engineer trying to figure out how stuff works

Discussion

markdown guide