A Newbie’s Guide To Piping In Linux

Today I will be showing you how to use piping or pipe commands in Linux. Piping is used to take the output from one command and “pipe” it into another command. An example of this would be listing all your drives on an HP server with hpacucli and then piping it into grep to search for the term “fail” so that only failed drives are shown. To pipe a command you will use the “|” symbol between commands.

How To Pipe Commands In Linux

So let’s get started with some basic examples of using pipes. The following commands will take the output of the first command and then pipe into another. I will show the command by itself as well as the piped version afterward.

sudo cat /var/log/*
sudo cat /var/log/* | grep -i error # Yes I know you don't have to cat into grep its an example
smartctl -HAi /dev/sda
smartctl -HAi /dev/sda | grep -i stat

Pipe Remote Scripts

You can also pipe remote scripts directly into any shell or any interpreter for that matter. For example, you can curl a bash script or a Perl script and pipe them directly into the environment to run it. Here are a few examples of using a pipe to do so.

curl -s https://site.com/script.pl | perl
curl -s https://site.com/script.sh | bash

I hope this guide has shown you how easy and useful piping in Linux is, please feel free to comment if you have any questions or additional info that you would like to see added. Thanks for reading!

Leave a Reply

Protected with IP Blacklist CloudIP Blacklist Cloud