67

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 :-)

Tom Goodfellow
  • 867
  • 1
  • 6
  • 7
  • Thanks for the question. I needed this so I could pipe the output of tree into enscript to get contol of the print formatting (using dprint/dprintm from my duplexpr package http://sourceforge.net/projects/duplexpr/.) – Joe Jun 01 '14 at 22:09

3 Answers3

89

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
terdon
  • 234,489
  • 66
  • 447
  • 667
  • 1
    There's no GNU `tree`, there's no Unix `tree`, there's no POSIX `tree`. The only `tree` implementation I'm aware of is http://mama.indstate.edu/users/ice/tree/. – Stéphane Chazelas Apr 29 '14 at 11:08
  • 3
    @StephaneChazelas thanks, so the `--charset` option should always be available then. Answer edited. – terdon Apr 29 '14 at 11:10
  • Splendid! (especially the sed version - for me sed is the wild lands beyond the awk frontier) – Tom Goodfellow Apr 29 '14 at 11:41
  • Now for a homework exercise I could add the other 125 box drawing chars to (try to) make a generic remapper. A crude one would be quite straightforward, eg all 19 cross-overs become just '+' - the two days would be used trying to get something that captures the full strange beauty of box drawings – Tom Goodfellow Apr 29 '14 at 11:51
  • 3
    I like the tree --charset=ascii option, thanks – ling Apr 01 '16 at 19:44
  • 10
    Actually I think the `--charset` option should be mentioned first - the alternative using `sed` is instructive, but more complicated... – rob74 May 31 '16 at 11:34
  • @rob74 I mentioned it second because i) I only discovered it after posting the `sed` solution and ii) because I don't know how portable it is. – terdon May 31 '16 at 11:39
  • Your sed version is missing a `s/│/|/g;` – pyrocrasty Feb 11 '17 at 08:27
44

What about tree --charset unicode ?

|-- boot_print
|   |-- config-2.6.32-5-amd64
|   |-- grub
|   |   |-- 915resolution.mod
|   |   |-- acpi.mod
|   |   |-- affs.mod
|   |   |-- afs_be.mod
|   |   |-- afs.mod
|   |   |-- aout.mod
|   |   |-- ata.mod
|   |   |-- ata_pthru.mod
|   |   |-- at_keyboard.mod
|   |   |-- befs_be.mod
|   |   |-- befs.mod
|   |   |-- biosdisk.mod
|   |   |-- bitmap.mod
|   |   |-- bitmap_scale.mod
|   |   |-- blocklist.mod
|   |   |-- boot.img
7

I tried the following to change locale. It also outputs ascii code draw lines same as --charset=ascii.

> LANG=C tree
Joe
  • 75
  • 1
  • 6