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).
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).
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
xcodebuild— instead I got chopped-up lines with no color. unbuffer xcodebuild | less -R worked flawlessly, however.
– Slipp D. Thompson
Nov 24 '15 at 05:33
expect libdrm-amdgpu1 libdrm-intel1 libdrm-nouveau2 libdrm-radeon1 libdrm2 libfontenc1 libgl1-mesa-dri libgl1-mesa-glx libglapi-mesa libice6 libllvm4.0 libpciaccess0 libsm6 libtcl8.6 libtk8.6 libtxc-dxtn-s2tc0 libx11-6 libx11-data libx11-xcb1 libxau6 libxaw7 libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-shape0 libxcb-sync1 libxcb1 libxcomposite1 libxdamage1 libxdmcp6 libxext6 libxfixes3 libxft2 libxi6 libxinerama1.... Is there an option without the ridiculous 186 MB of dependencies?
– Fake Name
Jul 30 '17 at 05:45
unbuffer before exec, : unbuffer exec > > (tee $LOG_FILE) 2>&1;, I'm getting error: couldn't execute "exec": no such file or directory while executing "spawn -noecho exec" ("eval" body line 1) invoked from within
"eval [list spawn -noecho] $argv" invoked from within "if {[string compare [lindex $argv 0] "-p"] == 0}
{ # pipeline set stty_init "-echo" eval [list spawn -noecho] [lrange $argv 1 end] | clo..." (file "/usr/bin/unbuffer" line 13). How do I solve this please. Thank you.
https://askubuntu.com/q/1344347/928088. Thanks
– Jags
Jun 09 '21 at 14:10
Use the ls option --color=always
--color=auto will not color output to a pipeline - for obvious reasons.
The ls man page says the following:
With --color=auto, color codes are output only if standard output is connected to a terminal (tty).
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 02 '11 at 12:00
ls. See my answer that fixes the problem for all programs, including heroku logs.
– Eamonn O'Brien-Strain
Jun 16 '14 at 03:25
I'll expand the script solution given in the comment of the accepted answer.
Using script may be useful in case you can't or don't want to install the expect package that contains the unbuffer command.
Print ls output to stdout and file with color codes:
script -efq output.log -c "ls -l --color=auto"
where (man script):
-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
output file command. SO doesn't support this much code in a comment so I put it here.
– mcp
Apr 13 '21 at 01:08
script -efq -c "ls --color=auto" >(cat) | tee output.log
For convenience I created this script:
#!/usr/bin/bash
script -efq -c "$*" >(cat)
call it say unbuff and then
unbuff ls --color=auto | tee output.log
cating it back out (need to pipe into grep). The >(cat) saves that whole workaround. The version of script on my machine only takes [-adkpqr] args but this little function ended up working great:
unbuff () { script -q >(cat) $* >/dev/null ; }
Here is a function based off this clean answer that I couldn't fit in the comments.
output()
{
output_file=$1
shift
command=$@
script -efq $output_file -c "$command"
less $output_file
}
Usage
output file command
$ pipe-with-color(){ script -efq -O /dev/null -c "$@";}
$ pipe-with-color "git log origin/trunk.. --format='%Cred%h%Creset␟%C(cyan)%an%Creset␟%H␟%Cblue%f%Creset'" | git name-rev --annotate-stdin --always --name-only | column -t -s'␟'
pipe output and keep colors, but do not make a temp file(write the log out to /dev/null by -O option).
The upper example shows how it can be used: first, pipe the git log ... colored output to git name-rev to make it human readable(prev cmds output color keeps), then pipe again to indent for readability, the final result also keeps the git log ... output color.
script -efq -c "ls --color=auto" >(cat) | tee outpu.log– nadapez May 10 '22 at 00:38