- What are Pipes in Linux? How does Pipe Redirection Works?
- Pipes in Linux: The general idea
- Keep in mind: Pipe redirects stdout to stdin but not as command argument
- Types of pipes in Linux
- Read the full story
- Pipe, Grep and Sort Command in Linux/Unix with Examples
- ‘pg’ and ‘more’ commands
- The ‘grep’ command
- The ‘sort’ command
- What is a Filter?
- Summary:
- What is a pipe in Linux
- How pipes work in Linux
- Syntax of pipes in Linux
- How to use pipes in Linux
- How to use pipe for sending the list of files and directories to “more” command in Linux
- How to use pipe to separate files from the list of all files and directories in Linux
- How to use pipe to count number of specific files from the list of all files and directories in Linux
- How to use pipe to sort a file and print its unique values in Linux
- How to use pipe to fetch particular data in Linux
- How to use pipe to print file lines in a specific range in Linux
- Conclusion
- About the author
- Sharqa Hameed
What are Pipes in Linux? How does Pipe Redirection Works?
There are two kinds of pipes in Linux: named and unnamed. Here’s a detailed look at pipe redirection.
You might have used a syntax like cmd0 | cmd1 | cmd2 in your terminal quite too many times.
You probably also know that it is pipe redirection which is used for redirecting output of one command as input to the next command.
But do you know what goes underneath? How pipe redirection actually works?
Not to worry because today I’ll be demystifying Unix pipes so that the next time you go on a date with those fancy vertical bars, you’ll know exactly what’s happening.
Note: I have used the term Unix at places because the concept of pipes (like so many other things in Linux) originates from Unix.
Pipes in Linux: The general idea
Here’s what you’ll be seeing everywhere regarding “what are Unix pipes?”:
- Unix pipes are an IPC (Inter Process Communication) mechanism, that forwards the output of one program to the input of another program.
Now, this is the general explanation that everyone gives. I want to go in deeper. Let’s rephrase the previous line in a more technical way by taking away the abstractions:
- Unix pipes are an IPC (Inter Process Communication) mechanism that takes the stdout of a program and forwards that to the stdin of another program via a buffer.
Much better. Taking away the abstraction made it much cleaner, and more accurate. You can look at the following diagram to understand how pipe works:
One of the simplest example of the pipe command is to pass some command output to grep command for searching a specific string.
For example, you can search for files with name containing txt like this:
Keep in mind: Pipe redirects stdout to stdin but not as command argument
One very important thing to understand is that pipe transfers the stdout of a command to stdin of another but not as a command argument. I’ll explain it with example.
If you use cat command without any arguments, it’ll default to reading from stdin . Here’s an example:
$ cat Subscribe to Linux Handbook for more articles like this ^D Subscribe to Linux Handbook for more articles like this
Here I used cat without passing any files, so it defaulted to stdin . Next I wrote a line and then used Ctrl+d to let it know that I’m done writing (Ctrl+d implies EOF or End Of File). Once I was done writing, cat read from stdin , and wrote that very line to stdout .
Now consider the following command:
The second command is NOT equivalent to cat hey . Here, the stdout , «hey», is taken to a buffer, and transferred to the stdin of cat . As there were no command line arguments, cat defaulted to stdin for read, and there was something already in the stdin, so cat command took it, and printed to stdout .
In fact, I created a file named hey and put some content in it. You can see the difference clearly in the image below:
Types of pipes in Linux
There are two kinds of pipes in Linux:
Read the full story
The rest of the article is available to LHB members only. You can sign up now for FREE to read the rest of this article along with access to all members-only posts. You also get subscribed to our fortnightly Linux newsletter.
Pipe, Grep and Sort Command in Linux/Unix with Examples
The Pipe is a command in Linux that lets you use two or more commands such that output of one command serves as input to the next. In short, the output of each process directly as input to the next one like a pipeline. The symbol ‘|’ denotes a pipe.
Pipes help you mash-up two or more commands at the same time and run them consecutively. You can use powerful commands which can perform complex tasks in a jiffy.
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.
An illustration would make it clear.
Click here if the video is not accessible
‘pg’ and ‘more’ commands
Instead of ‘less’, you can also use.
And, you can view the file in digestible bits and scroll down by simply hitting the enter key.
The ‘grep’ command
Suppose you want to search a particular information the postal code from a text file.
You may manually skim the content yourself to trace the information. A better option is to use the grep command. It will scan the document for the desired information and present the result in a format you want.
Here, grep command has searched the file ‘sample’, for the string ‘Apple’ and ‘Eat’.
Following options can be used with this command.
Option | Function |
---|---|
-v | Shows all the lines that do not match the searched string |
-c | Displays only the count of matching lines |
-n | Shows the matching line and its number |
-i | Match both (upper and lower) case |
-l | Shows just the name of the file with the string |
Let us try the first option ‘-i’ on the same file use above –
Using the ‘i’ option grep has filtered the string ‘a’ (case-insensitive) from the all the lines.
The ‘sort’ command
This command helps in sorting out the contents of a file alphabetically.
The syntax for this command is:
Consider the contents of a file.
There are extensions to this command as well, and they are listed below.
Option | Function |
---|---|
-r | Reverses sorting |
-n | Sorts numerically |
-f | Case insensitive sorting |
The example below shows reverse sorting of the contents in file ‘abc’.
What is a Filter?
Linux has a lot of filter commands like awk, grep, sed, spell, and wc. A filter takes input from one command, does some processing, and gives output.
When you pipe two commands, the “filtered ” output of the first command is given to the next.
Let’s understand this with the help of an example.
We have the following file ‘sample’
We want to highlight only the lines that do not contain the character ‘a’, but the result should be in reverse order.
For this, the following syntax can be used.
cat sample | grep -v a | sort - r
Let us look at the result.
Summary:
- Pipes ‘|’ send the output of one command as input of another command.
- The Filter takes input from one command, does some processing, and gives output.
- The grep command can be used to find strings and values in a text document
- Piping through grep has to be one of the most common uses
- ‘sort’ command sorts out the content of a file alphabetically
- less ,pg and more commands are used for dividing a long file into readable bits
What is a pipe in Linux
In Linux-based operating systems, Pipe is a type of redirection utilized for transfer the standard output of one command to a destination or other command. It is used for sending the output of one process, program, or command to another process, program, or command for additional processing. The Linux systems permit the standard output or stdout of a command to be connected with the standard input or stdin of the other command. In Linux, pipes are represented using the “|” pipe character.
A pipe connects two or more processes, programs or commands for a limited time. For additional processing, the Linux system utilizes the command-line program known as filters. The direct connection which is created between multiple processes, commands, and programs permits them to run at the same time. However, pipes also enable the data transmission between them without going through the display screen or temporary text files.
How pipes work in Linux
Data moves from left to right through pipes and therefore pipes are unidirectional. The utilization of pipes in the Linux terminal has many advantages. You can group numerous programs using pipes for creating highly powerful commands. Most command-line programs support multiple modes of operation. These programs can write and read data to files and accept standard output and input. This statement declares that the output of one program can be utilized as input for another. You can then send the output of the second program as input to a third program or save it to a file. That’s how pipes work in a Linux-based operating system.
Syntax of pipes in Linux
The pipe character “|” is used for adding a pipe in a command. The general syntax of pipes in Linux is as follows:
Write out the first_command in the terminal; then specify the pipe character “|”. After that, add the second_command. Till this point, the pipe will send the standard output of the first_command as an input to the second_command. Pipes can be used to generate a chain of commands. However, the functionality of the pipes will remain in the whole commands chain.
How to use pipes in Linux
In a Linux terminal, pipes are represented using the “|” pipe character. Now, we will write out some commands comprising pipes to explain the working of pipes in Linux practically.
Note: For the demonstration of the pipe examples, we are using Ubuntu 20.04. However, pipes work the same in all Linux-based systems.
How to use pipe for sending the list of files and directories to “more” command in Linux
In this example, we will use the pipe between “ls” and “more” commands. The “ls” command is utilized for listing directories and files, and the “-l” option is added to list them in long format. Whereas the “more” command will display the list in a scrollable manner, one screen at a time:
The execution of the above-given command will send the list of files and directories as an input to the “more” command using pipe “|”:
Now, press “Enter” view more list directories and files:
How to use pipe to separate files from the list of all files and directories in Linux
The pipe also provides you the facility to separate and list specific files from a list. For this, you can use the “ls” command to list files and the “grep” command for searching the specific pattern and add the “|” pipe character between these commands.
In the below-given example, the pipe character will send the list of files and directories to the “grep” command. Then, the grep command will extract the file having the “txt” pattern in them:
How to use pipe to count number of specific files from the list of all files and directories in Linux
You can utilize pipes to create a chain of commands. This chain of commands is executed at once in the Linux terminal. For instance, we can extend the previously executed command by adding a pipe and “wc” command. The second pipe will send the output of the “grep” command to “wc”.
The output of the command will print out the total number of files containing the “txt” pattern:
How to use pipe to sort a file and print its unique values in Linux
If you want to sort a file and then print out its unique values in the terminal, then execute the below-given command:
Here, the “sort” command is utilized to sort the “sampletest1.txt” file. The pipe “|” sends the “sort” command output to “uniq“. Then, the “uniq” command will filter the duplicate values:
How to use pipe to fetch particular data in Linux
You can utilize the pipe “|” between the cat and grep command. The “cat” command will extract the data from “sampletest1.txt”, whereas the “grep” command will search for the “U” letter in the “sampletest1.txt” content. For further processing, pipe “|” will send the “cat” command output to “grep”:
The output will show you the text having “U”:
How to use pipe to print file lines in a specific range in Linux
“head” and “tail” commands are used to print out the first and last part of a file. In this example, we will utilize the pipe “|” to fetch the “sampletest2.txt” file data resulted from the “cat” command and then pass it to the “head” and “tail” command as input:
It will show you the below-given output:
Conclusion
In Linux-based systems, the pipe is utilized for combining two or more commands in such a way that the output of one command is passed as the input to the other one. The “|” symbol indicates pipe operator. With the help of pipe operator, each process output is directly given as input to the next command. In this post, you have learned what a pipe operator is in Linux. Moreover, we have also demonstrated various examples related to pipes in a Linux system.
About the author
Sharqa Hameed
I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.