Sometimes the output from a command line tool can be so large it doesn't all fit on the screen, even with scrolling. Or you may want to preserve the output for later study.
For instance:
> wget --help
That's a lot of output!*
On both Windows and Linux, you can easily save the output of a command to a file like this:
> wget --help > output.txt
But that has two problems:
- It only saves stdout, not stderr.
- It no longer shows you the output. And that can be especially annoying if it's a command that takes a long time (like compiling GCC).
How to fix? On Linux, use this script I've named teelog:
#!/bin/sh
progpath=$1
progname=$(basename $1)
shift
$progpath "$@" 2>&1 | tee $progname.log
That redirects stderr into stdout, and then sends stdout to tee. The tee command saves it to a file while also sending it back out to stdout so you can still see it.
Don't forget to set the executable bit with chmod +x teelog. Then put that script somewhere on your PATH and just run:
$ teelog wget --help
You'll see everything just like normal, but everything will also get saved to ./wget.log.
On Windows, things are a bit more difficult. First of all, there's this little snippet that people on the internet keep repeating:
> wget --help 2>&1 > output.txt
That does not work reliably on Windows. It should work, and it does work on Linux. But on Windows, there's what appears to be a race condition: Whenever the command outputs to both stdout and stderr, the order is likely to get screwed up resulting in mixed-up, possibly-garbled, output. Interestingly, in my experience it does seem to work if you pipe to another program instead of redirecting straight to a file. But that leads to the second problem:
The second problem is that Windows (surprise, surprise) doesn't have a built-in tee utility like Linux. Fortunately, people have gone ahead and made such a tool for Windows. There's a port of the GNU tee to Windows which seems to be pretty good. There's also another open-source one here, but it's very slow. The GNU one seems a bit better.
With a third-party Windows tee under out belt, we can now make it a teelog.bat for Windows:
@echo off
set TEELOG_PROGNAME=%~nx1
%* 2>&1 | tee.exe %TEELOG_PROGNAME%.log
Make sure both teelog.bat and tee.exe are somewhere on your PATH (The Windows directory works well enough, as long as you have admin rights on your system.) Then just like on Linux:
> teelog wget --help
Whee!
*: Yes, I know the output of "wget --help" does fit with scrolling. And yes, I know Windows doesn't come with wget. But you can get it for Windows.
Read more