Use the Pipe Command in Linux

This article explains how to use the pipe (|) character in Linux. Next, we will give the pipe (|) command examples.

Use the pipe (|) Command in Linux

Usually, applications require an input to execute and print the output to the screen.

The pipe (|) character lets you use the output of the first command as the second command’s input. Let’s explain more with examples.

Below is the file.txt content we will use in this article.

first line
line 2
line 3
Hello
test line
Hello
World!

Pipe the cat to the wc

The cat command prints the file content. The wc -l command takes a file as input and prints the line number of the file to the screen. When we pipe the cat to the wc, the output of the cat is given as input to the wc instead of being written to the screen.

cat file.txt | wc -l

Cat pipe wc

Pipe the sort to the uniq

sort sorts and prints lines of text files. The uniq command takes a file as input, omits the repeated lines, and prints the remainder lines to the screen.

When we pipe the sort to the uniq, the output of the sort is given as input to uniq instead of being written to the screen.

sort file.txt | uniq

Sort pipe uniq

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.