Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
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

2 Answers 2

up vote 4 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
1  
Cool trick! Thanks! –  Paweł Gościcki Jun 16 at 14:07
    
this is really the correct answer.. –  Kokizzu Nov 21 at 3:14

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 have a look at /usr/local/heroku/lib/heroku/helpers/log_displayer.rb:20 and remove the condition that checks if you're on a terminal! –  Johann Philipp Strathausen May 30 '13 at 15:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.