The Linux pee command will split your output into two streams... Yes, pun intended. It is another incredibly useful tool provided by the moreutils package. Although the name and description may give you a chuckle, this is a serious utility. Let's take a look at the pee command and it's basic usage.
To use the pee command you must install the moreutils package.
What Does the pee Command Do?
To understand the pee command, it might help to familiarize yourself with the tee command provided by GNU Core Utilities. In short, the tee command takes standard input and writes it out to multiple files and standard output.
Conversely, the pee command takes standard input and redirects it to multiple pipelines (separate data streams) WITHOUT writing it to standard output.
The pee Command Basic Usage With Examples
The pee command does not have any options. It simply takes commands as arguments. Let's take a look at some examples.
[savona@putor ~]$ echo "Show this forward and in reverse" | pee cat rev Show this forward and in reverse esrever ni dna drawrof siht wohS
In the example above the echo command prints the sentence to standard output. This standard output it piped into pee, which sends a copy of it to each of the commands. In this case the cat command printed the sentence to standard output. The rev command reversed the order of the sentence and again printed it to STDOUT.
You can also use more advanced commands that take their own arguments. Simply wrap the full command in single quotes like so:
[savona@putor ~]$ echo "Linux is Fun!" | pee cat 'cut -d" " -f1' Linux is Fun! Linux
Here is a diagram to help understand the flow of streams.
In the above example we used the pee command to feed the stream to two commands. The first was cat, which just prints the sentence to STDOUT. The second was the cut command which needs a delimiter (
-d) and a field designation (
-f) as arguments.
Once the streams are duplicated, you can do anything that you normally would do with them. Here we create a new pipe out of the stream.
[savona@putor ~]$ echo "Linux is Fun!" | pee 'rev | cut -c 2- | rev' 'tr [:lower:] [:upper:]' LINUX IS FUN! Linux is Fun
In the above example we again used the pee command to copy STDIN to two commands. One being another pipe that uses rev and cut to remove the last character. The second is a simple translation with the tr command to change lowercase letters to uppercase.
There is no limit (that I know of) to how many different pipes you can feed the stream to.
[savona@putor ~]$ echo "Print this 5 times" | pee cat cat cat cat cat Print this 5 times Print this 5 times Print this 5 times Print this 5 times Print this 5 times
That's it! It is a very basic, yet useful tool. Without many options it is simple to learn and easy to implement.
Resources and Further Reading
This site uses Akismet to reduce spam. Learn how your comment data is processed.