Pipe Unix Tutorial
The UNIX command pipe works like a physical pipe. It connects two UNIX commands and thus facilitates a one-way flow of data. Like a physical pipe, which controls the flow of fluid, the pipe command gives users control of the output of the commands being executed.
-
Basics
-
Similarities between a physical pipe joint and UNIX Pipe command
Just like a physical joint of two pipelines that gives the illusion of one continuous pipe, the pipe command (|) lets two UNIX commands work together. For example:
$ cat abc.txt | wc --l
23The UNIX command "cat" is used to retrieve the contents of a file, while the "wc -l" command does a word count of the input stream of words. The pipe command "|" in this example passes the output of the "cat" of file abc.txt (which lists down the contents of the file abc.txt) to "wc -- l," which performs a word count on the contents. In the example, the result is 23, which means the file abc.txt contains 23 words. If these commands were executed separately, it would be difficult to count the number of words in the file abc.txt. Pipe lets the user achieve this by combining the two commands.
In a physical pipeline joint, the output of one pipe may not be the same as the input for the other pipe, as illustrated in the diagram. Similarly in UNIX, it is possible that the output quantity of one command may not match the input quantity of the other. The pipe command gives you the ability to control the flow. If one command acts faster than the other, it readjusts the flow. For example:
$ ls | more
The "ls" command lists the contents of a directory. However, this may result in excess output on the terminal if the directory consists of lots of files. Therefore, in order to control the output and make more sense of it, the pipe command (|) is used to pass this output to another command, "more" (which limits the output to a page). This results in the output being shown in one page. Only when the user presses "Enter" is the next page of output shown.
Flexibility
-
The pipe command has the ability to join more than two UNIX commands, providing more flexibility to the user. However, to use this, users should know what they want overall and what individual commands do. For example:
$ls | grep ^d | grep notes | wc --l
The command above intends to find all sub-directories inside a directory that contain the word "notes" in them and then does a word count. This is done by the combination of the four commands shown above. The first command, "ls," lists the content of the current directory. The second command, "grep ^d," finds the sub-directories from that list. The third command, "grep notes," finds the sub-directories that contain the word "notes." The fourth command, "wc --l," does a word count from the output.
The main point to remember while using the pipe command is that the output from the command on the left is to the standard output and the input to the command to the right is from the standard input. So if there are UNIX commands that send outputs not to standard outputs (terminals, for example) in which the command is run, the pipe command should not be used.
Programming
-
Pipelines can be created in C/C++ programs, too, using the pipe () method. This call creates a pipe object, which creates two new file descriptors, fd1 and fd2. A read from fd1 accesses the data written to fd2 on a FIFO (First In First Out) basis, and a read from fd2 accesses the data written to fd2 on a FIFO basis.
-