Connecting commands together with the pipe

Tue, 09/04/2012 - 16:58 -- jamie

One of the philosophies that Linux has inherited is the idea that we should create small programs that do one thing well. If we want to do more complex tasks, we can combine multiple programs to do more complex things.

One way that programs are combined on the command line is by using the "pipe" character (aka |).

The pipe character takes the output of the first command and sends it as input to the second command.

For example, the command ls outputs a listing of files in a directory. The command grep searches it's input for a particular string and outputs all lines that match the string.

Suppose you want to edit your dnsmasq settings but you can't remember the exact name of the /etc/dnsmasq.d directory. You remember that it has dns in it and it's in /etc. You could type:

ls /etc/ | grep dns

That command will take the output of `ls /etc/` and pass it to grep. grep will search those lines and only output the ones with the term dns in it.