Anchor Ad Example

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

Piping in the Linux command line works on every Linux distribution, which means all you need is a running instance of any Linux distribution.

The syntax of a piped command looks like this:

Command 1 | Command 2 | Command 3

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

Let’s first talk about the commands we’ll pipe together.

1. Create a new file

Before we actually get to the piping, I’ll first demonstrate the creation of a new file that contains a list of colors. Create the file with the command:

nano colors.txt

In that file, paste the following:

Orange
Yellow
Red
Blue
Green
Purple
Black
Pink

Save and close the file.

2. Use the sort command

We’re going to use the sort command to list the content in the colors.text file in alphabetical order. 

We can do that with the command:

sort colors.text

The output should look like this:

Black
Blue
Green
Orange
Pink
Purple
Red
Yellow

Instead of running these commands separately, we can use a combination of && and | to do this on one line. I’ve already discussed how to combine Linux commands for a more efficient experience using the && characters. We’ll make use of that technique with our piping command.

3. Creating and adding content to the new file with a single command

First, we’re going to create our colors.txt file and add the content with two commands that will be joined together with &&, like so:

touch colors.txt && echo -e "Orange\nYellow\nRed\nBlue\nGreen\nPurple\nBlack\nPink" >> colors

What you see above uses the -e option to inform the echo command to interpret escape sequences. In this case, the \n escape sequence creates a new line after each color. 

Before that, we use the touch command to create the new file. So, the file is created and then content is added, one line at a time.

4. Pipe it all together

Before we do anything, make sure to delete the current colors.txt file with the command rm colors.txt

What we’ll do now is add our first two commands and then use the cat and sort commands via piping. Cat reads content of a file to the terminal, and sort will sort the output. All together, this command will look like this:

touch colors.txt && echo -e "Orange\nYellow\nRed\nBlue\nGreen\nPurple\nBlack\nPink" >> colors.txt && cat colors.txt | sort

The output of the above command should look like this:

Black
Blue
Green
Orange
Pink
Purple
Red
Yellow

One thing to keep in mind here is the only piping done in the above command is cat colors.txt | sort. The output of the cat colors.txt command is sent to the sort command, which displays the results. 

And that’s how piping works with the Linux command line. This is a very handy trick you can use to make working with the command line even more efficient.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Prev
Best Free Google Chrome VPN Extension

Best Free Google Chrome VPN Extension

Unblock any website and stream TV shows, movies and music in one click with the

Next
Podcasting Technology: What is it?

Podcasting Technology: What is it?

Podcasts are for use on handheld audio/video equipment and computers