Saving the output of "ls" command in color.
When I save the output of "ls" command to a "txt" or "rtf" file, the color information is lost. Is there any way to retain the color code of different files/directories obtained by the command "ls --color" while saving?
|
You could do this, using `cat -v`:
Code:
/bin/ls --color=always | cat -v |
Check your alias: it should be something like this:
Code:
$ alias ls Code:
$ \ls --color > file Edit: GrapefruiTgirl beat me for a bunch of seconds! :) |
Didn't work for me - here's what I got:
Code:
^[[0m^[[00m#Caravan#^[[0m Code:
ls --color=always | cat -v > ls.txt |
@mlangdn: your output is exactly what the OP was looking for. It contains the color codes as they can be interpreted by the echo statement. Look at the Bash Prompt Howto, here for an example of their usage.
|
Hmm, yes interesting. I didn't notice that there are other ^[ control characters there which are not part of the color codes. The color codes are there, but they don't work as expected when the file is read.
This is clunky and possibly not fool-proof, but is a workaround for those extra control characters, if you want to actually see the colors when viewing the file of saved output from the command I/you used: Code:
echo -e "$(sed 's|\^\[|\\033|g' ls.txt)" |
Quote:
|
That worked. Its really nothing I need - I just did it out of boredom this morning! Happy Turkey Day!
|
There's no need to use cat -v when piping output. ls color=always >file is all that's required.
For the record, colors in terminal programs are provided by ansi escape code sequences. These are non-printing characters that can tell the console how to format the output. Most programs that can provide color output are smart enough to turn it off when directed into a pipe or file, because most of the time you simply want the text itself. The existence of the embedded codes can cause havoc with further text processing tools, for example. In addition, the program that displays the text has to be able to understand them. Your terminal can do it, as do less and some cli text editors with the proper options enabled. But gui editors like kwrite or gedit or OpenOffice generally have no ability to interpret the codes, and so they will simply appear as strings of odd characters embedded in the text. So yes, you can save the "colors", but it may not be what you really want after all. |
Quote:
Code:
echo -e "$(cat -v ls.txt | sed 's|\^\[|\\033|g')" Code:
cat ls.txt |
Quote:
|
Quote:
|
Quote:
Quote:
|
to escape sequence, or not to escape sequence.
That's what I thought, but no, it is not my xterm. I have discovered what is causing the difference for me - it was that I had initially included the `cat -v` in the pipline when I first saved my ls.txt file. But why it is causing the difference, is still beyond me. This is very odd, look below... I create 4 files, each containing a slightly different dump of the colored `ls` output. Two outputs have been piped through `cat -v` and dumped into a file, and two have not.:
Code:
# create 4 files: Code:
root@reactor: ls -l Code:
root@reactor: file ls1.txt :scratch: What have I gotten into here?! BTW - sorry to the OP - I hope I am not dragging this off-course too much, and/or that you are also finding this educational (or at the least, interesting!) |
He he... just to add more confusion, here is my output (similar but not identical) with escape sequences along with the output of od -c:
Code:
$ ls -l *.txt |
All times are GMT -5. The time now is 10:40 AM. |