Preserve colors while piping to tee

pipetee

ls -l --color=auto | tee output.log

Without pipe/tee it's colored. How can I make it so that it stays colored while using tee (can be colored only on the screen, I don't care about colors in logs).

Best Answer

Simply insert unbuffer before any command to make it think it is writing to an interactive output even if it is actually piping into another executable. This will preserve color in the case of ls.

For example

unbuffer ls -l --color=auto | tee output.log

If you don't already have it installed, on Ubuntu and other Debian-ish Linux distributions you can install unbuffer by doing.

sudo apt-get install expect-dev