1

Why does the file output of the tree program include strange characters? Example:

$ tree -fFN --charset=utf8 -o list.txt test/

Contents of the list.txt file:

<0x1b>[01;34mtest<0x1b>[00m
├── <0x1b>[01;34mtest/folder-01<0x1b>[00m/
│   ├── test/folder-01/file-01
│   └── test/folder-01/file-02
├── <0x1b>[01;34mtest/folder-02<0x1b>[00m/
│   ├── test/folder-02/file-03
│   └── <0x1b>[01;34mtest/folder-02/folder-04<0x1b>[00m/
│       └── test/folder-02/folder-04/file-05
└── <0x1b>[01;34mtest/folder-03<0x1b>[00m/
    └── test/folder-03/file-04

What are these characters?

<0x1b>
[01;34m
[00m

I am using the Terminal app on macOS Mojave. Until a few days ago the output was correct without these additional characters.

2 Answers 2

3

I highly suspect the "contents of the list.txt file" you posted is an interpretation (representation) of the actual content, not the literal content. Then it makes sense.

<0x1b> is what the program you used to view the file prints to represent the (unprintable) ASCII escape character (in various circumstances the character may be referred to as ESC, \x1b, \033, \e, ^[).

Then <0x1b>[01;34m and similar sequences are CSI sequences. Your file contains SCI sequences that change color of the following text. Run cat list.txt to pass the file to the terminal as-is. If your terminal supports CSI sequences (it probably does) then you will see colorized text.

The sequences are there in the file because your tree put them there. In my Kubuntu man 1 tree states few options to colorize (or not) the output: -C, -n. Without any of these options the behavior depends on LS_COLORS and TREE_COLORS environment variables and on stdout being a terminal or not.

By default, if LS_COLORS (or TREE_COLORS) is set, tree will colorize its output to a terminal, but not if piped to a non-terminal (e.g. to a file or the next command in a pipeline). The quirk is if you tell it to write to a file with -o then its behavior still depends on whether stdout is a terminal or not. If you redirected stdout to a non-terminal

tree -fFN --charset=utf8 -o list.txt test/ >/dev/null
# or simply
tree -fFN --charset=utf8 test/ >list.txt

then the output would not contain color codes.


Until a few days ago the output was correct without these additional characters.

Maybe there was no LS_COLORS in your environment until few days ago; and the files created earlier don't contain color codes. Did you add LS_COLORS to the environment recently? Or maybe the earlier files contain(ed) color codes but you used to examine them in a way that actually uses CSI sequences to display colors? or ignores them?

It's not clear if you want the color codes to be in the file in the first place.

Anyway check the manual of your tree. Use a proper option to force coloring or not-coloring, or manipulate the environment and/or redirections to make tree behave as you expect.

If you need to remove color codes from already existing files then pass each file through one of the filters from this post: Removing ANSI color codes from text stream.

1

I added the -n option and the problem was solved. I had added LS_COLOR to the .bash_profile file.

1
  • For the record: in general it's fine to answer one's own question. Here, however, the explicit questions in the question body are "why does the output include strange characters?" and "what are these characters?". If your intent was to answer, then you should have answered the questions you formulated. If your intent was to give feedback to my answer, then you should have written a comment. Your answer may be not as detailed as my answer and you may even prefer accepting your own answer; but let it be an actual answer. I'm on the verge of downvoting. Jul 25, 2020 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .