如何生成项目树状结构图

Prerequisites

  • Install tree command via Homebrew by running brew install tree

  • run brew to check if Homebrew is currently installed on your computer

  • if not, run the following to install Homebrew:

1
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Basic Syntax

1
tree [directory-path]

[directory-path] is an optional argument that specifies the directory you want to display as a tree structure. If you don’t specify a directory path, tree will display the tree structure for the current directory.

Here are some useful options you can add to the tree command:

-L [level]: Limit the depth of the tree to [level] directories.

-I [pattern]: Exclude files and directories that match the [pattern].

-P [pattern]: [pattern] is used to match files or directories that you want to include in the output.

-a: Show hidden files and directories.

-f: Print the full path prefix for each file or directory.

-h: Print the size of each file or directory in a human-readable format.

Example

1
2
3
4
5
6
$ tree -L 1 -P "source/*" -I "_drafts|_pages" source

source
└── _posts

2 directories, 0 files

You can omit the report on the last line with option --noreport if it bothers you.