本文目录导读:

Windows 系统
使用 tree 命令(最简单)
# 导出当前目录结构到文件 tree /F > 目录结构.txt # 只显示文件夹(不显示文件) tree > 目录结构.txt # 指定路径 tree C:\你的路径 /F > 目录结构.txt
使用 PowerShell 脚本
# 基本目录结构导出
Get-ChildItem -Recurse | Where-Object { $_.PSIsContainer } | Select-Object FullName | Out-File -FilePath 目录结构.txt
# 包含文件的详细结构
Get-ChildItem -Recurse | ForEach-Object {
$indent = " " * ($_.FullName.Split('\').Count - 1)
"$indent$($_.Name)"
} | Out-File -FilePath 目录结构.txt
macOS / Linux 系统
使用 tree 命令(推荐)
# 安装 tree(如果未安装) brew install tree # macOS sudo apt-get install tree # Ubuntu/Debian # 导出目录结构 tree -L 3 > 目录结构.txt # 限制深度为3层 tree -a > 目录结构.txt # 包含隐藏文件 tree -I 'node_modules|.git' > 目录结构.txt # 排除指定文件夹
使用 find 命令
# 基本目录结构 find . -type d > 目录结构.txt # 包含文件的完整结构 find . -type f -o -type d > 目录结构.txt # 带层级缩进显示 find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
进阶脚本示例
Python 脚本(跨平台)
import os
def list_directory_structure(path, indent=''):
structure = []
items = os.listdir(path)
for i, item in enumerate(items):
is_last = i == len(items) - 1
prefix = '└── ' if is_last else '├── '
full_path = os.path.join(path, item)
structure.append(f"{indent}{prefix}{item}")
if os.path.isdir(full_path):
extension = ' ' if is_last else '│ '
structure.extend(list_directory_structure(full_path, indent + extension))
return structure
# 使用示例
structure = list_directory_structure('.')
with open('目录结构.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(structure))
实用技巧
排除特定文件夹(如 node_modules)
# Linux/macOS find . -type f -not -path './node_modules/*' -not -path './.git/*' > 目录结构.txt # Windows PowerShell Get-ChildItem -Recurse -Exclude 'node_modules','.git' | Select-Object FullName > 目录结构.txt
生成 Markdown 格式
# 使用 tree 生成 markdown 格式 tree -L 3 --charset utf-8 | sed 's/├/|/g; s/── /-- /g; s/└/|/g' > 目录结构.md
根据你的具体需求(平台、是否需要包含文件、是否需要排除某些文件夹),选择合适的命令即可。