42

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

  • 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 '14 at 22:09
56

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
  • 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. – Stéphane Chazelas Apr 29 '14 at 11:08
  • 2
    @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
    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
30

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
    That's the same as tree --charset nwildner – Stéphane Chazelas Apr 29 '14 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 '14 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.