LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Saving the output of "ls" command in color. (https://www.linuxquestions.org/questions/linux-newbie-8/saving-the-output-of-ls-command-in-color-846491/)

ntubski 11-25-2010 03:20 PM

Code:

$ man cat
...
      -v, --show-nonprinting
              use ^ and M- notation, except for LFD and TAB

In other words cat -v translates the escape sequences into a plain text representation.


Code:

% echo -en '\r' | cat | xxd
0000000: 0d                                      .
% echo -en '\r' | cat -v | xxd
0000000: 5e4d                                    ^M
% echo -n '^M' | cat -v | xxd                                                    ~
0000000: 5e4d                                    ^M


MTK358 11-25-2010 03:55 PM

This works for me:

Code:

ls --color=always > file
cat file

and it will show in color (but only in a terminal, because it understands the color codes, while GUI editors will show funny characters).

This link, even though it's mainly to explain how to color your prompt, has a lot of info about the escape codes:

https://wiki.archlinux.org/index.php/Color_Bash_Prompt

David the H. 11-26-2010 12:33 AM

Quote:

Originally Posted by ntubski (Post 4171129)
In other words cat -v translates the escape sequences into a plain text representation.

We can confirm that. While the printed output may look the same, the ^[ from ls --color is an actual ascii escape character (hex code 1B), while filtering it through cat -v changes it to a literal carat and bracket.

Code:

$ mkdir dir
$ ls -d --color dir >testfile.txt
$ uniname testfile.txt
character  byte      UTF-32  encoded as    glyph  name
        0          0  00001B  1B                    ESCAPE
        1          1  00005B  5B            [      LEFT SQUARE BRACKET
        2          2  000030  30            0      DIGIT ZERO
        3          3  00006D  6D            m      LATIN SMALL LETTER M
        4          4  00001B  1B                    ESCAPE
        5          5  00005B  5B            [      LEFT SQUARE BRACKET
        6          6  000030  30            0      DIGIT ZERO
        7          7  000031  31            1      DIGIT ONE
        8          8  00003B  3B            ;      SEMICOLON
        9          9  000033  33            3      DIGIT THREE
      10        10  000034  34            4      DIGIT FOUR
      11        11  00006D  6D            m      LATIN SMALL LETTER M
      12        12  000064  64            d      LATIN SMALL LETTER D
      13        13  000069  69            i      LATIN SMALL LETTER I
      14        14  000072  72            r      LATIN SMALL LETTER R
      15        15  00001B  1B                    ESCAPE
      16        16  00005B  5B            [      LEFT SQUARE BRACKET
      17        17  000030  30            0      DIGIT ZERO
      18        18  00006D  6D            m      LATIN SMALL LETTER M
      19        19  00000A  0A                    LINE FEED (LF)

Code:

$ ls -d --color dir |cat -v >testfile.txt
$ uniname testfile.txt
character  byte      UTF-32  encoded as    glyph  name
        0          0  00005E  5E            ^      CIRCUMFLEX ACCENT
        1          1  00005B  5B            [      LEFT SQUARE BRACKET

        2          2  00005B  5B            [      LEFT SQUARE BRACKET
        3          3  000030  30            0      DIGIT ZERO
        4          4  00006D  6D            m      LATIN SMALL LETTER M
        5          5  00005E  5E            ^      CIRCUMFLEX ACCENT
        6          6  00005B  5B            [      LEFT SQUARE BRACKET

        7          7  00005B  5B            [      LEFT SQUARE BRACKET
        8          8  000030  30            0      DIGIT ZERO
        9          9  000031  31            1      DIGIT ONE
      10        10  00003B  3B            ;      SEMICOLON
      11        11  000033  33            3      DIGIT THREE
      12        12  000034  34            4      DIGIT FOUR
      13        13  00006D  6D            m      LATIN SMALL LETTER M
      14        14  000064  64            d      LATIN SMALL LETTER D
      15        15  000069  69            i      LATIN SMALL LETTER I
      16        16  000072  72            r      LATIN SMALL LETTER R
      17        17  00005E  5E            ^      CIRCUMFLEX ACCENT
      18        18  00005B  5B            [      LEFT SQUARE BRACKET

      19        19  00005B  5B            [      LEFT SQUARE BRACKET
      20        20  000030  30            0      DIGIT ZERO
      21        21  00006D  6D            m      LATIN SMALL LETTER M
character  byte      UTF-32  encoded as    glyph  name
      22        22  00000A  0A                    LINE FEED (LF)


colucix 11-26-2010 02:45 AM

I didn't know about the uniname command from the uniutils package. Thank you David for revealing it. Regarding the weird behaviors, I think they have been clarified. Thanks to all.

smurthygr 11-26-2010 02:53 AM

Very interesting stuff, but not the answer I was looking for. I want to know if the colors can be saved into a file so that the file can be printed showing different file types in different colors. Anyway thanks for the answers.

colucix 11-26-2010 04:43 AM

Quote:

Originally Posted by smurthygr (Post 4171526)
Very interesting stuff, but not the answer I was looking for. I want to know if the colors can be saved into a file so that the file can be printed showing different file types in different colors. Anyway thanks for the answers.

Given the discussion above, it depends on the application by which you open the file. If it can interpret ansi escape sequences (like a terminal) the answer is yes.

The correct way to write the file can be either one of the following (as shown before):
Code:

\ls --color > file
/bin/ls --color > file
ls --color=always > file


brianL 11-26-2010 05:59 AM

smurthygr wants it printed out on paper as it's seen in the terminal.

colucix 11-26-2010 07:10 AM

Quote:

Originally Posted by brianL (Post 4171667)
smurthygr wants it printed out on paper as it's seen in the terminal.

Oops... I didn't notice that... the requirement was not specified in the original post. Thanks brianL.

Unfortunately, I'm not aware of any tool to interpret ansi color escape sequences and convert them to Postscript colors (it looks like enscript and a2ps, just to mention some CLI printing tools, cannot do that).

theNbomr 11-26-2010 10:25 AM

I was going to post a nice example of how the $TERM variable can be used to modify the escape codes used by ls to generate the color output, so the OP could potentially control how the listing gets created, and then also potentially create a filter that can translate the output to something his printer can handle. However, when I set $TERM to any value that is found as a terminal type entry in /etc/termcap, 'ls --color=always' doesn't change the escape sequences that it uses to generate color output.
So, does anyone know how to control what ls uses to generate its escape codes? My testing is done on a KDE Konsole ($TERM=xterm).
Code:

#! /bin/sh

NATIVE_TERM=$TERM
ls -lash --color=always > /tmp/ls.${NATIVE_TERM}
od -t c /tmp/ls.${NATIVE_TERM} > /tmp/ls_od.native_term

for termtype in $( perl -e 'while(<>){ if($_ =~ m/^[^\s#]/){@types=split /\|/,$_; print $types[0],"\n";}}' /etc/termcap ); do
    TERM=$termtype
    # echo $TERM
    ls -lash --color=always > /tmp/ls.${TERM}
    od -t c /tmp/ls.${TERM} > /tmp/ls_od.${TERM}
    diff /tmp/ls_od.native_term /tmp/ls_od.${TERM}
done

TERM=$NATIVE_TERM

This compares the output of ls for every known terminal type on my system against the native terminal type and finds no differences whatsoever. I can see that other applications (vi, man,...) respect the $TERM variable; just not ls. Google seems silent on the subject.
Sorry if this is a hijacking of the thread. It started out as an on-topic contribution...

--- rod.

colucix 11-26-2010 09:29 PM

Quote:

Originally Posted by theNbomr (Post 4171882)
I was going to post a nice example of how the $TERM variable can be used to modify the escape codes used by ls to generate the color output, so the OP could potentially control how the listing gets created, and then also potentially create a filter that can translate the output to something his printer can handle.

The idea is correct: the problem is to create a filter to generate the proper PostScript code based on the given color escape sequences, but they cannot be changed by the ls command (basically because they are a standard). Moreover looking at the ls source code, there is no link between the terminal type and the generated colors, since the problem is if the output is connected to a terminal or not, hence if the sequences must be generated or not.

On the other hand, ls is influenced by the LS_COLORS environment variable, but only to retrieve the color definitions associated to each object and change colors accordingly.

David the H. 11-27-2010 05:25 PM

Quote:

Originally Posted by theNbomr (Post 4171882)
So, does anyone know how to control what ls uses to generate its escape codes?

They're controlled by the LS_COLORS environment variable. You can use the dircolors command to generate a properly-formatted variable entry.

Here's a detailed how to:
http://ubuntuforums.org/archive/index.php/t-41538.html

also check out man dircolors and info dircolors.

theNbomr 11-28-2010 02:11 AM

Yes, I knew that. That simply defines the relationship between file types and colors. How the colors actually get displayed on a terminal is a different matter, and is quite device-dependent. That is the part that I was trying to establish. colucix seems to have figured that part out. Now that I know where there must exist a concise list of the escape codes used, I think I will have a go at producing a basic filter that can translate the escape codes to something palatable to a printer. I don't know that Postscript is the easiest to generate, so perhaps some intermediate format, letting an existing tool do some of the heavy lifting. While researching the subject, I came across some PHP code that does the translation to some kind of HTML. Perl should do at least as well.

--- rod.

GrapefruiTgirl 11-30-2010 08:06 AM

@ ntubski,

I'm curious: on your system, what package brings `xxd` ?

ntubski 11-30-2010 09:13 AM

To my surprise it turns out to be vim-common: Debian Package Search for xxd.

GrapefruiTgirl 11-30-2010 09:21 AM

Ehh, no wonder I couldn't find it - I haven't got vim installed.

Thanks!


All times are GMT -5. The time now is 10:44 PM.