Sunday, April 14, 2013

cmdline find of the day: tee

I'm always amazed at how much of linux/unix I've never used. I've been working with unix and linux going on 17 years now and I've never used the command 'tee' but today while googling for a trick to trap the output of the command line to a file and pipe to the screen at the same time I came across it.

For years, I've been doing the following:
command >& log_name & tail -f log_name
or occasionally I substitute 'tail' with 'less' and press 'shift+f'... but today and for now on, I'm simply using tee:
command 2>&1 | tee log_name
Tee does just that, it captures stdout (and if you use 2>&1 stderr) and pipes to a file while printing to the screen.

and to make my life even easier.... the reason I was looking for this command was so that I could easily put together a simple bash function that would trap the output from 'make' add place it in a log file with a time stamp. Here is my simple function
MAKE()
{
   make $@ 2>&1 | tee ${!#}_`date +%Y%m%d%H%M`.log
}
 and if you are curious the '${!#}' part grabs the last argument on the command line so that if you want to use tools like '-j3' for multiple jobs or '-l3.9' to load management it won't get mixed into your log file name.

No comments: