preface

We often need to list the structure tree of a project when writing a blog or project design document. We can use tree to list the project structure as follows:

News_watch_notice ├── CMD // Main ├── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── Readme. md ├─ Vendor ├─ utilsCopy the code

Use the Windows tree

Windows provides a tree command for us to use, but it’s not very useful.

TREE [Drive :][path] [/F] [/A] /F Displays the names of files in each folder. /A uses ASCII characters instead of extended characters.Copy the code

D:\dev\gopath\src\news_watch_notice>tree /a

Folder PATH list serial number for 989 e - DB7E D:. + -. The idea + CMD | \ - - - + + - - - - the conf qrcode dis | \ + -- -- -- -- -- -- qrcode PKG | + -- -- -- mail | +---reptile | \---wechat +---qrcode +---utilsCopy the code

D:\dev\gopath\src\news_watch_notice>tree /f

Yml │ Dockerfile │ Makefile │ Readme.md │ tree.md │ ├─.idea │ Name │ misc.xml │ modules. XML │ news_watch_notice.iml │ vs.xml │ workshop. XML │ ├─ CMD │ news_watch_notice.go │ │ └ ─ qrcode │ qrcode. JPGCopy the code

As you can see, the output does follow the desired structure, but the use of only two commands is not enough to meet our daily needs, such as ignoring a file and what to do when we want to output the generated tree structure into a file.

Use the tree-node-CLI based node

To use these commands, we need to do some preparation

  • Nodejs installation, can be used here (recommended to use the stable version of LTS, and nodeJS comes with NPM installation package manager)

Install tree node — the cli

Install the tree-node-CLI module package
npm install -g tree-node-cli
Copy the code

Using the command

$ tree --help
Usage: tree [options]

Options:
  -V, --version             output the version number
  -a, --all-files           All files, include hidden files, are printed.
  --dirs-first              List directories before files.
  -d, --dirs-only           List directories only.
  -I, --exclude [patterns]  Exclude files that match the pattern. | separates alternate patterns. Wrap your entire pattern in double quotes. E.g. `"node_modules|coverage".
  -L, --max-depth <n>       Max display depth of the directory tree.
  -r, --reverse             Sort the output in reverse alphabetic order.
  -F, --trailing-slash      Append a '/' for directories.
  -h, --help                output usage information

Copy the code

Tree -d displays only folders;

  • Tree-l n Displays the level of an item. N indicates the number of tiers. For example, if you want to display the three-tier structure of the project, you can use tree-l 3;
  • Tree -I pattern Filters files or folders that do not want to be displayed. For example, if you want to filter the vendor folder in your project, use tree -i “vendor”;
  • Tree > tree.md Outputs the project structure to tree.md.

For example, if we want to display all the file structures of the three layers of a project, and filter the node_modules folder, and finally output it to tree.md, we can do this

$ tree -L 3 -I "vendor" > tree.md
Copy the code

Results: the tree. The md

News_watch_notice ├ ─ ─ CMD │ ├ ─ ─ news_watch_notice. Go │ └ ─ ─ qrcode │ └ ─ ─ qrcode. JPG ├ ─ ─ the conf │ └ ─ ─ the conf. Conf ├ ─ ─ dis │ ├ ─ ─ news_watch_notice │ └ ─ ─ qrcode │ └ ─ ─ qrcode. JPG ├ ─ ─ Dockerfile ├ ─ ─ a Makefile ├ ─ ─ PKG │ ├ ─ ─ mail │ │ └ ─ ─ mail. Go │ ├ ─ ─ Reptile │ │ └ ─ ─ reptile. Go │ └ ─ ─ wechat │ └ ─ ─ wechat. Go ├ ─ ─ qrcode │ └ ─ ─ qrcode. JPG ├ ─ ─ the README. Md ├ ─ ─ tree. The md ├ ─ ─ utils │ └ ─ ─ common_utils. GoCopy the code




Wechat official account



github