How to display Directory & File structure in Markdown Files

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. How to display Directory & File structure in Markdown Files
  2. Different formatting options when using the tree command
  3. Using a VS Code extension to generate a file tree

# How to display Directory & File structure in Markdown Files

The easiest way to display a project's directory and file structure in markdown files is to:

  1. Open your terminal in the specific directory.
  2. Issue the tree command.
shell
tree

issue tree command macos linux

The tree command can also be run in Windows (in CMD and PowerShell).

On Windows and Linux, you can also exclude one or more directories from the command's output.

shell
tree -I "node_modules|bower_components"

The command excludes the output of the node_modules and bower_components directories.

  1. Use three backtick (`) characters to define a block of code in your markdown file.

Here is an example README.md markdown file that renders a directory and file structure.

README.md
``` ├── example.json ├── index.html ├── index.js ├── package.json ├── package-lock.json ├── README.md └── src ├── app.js ├── models.js ├── routes.js └── utils ├── another.js ├── constants.js └── index.js ```

Notice that the directory structure is wrapped in three backtick characters.

You can find the backtick character above the Tab key, to the left of 1 on your keyboard.

Three backtick characters are used to display a code snippet.

If you use Visual Studio Code, once you open a markdown file, you can click on the Open Preview to the Side button at the top to display your markdown file in preview mode.

vscode display directory structure

You must wrap the directory tree in backticks, otherwise, the spacing will likely not be preserved.

If you can't get the backtick characters syntax to work, wrap your directory structure in pre tags.
README.md
<pre> ├── example.json ├── index.html ├── index.js ├── package.json ├── package-lock.json ├── README.md └── src ├── app.js ├── models.js ├── routes.js └── utils ├── another.js ├── constants.js └── index.js </pre>

The pre HTML element is used to display preformatted text. The text is rendered exactly as it is displayed in the HTML (or markdown) file.

You could also use the Enter key to separate the entries with a newline character if you prefer the formatting.

README.md
├── example.json ├── index.html ├── index.js ├── package.json ├── package-lock.json ├── README.md └── src ├── app.js ├── models.js ├── routes.js └── utils ├── another.js ├── constants.js └── index.js

separate directory and file entries with newline

# Different formatting options when using the tree command

Here are some examples of different formatting options when using the tree command.

shell
# macOS and Linux tree -I "node_modules|bower_components" | sed 's/├/\+/g; s/─/-/g; s/└/\\/g'

The output of the command looks as follows.

README.md
``` +-- example.json +-- index.html +-- index.js +-- package.json +-- package-lock.json +-- README.md \-- src +-- app.js +-- models.js +-- routes.js \-- utils +-- another.js +-- constants.js \-- index.js ```

The example only uses ASCII characters to display the directory structure.

Here is another ASCII-only character format you could use.

README.md
``` +-- example.json +-- index.html +-- index.js +-- package.json +-- package-lock.json +-- README.md +-- src | +-- app.js | +-- models.js | +-- routes.js | +-- utils | +-- another.js | +-- constants.js | +-- index.js +-- assets | +-- style.css | +-- cat.png +-- example.com ```

You could also set the --charset argument to unicode when issuing the tree command.

shell
tree -I "node_modules|bower_components" --charset unicode

The command produces the following output.

README.md
``` |-- example.json |-- index.html |-- index.js |-- package.json |-- package-lock.json |-- README.md `-- src |-- app.js |-- models.js |-- README.md |-- routes.js `-- utils |-- another.js |-- constants.js |-- index.js `-- README.md ```

# Using a VS Code extension to generate a file tree

You can also use a VS Code extension to generate a file tree.

  1. Click on Extensions in the left sidebar.
  • You can also open the Extensions menu by pressing:
    • Ctrl + Shift + X on Windows or Linux
    • Command + Shift + X on macOS
  1. Type file-tree-generator.

install file tree generator extension

  1. Click on the Install button.

Make sure to install the correct file-tree-generator extension.

  1. Right-click on the folder for which you want to generate a tree.
  2. Click Generate to tree.

right click generate to tree

If you can't get the extension to work, try to restart VS Code.

Once you generate the directory structure, you can select the output and copy it.

copy directory structure

If you want to remove the icons, click on the icon off button.

Now, paste the output into your markdown file and don't forget to wrap it in three backticks as in the previous examples.

README.md
``` 📦src ┣ 📂utils ┃ ┣ 📜README.md ┃ ┣ 📜another.js ┃ ┣ 📜constants.js ┃ ┗ 📜index.js ┣ 📜README.md ┣ 📜app.js ┣ 📜models.js ┗ 📜routes.js ```

You could also use the pre tag if you can't get the backtick characters to work.

README.md
<pre> 📦src ┣ 📂utils ┃ ┣ 📜README.md ┃ ┣ 📜another.js ┃ ┣ 📜constants.js ┃ ┗ 📜index.js ┣ 📜README.md ┣ 📜app.js ┣ 📜models.js ┗ 📜routes.js </pre>

If you can't get the extension to work when trying to generate a directory and file structure for your entire project:

  1. Open VS Code one directory up.
  2. Click on your project folder.
  3. Select Generate to tree.

The extension doesn't always work when you click on the empty space in Explorer. It works much more reliably if you right-click a specific folder before selecting Generate to tree.

I've also written a detailed article on how to add images and links to files in Markdown in VS Code.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.