Create a directory-tree with the command-line

December 12th, 2017 1 minute read

When you write documentation for your project or site, say a README Table of Content, you may need something like Tree to generate a directory structure for you. Tree will display a list of directories as trees (with optional color/HTML output) automagically.

If you are a Mac user, you can use Tree in the command line.

If you don’t already have Homebrew on your machine, install it first.

Then in the command line, simply run

brew install tree

then you can open a new window

CD your-project

Then run

	tree

It will output a nice directory tree within the terminal window that you can just paste into your documentation.

Example Jekyll Theme folder/
├── CNAME
├── LICENSE.txt
├── README.md
├── _config.yml
├── _data
│   ├── authors.yml
│   ├── comments
│   │   ├── a-post-with-comment
│   │   │   └── comment-1.yml
│   ├── navigation.yml
│   └── ui-text.yml
├── _drafts
│   ├── something-something.md
│   ├── sample-post.md
│   └── theme-setup.md
├── _includes
│   ├── comment.html
│   ├── comments-providers
│   │   ├── custom.html
│   │   ├── scripts.html
│   │   └── staticman.html
│   ├── comments.html
│   ├── footer.html
│   ├── head.html
│   └── scripts.html
├── etc.

If you are using Markdown, just surround this code with triple backticks1 to get the same output.

```
├── CNAME
├── Gemfile
├── Gemfile.lock
├── LICENSE
├── README.md
├── ...
```

Other methods

Although using Tree in the command-line interface seems easier for me here are some other methods to the same end:

Directory tree plugin adds directory tree to markdown files via markdown-magic. → Directory tree plugin GitHub repo. What’s nice is that it lets you control your directory structure depth and which folder or file you choose to ignore.

md-file-tree generates markdown list of all the files in a directory. → md-file-tree GitHub repo. Its Markdown output looks slightly different

mddir Mddir generates a markdown file/folder structure for readme files → mddir GitHub repo. Again the output is different.

☞ If you are using Atom, there is also ASCII Tree, a package to generate ASCII trees to visualize file/folder structure.

☞ If you prefer to generate your directory manually, you can use Shawn Chin’s interactive tree builder.

References: Tree, Homebrew Formulas

Leave a Comment