71

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

1
  • 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 sourceforge.net/projects/duplexpr/.)
    – Joe
    Jun 1, 2014 at 22:09

3 Answers 3

92

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
8
  • 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 mama.indstate.edu/users/ice/tree. Apr 29, 2014 at 11:08
  • 3
    @StephaneChazelas thanks, so the --charset option should always be available then. Answer edited.
    – terdon
    Apr 29, 2014 at 11:10
  • Splendid! (especially the sed version - for me sed is the wild lands beyond the awk frontier) Apr 29, 2014 at 11:41
  • 3
    I like the tree --charset=ascii option, thanks
    – ling
    Apr 1, 2016 at 19:44
  • 12
    Actually I think the --charset option should be mentioned first - the alternative using sed is instructive, but more complicated...
    – rob74
    May 31, 2016 at 11:34
45

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
2
  • 2
    That's the same as tree --charset nwildner Apr 29, 2014 at 11:10
  • Yup. The other answer was edited so, i didn´t noticed that @terdon gave a more complete answer while i has writing mine ;)
    – user34720
    Apr 29, 2014 at 11:17
8

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

> LANG=C tree

You must log in to answer this question.

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