Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
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 at 22:09

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer
    
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 at 11:08
    
@StephaneChazelas thanks, so the --charset option should always be available then. Answer edited. –  terdon Apr 29 at 11:10
    
Splendid! (especially the sed version - for me sed is the wild lands beyond the awk frontier) –  Tom Goodfellow Apr 29 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 at 11:51

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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