A pipe is a form of redirection (transfer of standard output to some other destination) that is used in Linux and other Unix-like operating systems to send the output of one command/program/process to another command/program/process for further processing. The Unix/Linux systems allow stdout of a command to be connected to stdin of another command. You can make it do so by using the pipe character ‘|’.
Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command’s output may act as input to the next command and so on. It can also be visualized as a temporary connection between two or more commands/ programs/ processes. The command line programs that do the further processing are referred to as filters.
This direct connection between commands/ programs/ processes allows them to operate simultaneously and permits data to be transferred between them continuously rather than having to pass it through temporary text files or through the display screen.
Pipes are unidirectional i.e data flows from left to right through the pipeline.
Let us understand this with an example.
When you use 'cat' command to view a file which spans multiple pages, the prompt quickly jumps to the last page of the file, and you do not see the content in the middle.
To avoid this, you can pipe the output of the 'cat' command to 'less' which will show you only one scroll length of content at a time.
cat filename | less
Use head and tail to print lines in a particular range in a file.
$ cat sample2.txt | head -7 | tail -5
This command select first 7 lines and last 5 lines from the file and print those lines which are common to both of them.
Use ls and find to list and print all lines matching a particular pattern in matching files.
$ ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} ;
This command select files with .txt extension in the given directory and search for pattern like “program” in the above example and print those ine which have program in them.