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.txtIn 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Â
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 -> tempOutput of the above two commands is same.Â
3. Sort a list of files by size using piping in Linux
ls -l sort -k 5
ls -l sort -k 5This 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 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 -56. 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" {} \;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 -l8.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.txtConclusion
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.Â
Similar Reads
Linux/Unix Tutorial
Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
Pipes and Filters in Linux/Unix
Pipes in UNIX The novel idea of Pipes was introduced by M.D Mcllroy in June 1972- version 2, 10 UNIX installations. Piping is used to give the output of one command (written on LHS) as input to another command (written on RHS). Commands are piped together using vertical bar " | " symbol. Syntax: com
2 min read
How to Install Python on Linux
This guide explains how to install Python on Linux machines. Python has become an essential programming language for developers, data scientists, and system administrators. It's used for various applications, including web development, data science, automation, and machine learning.This comprehensiv
15+ min read
grep command in Unix/Linux
The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by
7 min read
Unzip Command in Linux
As an open-source operating system, Linux presents plenty of powerful and versatile instructions for dealing with files and directories. One such command that performs an important role in coping with compressed files is the "unzip" command.Compressed files are a common way to keep space and share d
8 min read
How to Open a File in Linuxâ
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
Input Output Redirection in Linux
In Linux, whenever an individual runs a command, it can take input, give output, or do both. Redirection helps us redirect these input and output functionalities to the files or folders we want, and we can use special commands or characters to do so. For example, if we run the "date" command, it giv
4 min read
pr command in Linux
In Linux/Unix pr command is used to prepare a file for printing by adding suitable footers, headers, and the formatted text. pr command actually adds 5 lines of margin both at the top and bottom of the page. The header part shows the date and time of the last modification of the file with the file n
2 min read
Running Programs in Parallel Using xargs
The xargs is a concept of Linux/Unix piping, piping in Linux is a way to pass one command-processed output to another process for further processing. Using the xargs command to run many processes in parallel is a sort of command line argument known as "running programs in parallel." Similar to this,
6 min read
How to Fix "pip command not found" in Linux
Python has become an essential tool for developers, data scientists, and system administrators due to its versatility and extensive libraries. The 'pip' tool, which is the standard package manager for Python, plays a crucial role in managing Python packages, libraries, and dependencies. However, enc
9 min read