Question

Is there any way to export the colors of an output of a command?

Let's explain it with a small example:

ls -alh --color=auto

will print the colored contents of the directory, while

ls -alh --color=auto | cat

won't print some color. What I want to know is a trick or a tool, let's call it magic, that restores these commands like \033[1m, so that colors are available for latter processing:

ls -alh --color=auto | magic | cat

or

ls -alh --color=auto | magic >> file

Update:
I'm using ls just for this example, but want to know whether there is a general possibility.

Was it helpful?

Solution

script outputfile command 

will do the trick. e.g:

script capture.txt ls --color=always

OTHER TIPS

Since the color codes are actually part of ls output, there is no way to "restore" them (since they are not there in the first place).

But if you use ls --color=always, ls will output color codes even when used in non-interactive mode.

Basically, ls is being smart, and detecting when the output isn't going to a terminal. If you want to tell it to be less so, try ls --color=always.

Using color to distinguish file types is disabled both by default and with --color=never. With --color=auto, ls emits color codes only when standard output is connected to a terminal. The LS_COLORS environment variable can change the settings. Use the dircolors command to set it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top