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).
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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
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
Use the ls option
--color=auto will not color output to a pipeline - for obvious reasons.
The main page says the following:
With --color=auto, color codes are output only if standard output is connected to a terminal (tty).
I'll expand the
script solution given in the comment of the accepted answer.
script may be useful in case you can't or don't want to install the expect package that contains the
ls output to stdout and file with color codes:
script -efq output.log -c "ls -l --color=auto"
-e, --return Return the exit code of the child process. Uses the same format as bash termination on signal termination exit code is 128+n. -f, --flush Flush output after each write. This is nice for telecooperation: one person does `mkfifo foo; script -f foo', and another can supervise real-time what is being done using `cat foo'. -q, --quiet Be quiet (do not write start and done messages to either standard output or the typescript file).
View the output file with colors:
less -r output.log