Linux Commands

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:

$ first_command | second_command | third_command . . .

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:

$ ls -l | more

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:

$ ls | grep "txt"

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”.

$ ls | grep "txt" | wc -l

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:

$ sort sampletest1.txt | uniq

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”:

$ cat sampletest1.txt | grep "U"

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:

$ cat sampletest2.txt | head -3 | tail -7

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.