Pipes
Pipes: how to combine commands to perform complex tasks
Suppose that you want to save to know how many “txt” files are present in
our learn_bash
directory. We already know how to redirect the output from
a command to a files, so we could save the output of find to a file, and
then count the lines of that file:
1
2
3
find ~/learn_bash -name "*.txt" > files.txt
wc -l files.txt
rm files.txt
We basically create a temporary file (files.txt) and then we remove it after we are done.
Pipes
UNIX pipes are a powerful way of combining commands by redirecting the output of a program to make it becoming the input of another program.
Some general notes:
- The first command is executed first and (usually) reads from a file
- All the other commands will be fed from the output of the previous command: we usually do not specify any file path
Try this:
1
find ~/learn_bash -name "*.txt" | wc -l
In a single line we obtained the same information as before, but without
creating a temporary file.
Note that the first command in itself works, while the second command (wc -l
) is not
given any file to operate on: it reads the lines from the standard input, that is
from the output of the previous command being piped with the |
character.
We can combine several commands, with the usual rule that the first command requires to find its input from the system, while the others will be fed by the output of the previous command.
1
find ~/learn_bash -name "*.txt" | sort | head | tail -n 1
In this case we:
- Print all the files ending by
.txt
in thelearn_bash
directory - Sort them alphabetically
- Select the first ten lines
- Print the last line (of the ten)