Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
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).

share|improve this question
up vote 25 down vote accepted

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
share|improve this answer
Cool trick! Thanks! – Paweł Gościcki Jun 16 '14 at 14:07
this is really the correct answer.. – Kokizzu Nov 21 '14 at 3:14
Another solution, which does not require installing anything, is at… – Tgr Jan 31 '15 at 2:51
This causes the resulting file to contain color codes (of course); is there any way to then print the file in a way that makes use of the color codes and properly displays the colors in the terminal? – Kyle Strand Jul 10 '15 at 18:48
Ugh, that makes password entries show your password in cleartext! – AndiDog Aug 20 '15 at 13:37

Use the ls option --color=always normally it will not color output to a pipeline - for obvious reasons.

The man page says

With --color=auto, color codes are output only if standard output is connected to a terminal (tty).

share|improve this answer
OK. That explains it. But can I still somehow see the colors on the screen? (It's a TTY after all). I don't mind NOT having them in the logfile, but I surely want them on my screen. – Paweł Gościcki Nov 2 '11 at 10:46
I think I made myself not clear enough. ls -l was just an example. I have a completely different command (heroku logs) that strips colors when piped to tee. And I want to "fix/change" tee/pipe, not the command I'm executing. – Paweł Gościcki Nov 2 '11 at 12:00
@Pawel, you can't easily fix it in tee/pipe as tee/pipe are not stripping these color codes. The problem is that the initial command sees it is not writing to the terminal. You need a pseudo-terminal that acts like a pipe but which commands see as a terminal. – RedGrittyBrick Nov 2 '11 at 16:10
Hm... fair enough. I guess I just need to accept that it's how it is. – Paweł Gościcki Nov 3 '11 at 15:08
@PawełGościcki this answer only fixes the problem for ls. See my answer that fixes the problem for all programs, including heroku logs. – Eamonn O'Brien-Strain Jun 16 '14 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .