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

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

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

How about this example from Unix Tree / Linux Tree:

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

Have a look at App::Asciio  

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

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
…

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

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

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

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

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