Linux pipes explained

Pipes are a powerful tool in the open-source Linux operating system. For example, Linux pipes allow you to process a series of commands that refer to a dataset, or to efficiently move data back and forth between commands. The advantage is that complex processes are simplified because stand-alone commands become a real “command team”. Learn how pipes work in Linux in detail and why this command is so helpful.

$1 Domain Names

Register great TLDs for less than $1 for the first year.

Why wait? Grab your favorite domain name today!

Matching email
SSL certificate
24/7/365 support

What are Linux pipes?

The term pipe is derived from “pipeline”. In computer science, a pipe is a stream of data that runs between two processes that are either closely related or also have no common origin. This means that the result output by one program serves as input to another program. Among other things, this allows you to split larger problems into smaller sub-problems for a better overview.

In Linux, there are two different types of pipes, the unnamed pipes and the named pipes. While unnamed or anonymous pipes are used between processes that are closely related, named pipes or FIFO (they are based on the “First In - First Out” principle), allow communication between two unrelated processes that may also be on different machines within the same network. Unlike unnamed pipes, named pipes also enable bidirectional communication and not just one-way communication (one process writes while the other only reads).

Note

The logical linking of commands is of course nothing unusual in computer science. With the linux in command, for example, it is possible to link to a file or directory in the simplest way. Our overview article explains which other important Linux commands you should know.

Linux pipe syntax and examples of use

For you to use pipes properly in Linux, it is helpful to first understand the syntax. Below you will first see the structure of the pipe command and then examples of how it looks in your terminal or command bar.

Note

Our instructions refer to the widely available Ubuntu installations, which are available for free and updated regularly.

Command-1 | Command-2 | …| Command-N

As you can see, the syntax of the unnamed pipe is quite simple. It consists of the symbol “|”, which is placed between two other commands, for example. In the example below, the pipe is only temporarily valid.

$ cat contents.txt |grep file
0 Aug  9 13:55  file1
0 Aug  9 13:55  file2
0 Aug  9 13:55  file3
0 Aug  9 13:55  file4
0 Aug  9 13:55  file5
$ cat contents.txt |grep "file"|awk '{print $8}'
file1
file2
file3
file4
file5
$ cat contents.txt |wc -l
8
$

Above, contents.txt contains a list of all files in each directory - more precisely, the output of the ls -al command. First, we pipe the file names with the keyword “file” from contents.txt, so that the output of the cat command serves as input to the grep command. Then we pipe the awk command, which displays the 8th column of the filtered output of the grep command. We can also count the number of lines in contents.txt with the wc -l command.

Tip

The grep command can be used to search lines from code and log files for strings. If you want to easily find a Linux file in the command bar, use this command.

While an unnamed Linux pipe is valid for one process only, a named Linux pipe will ensure that the command is valid the entire time until you shut down the system or delete it. The correct syntax here is as follows:

mkfifo <named-pipe>

or

mknod p <named-pipe>

Imagine you have a process running in one terminal that produces output. Now you want to pipe that output to a second terminal. This is where a named pipe is a big help. To redirect a standard output of any command to another process, use the “>” symbol. To redirect a standard input of any command, use the “<” symbol. In the following example, you name the pipe in the first terminal.

$ mkfifo named-pipe
$ ls > named-pipe

Now, in the second terminal, enter the following code to see the output.

$ cat < named-pipe

The next time you are working with pipe commands in Linux and need to move data between commands, you can hopefully do this quickly and easily with an unnamed or named pipe.

Tip

Are you using Linux and looking for a comprehensive web hosting package that lets you write all parts of the program code yourself or use an intuitive builder kit? Linux hosting from IONOS has scalable performance, DDoS protection, geo-redundancy, and many other features.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.