How to pipe the output of one command to another in Linux

Getty Images/Zorika Nastasic

We could spend the rest of our days together Linux and never touch command line, The reason I still choose to use Terminal is not only because it is efficient, but because it has a lot of very useful tricks that you can use. One such trick is called “piping”.

In effect, piping takes the output of the first command and uses it in the next command. You can do this with as much piping as you like. You can pipe the output of command A to command B, then pipe the output of command B to command C, then pipe the output of command C to command D, and so on.

again: 8 things you can do on Linux that you can’t on MacOS or Windows

Piping sends data from one command and uses that data in the next command. And the transmission continues until the last command is executed, data always flows from right to left. This is important information and data piping flow is always one-way.

How to use piping in Linux

Things necessary: Piping in the Linux command line works on all Linux distributions. All you need is a running instance of any Linux distribution.

again: How to choose the Linux desktop distribution that’s right for you

The syntax of the piped command looks like this:

Command 1 | Command 2 | Command 3

The | character indicates piping, and bash reads it as such.

First, let’s talk about commands that connect with pipes.

Before we actually get to the plumbing, we’ll first show you how to create a new file containing a list of colors. Create the file with the following command:

Paste the following content into that file:


Save and close the file.

The output looks like this:


You can combine && and | instead of running these commands individually. To do this in one line.already discussed How to combine Linux commands for a more efficient experience Use && characters. Use this technique with pipe commands.

First, create a colors.txt file and add content with two commands joined by && like this:

touch colors.txt && echo -e "OrangenYellownRednBluenGreennPurplenBlacknPink" >> colors

Above, using the -e option, echo A command that interprets escape sequences. In this case, the n escape sequence creates a new line after each color.

again: Want to save your aging computer? Try these 5 Linux distributions

before that, touch Command to create a new file. So the file is created and the content is added to her one line at a time.

Before doing anything make sure to delete the current colors.txt file with the following command: rm color.txt.

again: The best laptops for all types of people and budgets

What we do here is add the first two commands and use the cat and sort commands via a pipe. Cat reads the contents of the file into the terminal and sort sorts the output. In summary, this command looks like this:

touch colors.txt && echo -e "OrangenYellownRednBluenGreennPurplenBlacknPink" >> colors.txt && cat colors.txt | sort

The output of the above command will be:


One thing to keep in mind here is that the command above only does one pipe: Cat color.txt | Sorting.the output of cat color.txt The command is sent to the sort command and the results are displayed.

again: How to enable Linux on your Chromebook (and why you should)

This is how piping works on the Linux command line. This is a very useful technique that can be used to make working with the command line even more efficient.

https://www.zdnet.com/article/how-to-pipe-the-output-of-one-command-to-another-in-linux/#ftag=RSSbaffb68 How to pipe the output of one command to another in Linux

Show More
Back to top button