banner
约 900 字
3 分钟

快速把文件目录输出为树形结构

无标签

摘要

快速输出可视化文件目录,便于让AI快速了解我们项目结构

以下为不同操作系统输出方式与命令

Windows系统#

默认系统命令#

  1. 基本命令

Shell
tree
  1. 只显示文件夹结构

Shell
tree /F
  1. 导出到文件

Shell
tree /F > tree.txt

输出示例:

纯文本
卷 Windows 的文件夹 PATH 列表
卷序列号为 XXXX-XXXX
D:.
├─app
│  ├─zones
│  │  └─general
│  └─admin
├─components
│  └─ui
├─lib
└─package.json

缺点:

  • 不能限制层级深度(没有 -L)

  • 不能排除目录(没有 -I),node_modules 会全量展开,输出会非常长

如果是给AI看目录,可以用下面的其他方法来输出

Bun 脚本#

复制下面的代码,粘贴到 控制台

Shell
bun -e "
import { readdirSync } from 'node:fs';
import { join } from 'node:path';

const IGNORE = ['node_modules', '.git', '.next', 'dist', 'build', '.turbo', '.vercel', 'coverage','.wrangler','docs','pic'];

function tree(dir, prefix = '', depth = 0, maxDepth = 5) {
  if (depth > maxDepth) return;

  const items = readdirSync(dir, { withFileTypes: true })
    .filter(i => !IGNORE.includes(i.name))
    .sort((a, b) => {
      if (a.isDirectory() && !b.isDirectory()) return -1;
      if (!a.isDirectory() && b.isDirectory()) return 1;
      return a.name.localeCompare(b.name);
    });

  for (let i = 0; i < items.length; i++) {
    const item = items[i];
    const last = i === items.length - 1;
    const connector = last ? '└── ' : '├── ';
    console.log(prefix + connector + item.name);

    if (item.isDirectory()) {
      const childPath = join(dir, item.name);
      const childPrefix = prefix + (last ? '    ' : '│   ');

      if (depth + 1 >= maxDepth) {
        // 只要该目录下还有内容(文件或目录都算),就显示 ...
        const hasChildren = readdirSync(childPath, { withFileTypes: true })
          .some(i => !IGNORE.includes(i.name));
        if (hasChildren) {
          console.log(childPrefix + '└── ...');
        }
      } else {
        tree(childPath, childPrefix, depth + 1, maxDepth);
      }
    }
  }
}

console.log('.');
tree(process.cwd(), '', 0, 5);
"
  • 可通过修改 IGNORE 变量内容,来排除不需要查看的目录

  • 可通过修改 tree(process.cwd(), '', 0, 5); 来决定输出的层数,0 代表当前根目录,5 代表层级深度

  • 已达设置的最大深度,但还有子目录,会显示 ...

输出效果大概是:

纯文本
.
├── app
│   ├── zones
│   │   └── general
│   │       └── ...
│   └── admin
│       └── ...
├── components
│   └── ...
├── lib
└── package.json

Git 命令#

Windows上 Git 需要添加一个拓展才可以

  1. 下载 Binaries zip:https://gnuwin32.sourceforge.net/packages/tree.htm

  2. 解压后,从 bin 目录里找到 tree.exe

  3. 把 tree.exe 复制到 :Git安装路径 > usr > bin > tree.exe

  4. 然后在项目根路径里,Shift + 鼠标右键 → Open Git Bash here

  5. 在Git Bash 窗口里执行下面命令

Shell
# 验证安装
tree --version

输入如下字样代表安装成功

tree v1.5.2.2 (c) 1996 - 2009 by Steve Baker, Thomas Moore, Francesc Rocher, Kyosuke Tokoro

查看目录,深度限定为 3 层,同时排除一些不需要看的目录

Shell
tree -L 3 -I 'node_modules|.git|.next|dist|build|.turbo|.wrangler|docs|pic'

如果想只看目录不看文件,加 -d:

Shell
tree -L 3 -d -I 'node_modules|.git|.next|dist|build|.turbo|.wrangler|docs|pic'

Linux系统#

很多发行版不自带,需要装:

Shell
# Debian / Ubuntu
sudo apt install tree

# CentOS / RHEL
sudo yum install tree

# Fedora
sudo dnf install tree

装好后直接用:

Shell
tree -L 3 -I 'node_modules|.git|.next|dist|build|.turbo|.wrangler|docs|pic'

MacOS系统#

macOS 默认没有 tree

  1. 先装 Homebrew

Shell
# 先装 Xcode 命令行工具(没装过的话)
xcode-select --install

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

Shell
brew install tree
  1. 装好后用法和 Linux 一样

Shell
tree -L 3 -I 'node_modules|.git|.next|dist|build|.turbo|.wrangler|docs|pic'

END