Eren Akbulut's Blog

Unix Pipes Explained

January 16th, 2021

Hello everyone today I'll try to cover Unix pipes briefly. In my opinion pipes are one of the most important features of Unix systems and if you are working on a Unix system knowing at least the basics of pipes is always wonderful.

So let's dive right in.


What is a Linux Pipe?

A pipe is basically a redirector that sends the output of an application to input of another application. In Unix Like operating systems pipes work unidirectional, which means you can only use a pipe to channel output to one direction.

The part that makes pipes such valuable tools is that some commands of any sort of Unix applications doesn't always put their output into a file and for those cases if we don't have a tool like a pipe we should first create a file with the output of first application and then read and write that file into another applications input, as you might guess or can visualize in your heads after seeing the image below, that kind of approach could cause major problems while trying to connect multiple applications in a output-input order.

unix-pipes-basic

I know that the explaining pipes in such a way might be a little abstract for the ones who has zero familiarity, so I'll try to demonstrate the use of pipes over examples. I'll try to use the most known commands to keep you on the topic.


Examples

unix-pipe-lswc
  • Now we have 2 seperate commands above, the first one is "ls" shows every visible file or folder we have in a directory and the other one is "wc" is a Unix application that can count the specific features of an input according the given flags. As we can see above when we piped ls's output to wc's input our new output is the number of things "ls" found in our current directory.

unix-pipe-catwc
  • In our second case we using wc again but with a cat input instead, when we check our test3 file we see that we have 3 empty and 3 non empty lines in it, and when we pipe that output into "wc" command it'll give us the number 6 as expected.

unix-pipes-psgrep
  • In this example we have 2 new commands I assume you know what those 2 do also but if you don't you can quickly check them out from here and here. If you know what those commands are, you can easily guess that the example above outputs all the process in my system with contains the word "docker"

unix-pipe-comunix-pipe-comp-cat
  • Now as we can see above when we combined 2 outputs with 2 inputs by using 2 pipes, with this combination we are creating a new .txt file with the from the test3 file by only using the line that contains string "This file"


Alright, that is it for today. I think there are still much more to explain about pipes but I think this content is enough as a introductory level tutorial.

I hope you enjoyed reading it, see you next time. :)

This blog has been created by Eren Akbulut 2020