ASCII Library for Creating "Pretty" Directory Trees?
Asked Answered
S

10

94

Is there some *nix tool or perl/php library that will let you easily create directory tree visualizations that look like the following?

www
|-- private
|    |-- app 
|    |    |-- php
|    |    |    |-- classes
|    |    |    +-- scripts
|    |    |-- settings
|    |    +-- sql
|    +-- lib
|         +-- ZendFramework-HEAD
+-- public
    |-- css
    |-- images
    +-- scripts
Suspensor answered 17/10, 2009 at 6:18 Comment(1)
If anyone is looking for this solution on Windows (I was!), you can simply type tree at the command line.Durwyn
L
126

How about this example from Unix Tree / Linux Tree:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'  
Luedtke answered 17/10, 2009 at 7:2 Comment(8)
You gotta love a one-linerPettway
That's disgustingly awesome, but part of what I like about the output above is how it's not all dashes to the files, you get those pipes connecting subdirectories verticallySuspensor
You might check this person's solution out then: sandeep-vaniya.blogspot.com/2008/04/…Luedtke
great! although not quite right output (I'm not critizing so much as adding constuctive critism) - the files in sub-directories are all being group together when I tried it?Jumpy
Does anyone know how you would add to that script so that it also shows files in the directory?Bubbler
could you give example output?Tropaeolin
Here is some example output. It only includes the directories, not the files within them:. |-power |-properties |---advanced_battery_charging |---peakshift |-rtc |---rtc1 |-----powerCarousel
Well I editted that previous comment so many times it stopped letting me change it lol. It's apparently impossible to add line breaks in comments so I replaced them with "\n": Here is some example output. It only includes the directories, not the files within them (and it's apparently impossible to put line breaks in comments, so I put in "\n" at every break: .\n |-power\n |-properties\n |---advanced_battery_charging\n |---peakshift\n |-rtc\n |---rtc1\n |-----powerCarousel
S
89

That oneliner is pretty cool, I'd recommend using the tree util.

bash-3.2$ mkdir -p this/is/some/nested/example
bash-3.2$ mkdir -p this/is/another/super/nested/example
bash-3.2$ mkdir -p this/is/yet/another/example
bash-3.2$ mkdir -p this/is/some/nested/other/example
bash-3.2$ tree this
this
`-- is
    |-- another
    |   `-- super
    |       `-- nested
    |           `-- example
    |-- some
    |   `-- nested
    |       |-- example
    |       `-- other
    |           `-- example
    `-- yet
        `-- another
            `-- example

13 directories, 0 files
Stanton answered 14/11, 2012 at 7:20 Comment(3)
For those on a Mac, brew install treeTalkingto
I struggled with tree using special characters as default, but suggestion from codealpha.net/696/… is to use tree --charset=ASCII, so that tree produces chars as in user1116793's example above.Mccall
It has some creative uses too. I use it to visualize my errands (most tools don't have true hierarchical capability - not even Jira).Lianaliane
D
20

I realize this question was answered ages ago, but I just found this program called tree which is pretty cool too.

Drisko answered 10/6, 2010 at 22:33 Comment(3)
This should be marked as the correct answer. It supports dircolors, and the output is more nicely laid out. It's even a macport too for Mac OS X users.Lianaliane
tree can also be installed using Homebrew, for those who've moved on from Macports.Telegony
It should be noted that, for Windows, you can simply type tree from the command line. No need to install anything.Durwyn
S
16

See the RecursiveTreeIterator class

Allows iterating over a RecursiveIterator to generate an ASCII graphic tree.

$treeIterator = new RecursiveTreeIterator(
    new RecursiveDirectoryIterator('/path/to/dir'),
    RecursiveTreeIterator::SELF_FIRST);

foreach($treeIterator as $val) echo $val, PHP_EOL;

Output will be something like this (with c:\php on my machine):

|-c:\php5\cfg
|-c:\php5\data
| |-c:\php5\data\Base
| | \-c:\php5\data\Base\design
| |   |-c:\php5\data\Base\design\class_diagram.png
| |   \-c:\php5\data\Base\design\design.txt
| |-c:\php5\data\ConsoleTools
| | \-c:\php5\data\ConsoleTools\design
| |   |-c:\php5\data\ConsoleTools\design\class_diagram.png
| |   |-c:\php5\data\ConsoleTools\design\console.png
| |   |-c:\php5\data\ConsoleTools\design\console.xml
…
Sodamide answered 16/11, 2010 at 16:23 Comment(0)
A
15

exa with --tree does an excellent job:

exa --tree ~/tmp/public/

<dir>
├── aboutme
│  └── index.html
├── atrecurse
│  └── index.html
├── base.css
├── html5
│  ├── cat-and-mouse
│  └── frantic
│     ├── css
│     │  └── main.css
Autotransformer answered 3/4, 2018 at 17:1 Comment(1)
Found this late but this is awesome! Developing a tool for generating the file tree for a certain type of project and exa -T -D has been really really helpful. Thanks!Psoriasis
H
12

Not a library per se, but this little utility is handy for generating quick tree graphs without leaving the browser: https://tree.nathanfriend.io/

Disclaimer: I'm the author :).

Hurty answered 10/12, 2019 at 16:56 Comment(2)
the best answerYocum
This is really nice! bookmarkedElijaheliminate
A
1

Cool Python script to do it: http://code.activestate.com/recipes/217212/

Animalcule answered 4/2, 2011 at 19:52 Comment(0)
C
1

[php ]To tweak the tree symbols, taken from https://gist.github.com/hakre/3599532

<?php
$path = './targetdir';
$unicodeTreePrefix = function(RecursiveTreeIterator $tree){
  $prefixParts = [
      RecursiveTreeIterator::PREFIX_LEFT         => ' ',
      RecursiveTreeIterator::PREFIX_MID_HAS_NEXT => '+ ',
      RecursiveTreeIterator::PREFIX_END_HAS_NEXT => '├ ',
      RecursiveTreeIterator::PREFIX_END_LAST     => '└ '
    ];
  foreach ($prefixParts as $part => $string) {
      $tree->setPrefixPart($part, $string);
  }
};
$dir  = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_FILENAME | RecursiveDirectoryIterator::SKIP_DOTS);
$tree = new RecursiveTreeIterator($dir);
$unicodeTreePrefix($tree);
echo "<br><br>";
echo "[$path]<br>";
foreach ($tree as $filename => $line) {
  echo $tree->getPrefix(), $filename, "<br>";
}

Example output

[./targetdir]<br> ├ aHR0cHM<br> ├ gtyyu.txt<br> ├ Screenshot at 2020-05-28 22-23-30.png<br> ├ 2004 - Synchrone<br> + ├ 09-Live for willyman.mp3<br> + ├ 04-Inabox.mp3<br> + ├ 05-Trashastan.mp3<br> + ├ 07-Nordick.mp3<br> + ├ 08-Rupture.mp3<br> + ├ Best of<br> + + ├ 08 - Civil War.mp3<br> + + ├ 09 - 14 Years.mp3<br> + + ├ 05 - Welcome To The Jungle.mp3<br> + + ├ 06 - Don't Cry.mp3<br> + + ├ 04 - Sweet Child O' Mine.mp3<br> + + ├ 02 - Paradise City.mp3<br> + + ├ 07 - Yesterdays.mp3<br> + + ├ 03 - Patience.mp3<br> + + ├ 01 - November Rain.mp3<br> + + └ 10 - Estranged.mp3<br> + ├ 03-Sarangui.mp3<br> + ├ 06-The test.mp3<br> + ├ 01-Sabradub.mp3<br> + └ 02-L'uzure.mp3<br> ├ Screenshot at 2020-02-11 12-31-52.png<br> ├ trur.txt<br> ├ .hidden<br> + ├ .sub_article.txt<br> + └ sub_article_in_hidden.txt<br> ├ gtuitre.txt<br> ├ aHR0cHM.txt<br> ├ CREEP.mp3<br> ├ subfolder<br> + └ sub_article.txt<br> ├ filtle.txt<br> ├ Best of<br> + ├ 08 - Civil War.mp3<br> + ├ 09 - 14 Years.mp3<br> + ├ 05 - Welcome To The Jungle.mp3<br> + ├ 06 - Don't Cry.mp3<br> + ├ 04 - Sweet Child O' Mine.mp3<br> + ├ 02 - Paradise City.mp3<br> + ├ 07 - Yesterdays.mp3<br> + ├ 03 - Patience.mp3<br> + ├ 01 - November Rain.mp3<br> + └ 10 - Estranged.mp3<br> ├ Screenshot at 2020-05-12 14-51-56.png<br> ├ of.txt<br> ├ highlight.css<br> └ Screenshot at 2020-06-10 19-28-51.png<br>
Cellule answered 13/6, 2020 at 21:28 Comment(0)
I
1

This has moved on a lot in recent years. The linux version in the package managers is cleaner and colorized:

Debian/Ubuntu:

sudo apt install tree

CentOS/RHEL/OpenSUSE:

sudo yum install tree

If you have a massive subdirectory of your current_directory structure and only want to show a sample of what the structure contains you can do something like:

tree -P *my_own_pattern_to_find* current_directory
Intervene answered 22/10, 2020 at 9:58 Comment(0)
C
0

Have a look at App::Asciio  

Chloras answered 17/10, 2009 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.