Open In App

Piping in Unix or Linux

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 the stdout of a command to be connected to the stdin of another command. You can make it do so by using the pipe character ‘|’

The 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 of Piping in Unix or Linux

1. List all files and directories and give them as input to `grep` command using piping in Linux

ls | grep file.txt

ls | grep file.txt

In this first we are using `ls` to list all file and directories in the current directory, then passing its output to `grep` command and searching for file name `file.txt`. The output of the ls command is sent to the input of the grep command, and the result is a list of files that match the search term.

2. List all files and directories and give them as input to `more` commands using piping in Linux.

$ ls -l | more 
$ ls -l | more

$ ls -l | more 

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 
ls -l -> temp” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20230511111531/114.webp 732w, https://media.geeksforgeeks.org/wp-content/uploads/20230511111531/114-100.webp 100w, https://media.geeksforgeeks.org/wp-content/uploads/20230511111531/114-200.webp 200w, https://media.geeksforgeeks.org/wp-content/uploads/20230511111531/114-300.webp 300w, https://media.geeksforgeeks.org/wp-content/uploads/20230511111531/114-660.webp 660w, ” sizes=”100vw” width=”732″><figcaption>ls -l -> temp</figcaption></figure>
<p>Output of the above two commands is same. </p><div id=

3. Sort a list of files by size using piping in Linux

ls -l sort -k 5
ls -l sort -k 5

ls -l sort -k 5

This command lists all the files in the current directory, and then uses the `sort` command to sort the list by the fifth column, which represents the file size.

4. Use sort and uniq command to sort a file and print unique values using piping in Linux

$ sort record.txt | uniq 

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

sort record.txt | uniq

sort record.txt | uniq 

5. Use head and tail to print lines in a particular range in a file.  

$ cat sample2.txt | head -7 | tail -5

This command selects the first 7 lines through (head -7) command and that will be input to (tail -5) command which will finally print last 5 lines from those 7 lines. 

cat sample2.txt | head -7 | tail -5

cat sample2.txt | head -7 | tail -5

6. 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 selects files with .txt extension in the given directory and searches for patterns like “program” in the above example and prints those which have program in them. 

 ls -l | find ./ -type f -name "*.txt" -exec grep "program" {} \;

 ls -l | find ./ -type f -name “*.txt” -exec grep “program” {} \;

7. 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 selects Rajat Dua and store them in file2.txt and print total number of lines matching Rajat Dua 

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

cat result.txt | grep “Rajat Dua” | tee file2.txt | wc -l

8.How can I redirect the output of a piped command to file in Unix or Linux?

We can use redirection operator `>` to redirect the output of a piped command.

For Example:

If i have a file name `file.txt` and want to redirect it to a file name `geeks.txt`.

 ls | grep 'file' > geeks.txt
 ls | grep 'file' > geeks.txt” srcset=”https://media.geeksforgeeks.org/wp-content/uploads/20230511115806/121.webp 835w, https://media.geeksforgeeks.org/wp-content/uploads/20230511115806/121-100.webp 100w, https://media.geeksforgeeks.org/wp-content/uploads/20230511115806/121-200.webp 200w, https://media.geeksforgeeks.org/wp-content/uploads/20230511115806/121-300.webp 300w, https://media.geeksforgeeks.org/wp-content/uploads/20230511115806/121-660.webp 660w, https://media.geeksforgeeks.org/wp-content/uploads/20230511115806/121-768.webp 768w, ” sizes=”100vw” width=”835″><figcaption> ls | grep ‘file’ > geeks.txt</figcaption></figure>
<h2>Conclusion</h2>
<p>Piping is a powerful feature in Unix and Linux operating systems which helps us to link different commands together to perform complex tasks quickly and efficiently. In this article we have learned how we can redirect the output of one command to the input of another command. Overall, we can say that by using piping we can save time, improve productivity and reduce disk space usage. </p>
<br/><div id=

Last Updated : 05 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads