Skip to content
Related Articles
Open in App
Not now

Related Articles

Piping in Unix or Linux

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 08 Feb, 2022
Improve Article
Save Article

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. 

Syntax : 

command_1 | command_2 | command_3 | .... | command_N 

Example : 
1. Listing all files and directories and give it as input to more command. 


$ ls -l | more 

Output : 

The more command takes the output of $ ls -l as its input. The net effect of this command is that the output of ls -l is displayed one screen at a time. The pipe acts as a container which takes the output of ls -l and gives it to more as input. This command does not use a disk to connect standard output of ls -l to the standard input of more because pipe is implemented in the main memory. 
In terms of I/O redirection operators, the above command is equivalent to the following command sequence. 

$ ls -l -> temp
more -> temp (or more temp)
[contents of temp]
rm temp

Output : 

Output of the above two commands is same. 

2. Use sort and uniq command to sort a file and print unique values. 


$ sort record.txt | uniq 

This will sort the given file and print the unique values only. 
Output : 

3. 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 through (head -7) command and that will be input to (tail -5) command which will finally print last 5 lines from that 7 lines. 
Output : 

4. 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 in which have program in them. 
Output : 

5. Use cat, grep, tee and wc command to read the particular entry from user and store in a file and print line count. 


$ cat result.txt | grep "Rajat Dua" | tee file2.txt | wc -l

This command select Rajat Dua and store them in file2.txt and print total number of lines matching Rajat Dua 
Output : 



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!