How to use the pipe in Linux commands

Find out how to take your Linux command fu to the next level with pipes.

How to use the pipe in Linux commands Find out how to take your Linux command fu to the next level with pipes.

There are many reasons why the Linux command line interface holds such an inherent flexibility. One reason is that, with ease, you can make two or more commands work together seamlessly. One such example of this is the pipe. 

What is the pipe? 

The pipe is that vertical bar on your keyboard that usually lives on the same key as the backslash. But what does the pipe do? 

Simply put, it takes the output of whatever command is on the left and uses it as the input of the command on the right. To make this simple, I'm going to show you how to create a new file, add text to that file, and search for a string in that file, all from a single command using two pipes. 

SEE: How to find files in Linux with grep: 10 examples (free PDF) (TechRepublic)

Individually these commands would be touch test, echo "this is my file" > test, and grep file test. Those three commands create a file named test, add the string "this is my test" to test, and then searches test for the string "file." 

How do you run the commands with pipes?

That single command would be: 

touch test | echo "this is my file" > test | grep file test

What happens with this is, the first pipe takes the output of touch test and sends it to the second command as input, which means the second command now has the necessary file available to echo text into. The second pipe then takes the output of the second command and uses it as input for the third command, which means the necessary string of text is available to search. 

So the output of the first command is used as the input of the second command and the output of the second command is used as the input of the third command. 

Using the pipe in Linux commands is a very simple way to make the CLI incredibly versatile, as well as efficient. Get used to using the pipe and take your Linux command fu to the next level.

Also see

linuxadminhero.jpg

Image: Jack Wallen