LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 11-25-2010, 03:20 PM   #16
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,446

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829

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
 
1 members found this post helpful.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-25-2010, 03:55 PM   #17
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
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

Last edited by MTK358; 11-25-2010 at 04:00 PM.
 
1 members found this post helpful.
Old 11-26-2010, 12:33 AM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by ntubski View Post
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)
 
1 members found this post helpful.
Old 11-26-2010, 02:45 AM   #19
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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.
 
Old 11-26-2010, 02:53 AM   #20
smurthygr
LQ Newbie
 
Registered: May 2008
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
Old 11-26-2010, 04:43 AM   #21
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by smurthygr View Post
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
 
Old 11-26-2010, 05:59 AM   #22
brianL
LQ 5k Club
 
Registered: Jan 2006
Location: Oldham, Lancs, England
Distribution: Slackware & Slackware64 14.1
Posts: 6,969
Blog Entries: 52

Rep: Reputation: Disabled
smurthygr wants it printed out on paper as it's seen in the terminal.
 
Old 11-26-2010, 07:10 AM   #23
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by brianL View Post
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).
 
Old 11-26-2010, 10:25 AM   #24
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 11-26-2010, 09:29 PM   #25
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by theNbomr View Post
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.
 
Old 11-27-2010, 05:25 PM   #26
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Quote:
Originally Posted by theNbomr View Post
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.
 
Old 11-28-2010, 02:11 AM   #27
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 11-30-2010, 08:06 AM   #28
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
@ ntubski,

I'm curious: on your system, what package brings `xxd` ?
 
Old 11-30-2010, 09:13 AM   #29
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,446

Rep: Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829Reputation: 829
To my surprise it turns out to be vim-common: Debian Package Search for xxd.
 
Old 11-30-2010, 09:21 AM   #30
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

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

Thanks!
 
  


Reply

Tags
colors, commands, ls


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Want to run a command per file from the output of "find" command kuldeep.k Linux - General 3 09-08-2010 05:20 AM
square brackets in output of "ps aux" not matching output of "ps -ejH" alirezan1 Linux - Newbie 14 07-14-2010 04:17 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM
Make "ls" command show colors without "--color=always" switch SharpyWarpy Linux - General 4 07-16-2007 11:00 PM
open office "input/output error" saving to smb share PK2K Linux - Software 6 03-09-2005 01:30 AM


All times are GMT -5. The time now is 09:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration