pipe Linux Command | Explained

In Linux, a pipe (|) is the diversion of the output to the other targets or commands. This is the direct connection between the commands separated by the | symbol. It is an easy method to transfer and permit data at a time rather than pass through the screen or files.

This write-up will show a detailed guide to using the pipe command in Linux. The following content is covered in this guide:

Let’s get started.

What is the pipe (|) Command in Linux?

The working of the pipe command depends on the syntax of the command. It is given as follows:

command 1 | command 2 | command 3 | command 4 | ....... so on

Working of the pipe command is just like the flow of the container from left to right, the output of the first command works as an input to the second command. Similarly, the second output work as input to the third command; this process continues until the execution of the last command.

How to Use the pipe Command in Linux?

Let’s practically perform the execution of pipe commands in Linux. Some examples of the pipe command in Linux are discussed below.

Example 1: To Get the List of Directories/Files on One Screen

The “more” command in Linux is exercised to get the output on one screen. Usually, its usage is observed with text files. However, if it is applied with the “ls” command with the help of “|”, it will return the directories/files in one column. Let’s check out the output of the following command:

$ ls | more

The difference can be observed with and without using the “more” command.

Example 2: Retrieving Only Specific Lines From File

The below-stated command utilizes multiple pipe commands. The description of the command is as follows: the cat command will print the content in the text file, then the content will be sent to the head command as an input, and it will pick 3 lines from that input and send them to the next command. In the end, the tail command will print the last two lines from the coming input:

$ cat itslinuxfoss.txt | head -3 | tail -2

In this command, the head will select the first 3 lines from the file, and the tail will print the last 2 lines from those 3 lines.

Example 3: Retrieving Word Count From the Copied File

Here, multiple commands are used to pass output from one to another command.

In this command, the content of the text file will be given to the “grep” command, which will match the particular words and forward them to the “tee” command, tee command will copy that content into the new file, and this file will be sent to the “wc” command which will count the number of words saved in the file:

$ cat itslinuxfoss.txt | grep "henry" | tee itslinuxfoss2.txt | wc -l

The output shows that the number “2‘ is returned on the terminal.

Example 4: Retrieving Specific Word From the Content

In this command, the first 4 lines will be selected, then from that 4 lines last 4 lines will be selected by the tail, and the “uniq” command will pick the unique content from that lines. Lastly, the “grep” command matches the “henry” keyword:

$ cat itslinuxfoss.txt | head -4 | tail -4 | uniq | grep joseph

Bonus Tip: Scenarios When Pipe Command is Useless

Some beginners might get confused while using the wrong way of pipe command. There must be a link to the command’s output which is being forwarded as input to the second command. The user must know about every output of the command. Let’s discuss an example of this scenario:

$ ls | cd Downloads

It is observed that nothing is displayed because the output of the first command is useless for the second command.

That’s all from this post.

Conclusion

In Linux, the pipe command is used to combine multiple commands. The previous command’s output is used as input to the next command, which continues until the execution of the last command. It is noticed that if the output of the previous command is useless for the next command, then there will be no output of the pipe command. This post has illustrated the usage and working of the Linux Pipe command.