Oxaric’s Blog

A compendium of amazing things…

Fun With BASH Piping!

Posted by oxaric on November 24, 2008

You may or may not be familiar with piping but if you use or plan to use a Linux machine you should definitely look into it.


Piping allows a command to send it’s output to another command. If you are familiar with programming you could think of this situation as calling a function, getting the output, and sending it directly to another function. Below are examples of two different forms of piping.


These are the commands I’ll use in the examples:


ls – lists all of the files in the current directory, Windows users might know this as the dir command


find – when used with no parameters it will list every file inside the current directory including files inside of other directories


rm – deletes a file, Windows users might know this is as the del command


grep – searches a string looking for a given string or pattern





Piping Example



~/test> ls | grep a.out


What happens:

First, the ls command will find and try to display all the files in the current directory. However, the bar symbol | causes the ls output to be piped to the grep command. The grep command will then take the input from the pipe and search the piped lines for any string that contains “a.out“. If grep finds any such strings it will then directly display them.


Using the bar symbol, |, is the most common form of piping. But another form of piping exists called substitution piping.


Substitution piping can be used with commands that do not work with the direct piping method shown above. An example of such a command is rm which will not accept a direct piping of file names or directories to delete. The rm command only accepts file names and directories if they are directly given in it’s parameters while calling it. This is the beauty of substitution piping. It allows you to perform a command and send the output line-by-line as a parameter to another command.


Substitution Piping Example




~/test> rm $(find | grep a.out)


What happens:

First, the commands inside of $() will run and produce a list of all files in the current directory and in any directories inside the current directory that have “a.out” in their file name. As you can see, both forms of piping can be used together. Once the commands inside the substitution pipe are completed the output will be passed line-by-line as a parameter to the first command rm which will delete every file passed to it.





Caveat emptor!
Be careful!



Piping can be a force for good and evil. It is extremely easy to be mistaken about the exact output of a command and end up deleting precious files or doing all sorts of crazy things. Test all of your commands before piping.

And remember, a wise old man once said, “To use commands is human, to pipe divine.” ;)

2 Responses to “Fun With BASH Piping!”

  1. jadu said

    I love to use $() instead of the `backticks`.

  2. Nick Warrington said

    This is my kind of tutorial. Thanks!

Leave a comment