ASCII Library for Creating "Pretty" Directory Trees?

PhpUnixTreeAscii

Php Problem Overview


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

Php Solutions


Solution 1 - Php

How about this example from Unix Tree / Linux Tree:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'  

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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
…

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

[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>

Solution 9 - Php

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

Solution 10 - Php

Have a look at App::Asciio  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlan StormView Question on Stackoverflow
Solution 1 - PhpbobbymcrView Answer on Stackoverflow
Solution 2 - Phpuser1116793View Answer on Stackoverflow
Solution 3 - PhpIbrahimView Answer on Stackoverflow
Solution 4 - PhpGordonView Answer on Stackoverflow
Solution 5 - Phpandy bootView Answer on Stackoverflow
Solution 6 - PhpNathan FriendView Answer on Stackoverflow
Solution 7 - PhpzcopleyView Answer on Stackoverflow
Solution 8 - PhpNVRMView Answer on Stackoverflow
Solution 9 - PhpEamonn KennyView Answer on Stackoverflow
Solution 10 - PhpdraegtunView Answer on Stackoverflow