Last updated: Apr 5, 2024
Reading time·4 min
The easiest way to display a project's directory and file structure in markdown files is to:
tree
command.tree
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.
tree -I "node_modules|bower_components"
The command excludes the output of the node_modules
and bower_components
directories.
Here is an example README.md
markdown file that renders a directory and file
structure.
``` ├── 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.
You must wrap the directory tree in backticks, otherwise, the spacing will likely not be preserved.
pre
tags.<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.
├── 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
Here are some examples of different formatting options when using the tree
command.
# macOS and Linux tree -I "node_modules|bower_components" | sed 's/├/\+/g; s/─/-/g; s/└/\\/g'
The output of the command looks as follows.
``` +-- 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.
``` +-- 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.
tree -I "node_modules|bower_components" --charset unicode
The command produces the following output.
``` |-- 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 ```
You can also use a VS Code extension to generate a file tree.
Ctrl
+ Shift
+ X
on Windows or LinuxCommand
+ Shift
+ X
on macOSMake sure to install the correct file-tree-generator extension.
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.
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.
``` 📦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.
<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:
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.
You can learn more about the related topics by checking out the following tutorials: