Pipe Like Mario

So when Mario jumped into a pipe he’d come out in a different area. Well, technically it’s a sewer pipe, but close enough. He’d go out one area and into the other.

Linux® has pipes too. They take the results out from one command and put them in to another. It’s not a sewer pipe, so you don’t have to worry about figuring how to make a pipe on the command line. It’s the | character. It kind of looks like a pipe… ish. It isn’t hollow, but it’ll do.

So what can we do with it? Well, let’s start with something simple. Suppose when I run ls in a directory I get waaaaaay too many files to look at.  It’s just pages and pages of results.  Rather than trying to read all those lines scrolling by, I can pipe the results from ls into more

Alpha:~ computerlamp$ ls | more

I could also pipe the results through less:

Alpha:~ computerlamp$ ls | less

For silliness, I can pipe the command echo through less:

Alpha:~ computerlamp$ echo "Mario Rocks" | less

Now if you don’t give a command after the pipe, things just hang around.

Alpha:~ computerlamp$ echo "Mario Rocks" |

The command line is waiting for me to give another command to pipe the results of that echo in to. I can either type another command or hit control-C to exit that. That’s holding down the control key and then hitting C. It’s a common way to interrupt things on Linux®.

If you remember way back from post on Linux® and Files, we talked about standard file types. What technically is happening with a | is that the first command writes to standard out and that is written to the standard in of the second command through the pipe. This means that in order for it to work, the second command has to take input from standard in. Not every command does this. We’ll talk about how to fix that next time.

Leave a Reply

Your email address will not be published. Required fields are marked *