What is Piping in Linux? [Types & Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Piping is a fantastic feature in Linux that makes complex commands easier. Using this feature you can execute multiple commands in a single line. A symbol is used to create a pipe between the commands and the symbol is (|). One command’s filtered output is used as another command’s input using the piping between them.

What is Piping Used for?

Piping is basically used to simplify the process of working with complex data by connecting commands in Linux. Multiple commands can be chained together to perform several processes in a single line. You can also use the pipes to replace one operation with another. This can specifically be helpful to manage large amounts of data. You can use piping for various purposes, I am listing some of them below for your better understanding:

  • Filtering
  • Sorting
  • Combining
  • Counting
  • Automating Tasks
  • Calculation on data
  • Manipulating and Transforming data

How Does Piping Work?

Piping is a way to redirect one command’s output to another command’s input in Linux. The commandONE’s output is filtered and then passed through the pipe to the commandTWO’s inputPiping in Linux using the piping symbol (|). And the final output will display the combination of these two commands. More than two commands can also be used to generate output using Piping.

The Syntax

The piping has a very simple syntax in Linux. You need to mention the pipe symbol (|) between the commands to connect them. And the syntax is as follows:

commandONE | commandTWO

Here, the commandONE’s output is filtered and piped to the commandTWO’s input.

Advantages & Productivity of Piping

There are several advantages and productivity benefits of using piping in Linux:

  • Efficiency: Piping allows you to run multiple processes in a single line which reduces the amount of time and effort required to perform tasks and makes data processing more efficient.
  • Flexibility: Piping is a flexible tool that allows you to combine a wide range of commands which makes it easy to automate complex tasks. Piping is an essential tool for data processing as it can be used to filter, sort, search, and transform data in countless ways.
  • Automation: Piping allows you to create scripts and workflows that help to automate complex operations.

Examples of Piping in Linux

Now that you know the syntax and how piping works, it is time to show you some examples of piping in Linux.

Example 1: Display the Last 10 Lines From History in Linux

You can use piping to combine the history command and the tail command to display the last 10 lines from history in Linux. Follow the steps below to display the last 10 lines from the history:

Steps to Follow >

➊ To start open the Terminal in Ubuntu.

➋ Type the following command in the command prompt:

history | tail -10

➌ Finally, strike the ENTER key.

Output >

In the image below, the output displays the last 10 lines from the history.

Display Specific Files from the List of All Files and DirectoriesExample 2: Count the Number of Lines From the List of all Files and Directories With Piping in Linux

You can count the number of lines from the list of all files and directories by piping the ls command and the wc command in Linux. You can follow the instructions below to do so:

Steps to Follow >

➊ Firstly launch the Ubuntu Terminal.

➋ Run the command below in the command prompt:

ls -l /usr/bin | wc -l

➌ Now, tap the ENTER button.

Output >

As you can see in the image below, the output displays the number of lines from the list of files and directories in Linux.

Display the Last 10 Lines From History in LinuxCount the Number of Lines From the List of all Files and Directories

Example 3: Send the List of Files and Directories to “less” Command With Piping in Linux

You can view the list of all files and directories on one screen at a time by piping the ls command and the less command in Linux. To do the same you can follow the following process:

Steps to Follow >

➊ Open the Terminal in Ubuntu.

➋ Execute the command below in the command prompt:

ls -l /usr/bin | less

➌ Then, press the ENTER key.

Output >

In the following image, the output displays the list of files and directories on one screen at a time in Linux.

Count the Number of Lines From the List of all Files and Directories in LinuxExample 4: Display Specific Files from the List of All Files and Directories in Linux

To view the specific files from the list of all files and directories you can pipe the ls command and the grep command in Linux. To do this you can follow the procedure below:

Steps to Follow >

➊ Initially launch the Terminal in Ubuntu.

➋ Copy the following command in the command prompt:

ls | grep "txt"

➌ Then, press the ENTER key.

Output >Send the List of Files and Directories to “less” Command in LinuxAs you can see, the output displays all the text files of the current directory.

Conclusion

In this article, I have explained what Piping is and how it works in Linux. I have also given some practical examples of Piping. After completing this article, you will get a basic idea of Piping in Linux.


Similar Readings

Rate this post
Jannatul Ferdousi Sylvie

Hi there! This is Jannatul Ferdousi Sylvie. I am one of the Linux Content Developer Executives at Softeko. I live in Dhaka, Bangladesh. I have completed my BSc. in Software Engineering from American International University-Bangladesh. You can see my projects in the GitHub account. I love traveling, shopping, cooking, and gardening. Read Full Bio

Leave a Comment