color and redirection?

Mikel

Registered
I've been messing around with 'ls -G' (directory with colour) and tried to redirect the output to a file and see if any colour info was retained. It didn't seem to.

'ls -G > file'
'cat file'

Anybody know how to could keep the colour info, or if it's possible?
 
The color is just in the terminal, it has nothing to do with the file names themselves. There is not a way to have the colorized output redirected.
 
It's actually quite easy. Add this option to ls:

--color=always

Otherwise it'll detect that it's sending output to a pipe rather than a terminal and deactivate printing of color escape codes.
 
Actually, since the output of the command is sent to stdout, and > and | both redirect stdout, they are basically the same. Only > sends to a file, and | sends to stdin of the next command.

you have to remeber, most everything on a UNIX machine are files, and files are streams of ASCII bytes. STDOUT/STDERR/STDIN are all files, so you can deal with them the same way you deal with text files/data files/etc...
 
Back
Top