Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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)
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.
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.
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):
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).
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...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.