Updated April 15, 2023
Introduction to Pipe Command
A pipe is an aspect of redirection i.e., transfer of any standard output to another target path which is commonly used in Unix and Linux-like operating systems in order to send the outputs of one process/command/program to another/program/command/process for any further processing. The Linux/Unix system enables stdout of a command to connect to stdin of other commands, which we can do by the pipe character ‘|.’
The Pipe command is used to transfer the output of two or more commands, and the output of one command will act as input to another command, and this command output will act as input to the subsequent command, and it goes on. We can also consider it as a virtual connection between different commands, programs, or processes. The command line which process is referred to as filters.
Pipes are usually unidirectional means data will flow from left to right by the pipeline.
Concept
Each program in the UNIX and Linux system has mainly 3 built-in data streams:
1) STDIN (0) –> Standard input
2) STDOUT (1) –> Standard output
3) STDERR (2) –> Standard error
** Whenever we work with “pipe,” “pipe” command will take STDOUT of command and transfer it to STDIN of the subsequent command.
Syntax:
The pipe command syntax is straightforward.
command1 | command2 | command3 | .... | commandN
Options
As the pipe is like a redirection operator, we can use it in different commands. Some of the options are mentioned below :
1) grep -I filename | sort: In this, the grep result will be fetched from the filename and will act as an input to the sort command, and the sort command will sort the data in default mode.
2) ls -l | ls File1: In this, the list of files will be listed, and the output of that will be transfer to the ls command to search the File1 from a bunch of files and directory.
3) sort -r file.txt | grep -i “Country”: In this option sort command will take input from the file.txt and sort the data in reverse order and pass the output to the grep command, which will search the keyword “Country” from the data redirection.
Let us understand the concept of pipe command concept in more elaborate form with the help of the examples.
1) Listing of all the files and directories and pass the input to the more command
Command: ls -l | more
Output:
Note: The more command will take the output of ls -l as input. The result of the command will display the output of ls -l one screen at a time but for a micro sec after which pipe will act like a container, which will take the output of ls -l and passes the result to more as input. The command will not use the disk to connect the standard output of the ls -l command to the standard input of the more command because the pipe is usually implemented in the main memory of the operating system.
We can also use the below command sequence to get the result as compared to the I/O redirection operator.
Command:
ls -l -> temp1
more -> temp1 (or more temp)
{content of the temp}
rm temp1
The output of the above two commands will be the same.
2) Using of sort and uniq command for sorting a file and print distinct value
Command:
sort file.txt | uniq
The above command will first sort the file and print the distinct data sets on the terminal.
3) Using of head and tail command to print line in a particular range of a file
Command:
cat file.txt | head -8 | tail -4
The above command will open the file using the cat command and then select the first 8 lines than the last 4 lines from the file.txt, and will printout those lines, which will be common to both commands.
4) Using of ls to discover the list and then print all the lines matching with a particular pattern in the matching file.
Command :
ls -l | find . -type f -name '*.tmp' -exec grep -i 'country' {} \;
On executing the command, will list all files and select the file with .tmp extension in a particular directory and will search for the pattern like “country” and print those lines which contain country keywords in them.
5) Using of cat, tee, grep, and wc command in order to read inputs from the user and store in a file to print count of line
Command:
cat a.txt | grep "country India" | tee a.txt | wc -l
The above command on execution will open the file and then search for the text “country India,” and by using tee command, it will store in the a.txt afterward will count the total number of lines in the a.txt
6) Using of cat and sed command to replace a particular text with another text
Command:
cat file.txt | sed -i s/country/state/g'
When the above command executes, it will open the file and search the country keyword and will replace it with state text globally in the output file
7) Use of cat, cut, and echo command to cut out a particular column with delimiter
Command:
cat file_states.txt | cut -d ' ' -f 1 | sort -r
The command will process the delimited file_states.txt and will cut the first column having “,” as delimiter and then display the content of the file in a reverse sorting manner.
Output:
Conclusion
In this way, we learned about the pipe command along with concepts and applications with the help of different examples.
Recommended Articles
We hope that this EDUCBA information on “Linux Pipe Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.