用于创建“漂亮”目录树的ASCII库?

时间:2009-10-17 06:18:04

标签: php unix tree ascii

是否有一些* nix工具或perl / php库可以让您轻松创建如下所示的目录树可视化?

www
|-- private
|    |-- app 
|    |    |-- php
|    |    |    |-- classes
|    |    |    +-- scripts
|    |    |-- settings
|    |    +-- sql
|    +-- lib
|         +-- ZendFramework-HEAD
+-- public
    |-- css
    |-- images
    +-- scripts

10 个答案:

答案 0 :(得分:98)

来自Unix Tree / Linux Tree的这个示例如何:

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

答案 1 :(得分:74)

oneliner非常酷,我建议使用 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

答案 2 :(得分:16)

我意识到这个问题很久以前就得到了回答,但我刚刚发现这个名为tree的程序也非常酷。

答案 3 :(得分:16)

请参阅RecursiveTreeIterator课程

  

允许迭代RecursiveIterator以生成ASCII图形树。

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

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

输出将是这样的(在我的机器上使用c:\​​ php):

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

答案 4 :(得分:4)

exa with --tree做得很好:

exa --tree ~/tmp/public/

<dir>
├── aboutme
│  └── index.html
├── atrecurse
│  └── index.html
├── base.css
├── html5
│  ├── cat-and-mouse
│  └── frantic
│     ├── css
│     │  └── main.css

答案 5 :(得分:1)

答案 6 :(得分:1)

[php]要调整树符号,取自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>";
}

示例输出

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

答案 7 :(得分:1)

近年来,这种情况发生了很多变化。软件包管理器中的linux版本更加干净和彩色:

Debian / Ubuntu:

sudo apt install tree

CentOS / RHEL / OpenSUSE:

sudo yum install tree

如果您的current_directory结构有一个庞大的子目录,并且只想显示该结构包含的样本,则可以执行以下操作:

tree -P *my_own_pattern_to_find* current_directory

答案 8 :(得分:0)

看看App::Asciio

答案 9 :(得分:0)

本质上不是一个库,但是这个小工具很方便在不离开浏览器的情况下生成快速树状图:https://tree.nathanfriend.io/

免责声明:我是作者:)。