These Unix Spotlights are about some Unix commands that you might not know exist and a few things you can do with them.

TL;DR: tee splits the output of stuff.  Think of it like a T where the input is going up the bottom and the output splits at the top.


Here was the problem.  I was building a docker container but at one point there was an error and the terminal had a ton of errors and I couldn't scroll up to see what caused them.  Bummer.  My first thought was to pipe out the thing into a log, which I did — but then I had to keep refreshing the log to see what was happening.  I looked for a log reader, like on Mac, but there wasn't anything great I found.

"I want to be able to read the terminal, and I want a log file of the things that are happening on the terminal.  Is that so much to ask?"

# This will append the results to ip_pings.log, but also display it in terminal.
$ ping 127.0.0.1 | tee -a ip_pings.log

But you can also do slightly less trivial things as I learned.  Here, I'm taking input (from a trivial place, but you can take it from any stream) and make a backup of the raw stream as well as transforming it.

For example, in my ls I don't want to see kilobytes, that's lame: I want coolobytes.  

# Do an ls, save that into backup.txt, 
# Do a sed find-and-replace (ignore if you don't know what it is)
# Then save the find-and-replace into main_file.txt

$ ls -sh | tee backup.txt | sed -r 's/([0-9.]+)K/\1 coolobytes/g' | tee main
_file.txt

total 20 coolobytes
4.0 coolobytes hello-backup.txt
 16 coolobytes hello.txt
   0 main_file.txt

$ cat main_file.txt

total 20 coolobytes
   0 backup.txt
4.0 coolobytes hello-backup.txt
 16 coolobytes hello.txt
   0 main_file.txt

$ cat backup.txt
total 20K
   0 backup.txt
4.0K hello-backup.txt
 16K hello.txt
   0 main_file.txt

You will probably never need to find-and-replace coolobytes but this can be helpful when parsing logs into a nicer form ad hoc.