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?
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
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.
pipe command looks very similar to the
>> but the difference is that pipe command redirects from one command to another 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
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
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
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 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
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.
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
date- print or set the system date and time.
cut- remove sections from each line of files.
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.