Biblioteka ASCII do tworzenia "ładnych" drzew katalogów?

Czy istnieje jakieś narzędzie *nix lub biblioteka perl / php, które pozwoli Ci łatwo tworzyć wizualizacje drzewa katalogów, które wyglądają jak poniżej?

www
|-- private
|    |-- app 
|    |    |-- php
|    |    |    |-- classes
|    |    |    +-- scripts
|    |    |-- settings
|    |    +-- sql
|    +-- lib
|         +-- ZendFramework-HEAD
+-- public
    |-- css
    |-- images
    +-- scripts
Author: Alan Storm, 2009-10-17

10 answers

A może ten przykład z Drzewa Unix / drzewa Linuksa :

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'  
 111
Author: bobbymcr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-10-17 07:02:03

Ten oneliner jest całkiem fajny, polecam użycietree 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
 80
Author: user1116793,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-14 07:20:49

Zdaję sobie sprawę, że odpowiedź na to pytanie została udzielona wieki temu, ale właśnie znalazłem ten program o nazwie tree , który też jest całkiem fajny.

 18
Author: Ibrahim,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-10 22:33:37

Zobacz RecursiveTreeIterator klasa

Pozwala na iterację przez RecursiveIterator w celu wygenerowania drzewa graficznego ASCII.

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

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

Wyjście będzie coś takiego (z c:\php na mojej maszynie):

|-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
…
 16
Author: Gordon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-18 21:57:48

Exa z --tree robi świetną robotę:

exa --tree ~/tmp/public/

<dir>
├── aboutme
│  └── index.html
├── atrecurse
│  └── index.html
├── base.css
├── html5
│  ├── cat-and-mouse
│  └── frantic
│     ├── css
│     │  └── main.css
 10
Author: andy boot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-03 17:01:24

Nie jest to biblioteka sama w sobie, ale to małe narzędzie jest przydatne do generowania szybkich Wykresów drzewa bez opuszczania przeglądarki: https://tree.nathanfriend.io/

Zastrzeżenie: jestem autorem:).

 3
Author: Nathan Friend,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-12-10 16:56:30

Fajny skrypt Pythona do zrobienia: http://code.activestate.com/recipes/217212/

 1
Author: zcopley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-04 19:52:14

[php] do poprawienia symboli drzewa, pobranych z 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>";
}

Przykładowe wyjście

[./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>
 1
Author: NVRM,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-13 21:28:23

W ostatnich latach wiele się zmieniło. Wersja Linuksa w menedżerach pakietów jest czystsza i koloryzowana:

Debian/Ubuntu:

sudo apt install tree

CentOS/RHEL/OpenSUSE:

sudo yum install tree

Jeśli masz ogromny podkatalog swojej struktury current_directory i chcesz pokazać tylko próbkę tego, co zawiera struktura, możesz zrobić coś takiego:

tree -P *my_own_pattern_to_find* current_directory
 1
Author: Eamonn Kenny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-10-22 10:05:47

Spójrz na App::Asciio  

 0
Author: draegtun,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-21 15:38:19