“tree” command output with “pure” (7-bit) ASCII output

asciicommand linetree

The "tree" command uses nice box-drawing characters to show the tree but I want to use the output in a "code-page-neutral" context (I know that really there's always a code page, but by restricting it to the lower characters I hope to be free of worries that someone in Ulan Bator sees smiley faces, etc).

For example instead of:

├── include
│   ├── foo
│   └── bar

I'd like something like:

+-- include
|   +-- foo
|   \-- bar

but none of the "tree" switch combinations I tried gave this (seems more as if they take the box-drawing chars as the baseline and make it yet prettier)

I also looked for box-drawing filters to perform such conversions without finding anything beyond an infinite amount of ASCII art :-). A generic filter
smells like something to be cooked-up in 15 mins – plus two more incremental days stumbling into all the amusing corner cases 🙂

Best Answer

I'm not sure about this but I think all you need is

tree | sed 's/├/\+/g; s/─/-/g; s/└/\\/g'

For example:

$ tree
.
├── file0
└── foo
    ├── bar
    │   └── file2
    └── file1

2 directories, 3 files
$ tree | sed 's/├/\+/g; s/─/-/g; s/└/\\/g'
.
+-- file0
\-- foo
    +-- bar
    │   \-- file2
    \-- file1

2 directories, 3 files

Alternatively, you can use the --charset option:

$ tree --charset=ascii
.
|-- file0
`-- foo
    |-- bar
    |   `-- file2
    `-- file1

2 directories, 3 files
Related Question