tee: More magic from coreutils

I have reached a point where I actively look forward to programs from coreutils, because I know they’re going to be good.

tee is no exception; like so much of what’s in coreutils, it is deceptively simple.

2014-05-24-jk7h5f1-tee

tee splits output two ways: once to STDOUT, and once to a file. So probably 90 percent of the time on this blog, when I’ve shown you something like this:

shuf -n1 /usr/share/dict/cracklib-small > test.txt ; cat test.txt

I could have done this instead:

shuf -n1 /usr/share/dict/cracklib-small | tee test.txt

and gotten both the file I was after and the output to show, without relying on two commands (and arguably abusing cat).

tee doesn’t just redirect to one file; you can list as many as you like.

kmandla@jk7h5f1 ~ $ echo "Test" | tee test-{01..10}.txt
Test
kmandla@jk7h5f1 ~ $ ls test-*
test-01.txt  test-03.txt  test-05.txt  test-07.txt  test-09.txt
test-02.txt  test-04.txt  test-06.txt  test-08.txt  test-10.txt

Knowing that there will be identical output means you can also use tee to write to a file, but wrangle the output in some fashion. This is where you should be thinking about sort or sed or whatever, to create a record of the output, then an adjusted version.

tee takes flags for appending files rather than clobbering them, and as an added note, if you insert a hyphen as a file name, you’ll get double STDOUT. In other words,

kmandla@jk7h5f1 ~ $ echo "Test" | tee -
Test
Test

If you didn’t know about tee until now, it should open a few doors for you. Or at least, make a few things easier in your command-line adventures. 😉

Advertisements

4 thoughts on “tee: More magic from coreutils

  1. Pingback: Links 27/5/2014: Greenwald on GNU/Linux, Drupal Nets Massive VC | Techrights

  2. Pingback: wgetpaste: A healthy collection of features | Inconsolation

  3. Georg

    Another very handy use is:

    $> command that has no need for sudo | sudo tee [-a] /some/place/only/root/knows

    to put some output of a regular user command in a file (or append) that only root has permissions to write to.

    PS: thanks for this blog! very nice place to find gems and be entertained at the same time 🙂

Comments are closed.