ASCII Library for Creating “Pretty” Directory Trees?

后端 未结 10 1052
时光说笑
时光说笑 2020-12-04 07:33

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
|             


        
相关标签:
10条回答
  • 2020-12-04 08:02

    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
    
    0 讨论(0)
  • 2020-12-04 08:04

    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
    …
    
    0 讨论(0)
  • 2020-12-04 08:08

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

    0 讨论(0)
  • 2020-12-04 08:09

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

    0 讨论(0)
提交回复
热议问题