Even though I could spend the rest of my days with Linux and never touch the command line, I still choose to work with the terminal because it’s not only efficient, but it has a lot of useful tricks that I can use. I can One such trick is called “piping”.
Effectively, piping is taking the output of the first command and using it in the next command. And you can do it with as much piping as you want. You can pipe the output of command A to command B, and then pipe the output of command B to command C, and then pipe the output of command C to command D…etc.
Too: 8 Things You Can Do With Linux That You Can’t With MacOS or Windows
Piping sends data from one command to be used in the next – and this continues until you run the last command, with data always flowing from right to left. This is a significant amount of information, and the flow of data piping is always the same.
how to use piping in linux
what you’ll need: Piping into the Linux command line works on every Linux distribution, which means all you need is a running instance of any Linux distribution.
Also: How to Choose the Right Linux Desktop Distribution for You
The syntax of the piped command looks like this:
Order 1 | Order 2 | order 3
The | The character indicates piping, and bash reads it as such.
Let’s first talk about the commands we’ll be piping together.
Before we actually get to piping, I’ll first demonstrate the creation of a new file containing a list of colors. Create file with command:
In that file, paste the following:
orange
Yellow
Red
Blue
Green
Purple
Black
pink
Save and close the file.
Output should look like this:
Black Blue Green Orange Pink Purple Red Yellow
Instead of running these commands individually, we can use && and | You can use a combination of to do it in one line. I have already discussed how to combine Linux commands for a more efficient experience using && characters. We’ll be using that technique with our piping commands.
First, we’re going to create our colors.txt file and add the content with two commands joined with &&, like this:
touch colors.txt && echo -e "Orange\nYellow\nRed\nBlue\nGreen\nPurple\nBlack\nPink" >> colors
Uses the -e option to indicate what you are seeing above echo Order of interpreting escape sequences. In this case, the \n escape sequence creates a new line after each color.
Too: Do you want to save your aging computer? Try These 5 Linux Distributions
Before that, we use touch command to create a new file. So, the file is created and then content is added, one line at a time.
Before we do anything, make sure to delete the current color.txt file with the command rm colors.txt,
Too: Best laptops for every type of person and budget
Now we’ll add our first two commands and then use the cat and sort commands through piping. Cat reads the contents of a file to the terminal, and sort will sort the output. Overall, this command would 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 note here is that only piping is done in the above command cat color.txt | sort, output of cat color.txt The command is sent to the sort command, which displays the result.
Too: How to Enable Linux on Your Chromebook (And Why You Should)
And piping works similarly with the Linux command line. This is a very handy trick that you can use to make working with the command line even more efficient.











