91

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
Alana Storm
  • 164,128
  • 91
  • 395
  • 599

10 Answers10

121

How about this example from Unix Tree / Linux Tree:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'  
bobbymcr
  • 23,769
  • 3
  • 56
  • 67
  • 22
    You gotta love a one-liner – pavium Oct 17 '09 at 07:09
  • 3
    That's disgustingly awesome, but part of what I like about the output above is how it's not all dashes to the files, you get those pipes connecting subdirectories vertically – Alana Storm Oct 17 '09 at 17:10
  • 1
    You might check this person's solution out then: http://sandeep-vaniya.blogspot.com/2008/04/perl-script-for-directory-tree.html – bobbymcr Oct 17 '09 at 17:23
  • 1
    great! although not quite right output (I'm not critizing so much as adding constuctive critism) - the files in sub-directories are all being group together when I tried it? – monojohnny Oct 19 '12 at 15:43
  • 3
    Does anyone know how you would add to that script so that it also shows files in the directory? – Raigex Feb 22 '13 at 20:45
  • could you give example output? – n611x007 Dec 05 '13 at 08:55
  • Here is some example output. It only includes the directories, not the files within them:. |-power |-properties |---advanced_battery_charging |---peakshift |-rtc |---rtc1 |-----power – Nick Crews Dec 21 '18 at 18:26
  • Well I editted that previous comment so many times it stopped letting me change it lol. It's apparently impossible to add line breaks in comments so I replaced them with "\n": Here is some example output. It only includes the directories, not the files within them (and it's apparently impossible to put line breaks in comments, so I put in "\n" at every break: .\n |-power\n |-properties\n |---advanced_battery_charging\n |---peakshift\n |-rtc\n |---rtc1\n |-----power – Nick Crews Dec 21 '18 at 18:33
88

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
user1116793
  • 881
  • 6
  • 2
  • 24
    For those on a Mac, `brew install tree` – briangonzalez Sep 17 '13 at 14:49
  • 8
    I struggled with tree using special characters as default, but suggestion from http://www.codealpha.net/696/putty-and-tree-how-to-avoid-weird-characters-squares/ is to use `tree --charset=ASCII`, so that tree produces chars as in user1116793's example above. – Brady Trainor Mar 09 '14 at 04:38
  • It has some creative uses too. I use it to visualize my errands (most tools don't have true hierarchical capability - not even Jira). – Sridhar Sarnobat May 16 '20 at 18:46
20

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

Ibrahim
  • 1,883
  • 19
  • 27
  • 2
    This should be marked as the correct answer. It supports dircolors, and the output is more nicely laid out. It's even a macport too for Mac OS X users. – Sridhar Sarnobat Nov 05 '12 at 21:41
  • 4
    `tree` can also be installed using Homebrew, for those who've moved on from Macports. – jeffbyrnes Apr 08 '13 at 15:24
  • 1
    It should be noted that, for Windows, you can simply type `tree` from the command line. No need to install anything. – Dan Atkinson Sep 08 '17 at 12:44
16

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
…
Gordon
  • 312,688
  • 75
  • 539
  • 559
13

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
andy boot
  • 11,355
  • 3
  • 53
  • 66
  • Found this late but this is awesome! Developing a tool for generating the file tree for a certain type of project and `exa -T -D` has been really really helpful. Thanks! – kingkupps May 05 '20 at 19:37
10

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

Nathan Friend
  • 12,155
  • 10
  • 75
  • 125
1

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

zcopley
  • 562
  • 4
  • 4
1

[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>
NVRM
  • 11,480
  • 1
  • 88
  • 87
1

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
Eamonn Kenny
  • 1,926
  • 18
  • 20
0

Have a look at App::Asciio  

AndyG
  • 39,700
  • 8
  • 109
  • 143
draegtun
  • 22,441
  • 5
  • 48
  • 71