How to write stdout to file with colors?

LinuxBashTerminalStdout

Linux Problem Overview


A lot of times (not always) the stdout is displayed in colors. Normally I keep every output log in a different file too. Naturally in the file, the colors are not displayed anymore.

Console color output example

I'd like to know if there's a way (in Linux) to write the output to a file with colors. I'm trying to use tee to write the output of vagrant to a file, this way I can still see the output (when it applies). I want to use it specifically for vagrant (it may change in the future, of course...)

Linux Solutions


Solution 1 - Linux

Since many programs will only output color sequences if their stdout is a terminal, a general solution to this problem requires tricking them into believing that the pipe they write to is a terminal. This is possible with the script command from bsdutils:

script -q -c "vagrant up" filename.txt

This will write the output from vagrant up to filename.txt (and the terminal). If echoing is not desirable,

script -q -c "vagrant up" filename > /dev/null

will write it only to the file.

Solution 2 - Linux

You can save the ANSI sequences that colourise your output to a file:

echo a | grep --color=always . > colour.txt
cat colour.txt

Some programs, though, tend not to use them if their output doesn't go to the terminal (that's why I had to use --color-always with grep).

Solution 3 - Linux

You can also color your output with echo with different colours and save the coloured output in file. Example

echo -e '\E[37;44m'"Hello World" > my_file

Also You would have to be acquainted with the terminal colour codes

Using tee

< command line > |tee -a 'my_colour_file'

Open your file in cat

cat 'my_colour_file'

Using a named pipe can also work to redirect all output from the pipe with colors to another file

for example

Create a named pipe

mkfifo pipe.fifo

each command line redirect it to the pipe as follows

<command line> > pipe.fifo

In another terminal redirect all messages from the pipe to your file

cat pipe.fifo > 'my_log_file_with_colours'

open your file with cat and see the expected results.

Solution 4 - Linux

In Ubuntu, you can install the package bsdutils to output to a text file with ANSI color codes:

script -q -c "ls --color=always" /tmp/t

Install kbtin to generate a clean HTML file:

ls --color=always | ansi2html > /tmp/t.html

Install aha and wkhtmltopdf to generate a nice PDF:

ls --color=always | aha | wkhtmltopdf - /tmp/t.pdf

Use any of the above with tee to display the output also on the console or to save a copy in another file. Example:

ls --color=always | tee /dev/stderr | aha | wkhtmltopdf - /tmp/test.pdf

Solution 5 - Linux

I found out that using the tool called ansi2html.sh

Is the most simple way to export colorful terminal data to html file,

The commands to use it are:

ls --color=always | ansi2html.sh --palette=solarized > ~/Desktop/ls.html
  • All is needed is to send the output using a pipe and then output the stdout to simple html file

Solution 6 - Linux

Solution

$ script -q /dev/null -c "your command" > log.txt
$ cat log.txt

Explanation

According to the man page of script, the --quit option only makes sure to be quiet (do not write start and done messages to standard output). Which means that the start and done messages will always be written to the file.

In order to utilize script and discard the output file at the same file, we can simply specify the null device /dev/null to it! Also, redirect the output to our desired destination and the color content will be written to the destination.

Solution 7 - Linux

On macOS, script is from the BSD codebase and you can use it like so:

script -q /dev/null mvn dependency:tree mvn-tree.colours.txt

It will run mvn dependency:tree and store the coloured output into mvn-tree.colours.txt

The tee utility supports colours, so you can pipe it to see the command progress:

script -q /dev/null mvn dependency:tree | tee mvn-tree.colours.txt

To get the script manual you can type man script:

SCRIPT(1)                 BSD General Commands Manual                SCRIPT(1)

NAME
     script -- make typescript of terminal session

SYNOPSIS
     script [-adkpqr] [-F pipe] [-t time] [file [command ...]]

Solution 8 - Linux

I was trying out some of the solutions listed here, and I also realized you could do it with the echo command and the -e flag.

$ echo -e "\e[1;33m This is yellow text \e[0m" > sample.txt

Next, we can view the contents of our sample.txt file.

$ cat sample.txt

Click link to see the output in yellow

Additionally, we can also use tee and pipe it with our echo command:

echo -e "\e[1;33m This is yellow text \e[0m" | tee -a sample.txt

Solution 9 - Linux

In the RedHat/Rocky/CentOS family, the ansi2html utility does not seem to be available (except for Fedora 32 and up). An equivalent utility is ansifilter from the EPEL repository. Unfortunately, it seems to have been removed from EPEL 8.

script is preinstalled from the util-linux package.

To set up:

yum install ansifilter -y

To use it:

ls --color=always | ansifilter -H > output.html

To generate a pretty PDF (not tested), have ansifilter generate LaTeX output, and then post-process it:

ls --color=always | ansifilter -L | pdflatex >output.pdf

Obviously, combine this with the script utility, or whatever else may be appropriate in your situation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAAlvzView Question on Stackoverflow
Solution 1 - LinuxWintermuteView Answer on Stackoverflow
Solution 2 - LinuxchorobaView Answer on Stackoverflow
Solution 3 - LinuxrepzeroView Answer on Stackoverflow
Solution 4 - LinuxIvan OgaiView Answer on Stackoverflow
Solution 5 - LinuxPini CheyniView Answer on Stackoverflow
Solution 6 - LinuxWinDerekView Answer on Stackoverflow
Solution 7 - LinuxpybView Answer on Stackoverflow
Solution 8 - LinuxSynt4x 4rr0rView Answer on Stackoverflow
Solution 9 - LinuxKevin KeaneView Answer on Stackoverflow