How to use the pipe in Linux commands

Discover how to take your Linux commandfu to the next level with pipes.

How to use the pipe in Linux commands
Discover how to take your Linux commandfu to the next level with pipes.

There are many reasons why the Linux command line interface offers such inherent flexibility. One reason is that you can easily let two or more assignments work together seamlessly. An example of this is the tube.

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 the command 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 two-pipe command.

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

These commands would be a touch test separately, echo “this is my file”> test and grep file test. Those three commands create a file called test, add the string “this is my test” to test, and then search for the string “file” in the test.

How do you execute 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 that the first pipe performs the touch test output and sends it to the second command as input, meaning that the second command now has the required file available to echo text. The second pipe then takes the output from the second command and uses it as input for the third command, which means that the necessary text string is available to search.

Thus, 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 and efficient. Get used to using the pipe and take your Linux command fu to the next level.

Open source weekly newsletter

You do not want to miss our tips, tutorials and comments about the Linux operating system and open source applications.
Delivered Tuesday

Register today

Also see

Image: Jack Wallen

Similar Posts

Leave a Reply