hometools

Capture Errors from Command Line Tools

April 2026

TL;DR

Use this to capture errors along with regular output from command line tools:

2>&1

Basic Capture

For example:

rm -v example.txt 2>&1 | cat > report.txt

If the file exists and is deleted, then it's file name is output to the report-1.txt via STDOUT.

example.txt
If the file doesn't exist, or can't be removed, then an error message is output to `report-1.txt` via `STDERR`.
rm: example.txt: No such file or directory

Seeing The Output

Running the above command doesn't output anything to the terminal. You can add tee /dev/tty to see the messages there as well.

rm -v example.txt 2>&1 | tee /dev/tty | cat > report.txt

Wrap Up

The 2>&1 looks weird to me. Totally worth it to remeber though when trying to capture the output of commands that use both STDOUT and STDERR.

-a