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– Joe2014年06月01日 22:09:11 +00:00Commented Jun 1, 2014 at 22:09
-
Thanks for posting. I was a bit stumped trying to copy and paste my plaintext art into html pages with all the garbled fonts.Sridhar Sarnobat– Sridhar Sarnobat2024年07月23日 23:25:42 +00:00Commented Jul 23, 2024 at 23:25
3 Answers 3
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
-
1There's no GNU
tree
, there's no Unixtree
, there's no POSIXtree
. The onlytree
implementation I'm aware of is mama.indstate.edu/users/ice/tree.Stéphane Chazelas– Stéphane Chazelas2014年04月29日 11:08:15 +00:00Commented Apr 29, 2014 at 11:08 -
3@StephaneChazelas thanks, so the
--charset
option should always be available then. Answer edited.2014年04月29日 11:10:06 +00:00Commented Apr 29, 2014 at 11:10 -
Splendid! (especially the sed version - for me sed is the wild lands beyond the awk frontier)Tom Goodfellow– Tom Goodfellow2014年04月29日 11:41:53 +00:00Commented Apr 29, 2014 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 drawingsTom Goodfellow– Tom Goodfellow2014年04月29日 11:51:07 +00:00Commented Apr 29, 2014 at 11:51
-
13Actually I think the
--charset
option should be mentioned first - the alternative usingsed
is instructive, but more complicated...rob74– rob742016年05月31日 11:34:21 +00:00Commented May 31, 2016 at 11:34
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
-
2That's the same as
tree --charset nwildner
Stéphane Chazelas– Stéphane Chazelas2014年04月29日 11:10:32 +00:00Commented 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– user347202014年04月29日 11:17:19 +00:00Commented Apr 29, 2014 at 11:17
I tried the following to change locale. It also outputs ascii code draw lines same as --charset=ascii.
> LANG=C tree
-
Damn, I was hoping this would work for this really nice tool but I guess software is not magic, it needs to be supported: github.com/yzhong52/ascii_treeSridhar Sarnobat– Sridhar Sarnobat2024年07月23日 23:33:02 +00:00Commented Jul 23, 2024 at 23:33