power of these commands lies in the capability to join them together to get
exactly what you want. There are some extra little commands that we have
not looked at that are often used as glue because they do one very simple
thing that enables a more complex process to work.
Pipes
All the commands we have looked at have printed their information to the
screen, but this is often flexible.
A pipe is a connector between one command’s output and another command’s
input. Instead of sending its output to your terminal, you can use a pipe to
send that output directly to another command as input.
Two of the commands we have looked at so far are ps and grep: the process
lister and the string matcher. You can combine the two to find out which users
are playing NetHack right now:
Click here to view code image
matthew@seymour:~$ ps aux | grep nethack
This creates a list of all the processes running right now and sends that list to
the grep command, which filters out all lines that do not contain the word
nethack. Ubuntu allows you to pipe as many commands as you can sanely
string together. For example, you could add the wc command, which counts
the numbers of lines, words, and characters in its input, to count precisely
how many times NetHack is being run:
Click here to view code image
matthew@seymour:~$ ps aux | grep nethack | wc -l
The -l (lowercase L) parameter to wc prints only the line count.
Using pipes in this way is often preferable to using the -exec parameter to
find simply because many people consider find to be a black art, and using
it less frequently is better. This is where the xargs command comes in: It
converts output from one command into arguments for another.
For a good example, consider this mammoth find command from earlier:
Click here to view code image
matthew@seymour:~$ find / -name "*.txt" -size +10k -user matthew -not
-perm +o=r
-exec chmod o+r {} \;
This searches every directory from / onward for files matching *.txt that