用 eza 取代 tree & ls 等命令以给 AI 提供更好的上下文

三葉Leaves Author

https://github.com/eza-community/eza

相比 ls 命令,eza 命令内置了下面几种很有用的特性:

  • git 集成
  • 内置 tree 试图显示的功能
  • 包括硬链接等更丰富的元信息

安装

Linux

1
cd /tmp && curl -L https://github.com/eza-community/eza/releases/latest/download/eza_x86_64-unknown-linux-musl.tar.gz -o eza.tar.gz && tar -xzf eza.tar.gz && chmod +x eza && mkdir -p /usr/local/bin && mv eza /usr/local/bin/eza && eza --version

这个命令适用于所有有 curl 的 Linux OS,即使是没有包管理器的 Synology DSM 都行。

Windows

1
winget install eza-community.eza

MacOS

1
brew install eza

如果没有 homebrew,先安装:

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

高频使用场景

平时自己随便用一下

1
2
# 长列表,显示隐藏文件,加表头
eza -lah

快速帮助 AI 了解文件夹结构

这里细节的带上了根目录显示

1
printf 'ROOT=%s\n' "‘$PWD’"; eza --tree --level=30 --all --long --header --classify=always --no-permissions --no-user --no-time --color=never --icons=never --hyperlink=never --inode --links --group-directories-first

看 git 代码库

1
eza --tree --level=5 --all --git-ignore --group-directories-first --color=never --icons=never --hyperlink=never

加上 --git 还可以进一步显示出 git status。

参数释义

参数 作用 我的建议
-l, --long 长列表,显示大小等元数据 ⭐⭐⭐⭐⭐
-a, --all 包含隐藏文件 ⭐⭐⭐⭐⭐
-T, --tree 树形递归 ⭐⭐⭐⭐⭐
-L N, --level=N 限制递归深度 ⭐⭐⭐⭐⭐
-h, --header 显示列标题 ⭐⭐⭐⭐
-F, --classify=always /* 等标记文件类型 ⭐⭐⭐⭐
-i, --inode inode 编号 排错时很好
-H, --links 硬链接数量 排错时很好
--git Git 状态 代码仓库很好用
--git-ignore 不显示 .gitignore 忽略的内容 AI 看代码仓库神器
-I '...' 排除指定文件 AI 上下文神器
-D, --only-dirs 只看目录 偶尔有用
-f, --only-files 只看文件 偶尔有用
--sort=size 按大小排序 查大文件
--sort=modified 按修改时间排序 查最近变化
--group-directories-first 文件夹优先 人类查看舒服

更专业的方式?

这会产出一个 jsonl 文件,适合给 AI 作为精准事实来源,避免一些多个空格少个空格的问题。依赖 python。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
python3 - <<'PY' > inventory.jsonl
import os
import json
import stat
from datetime import datetime

ROOT = "."

# inventory 中的所有 path 都相对于这个绝对路径
ROOT_ABS = os.path.abspath(ROOT)

entries = []

for dirpath, dirnames, filenames in os.walk(
ROOT,
topdown=True,
followlinks=False,
):
# @eaDir 本身保留在 inventory,
# 但不继续深入其内部
for dirname in list(dirnames):
full = os.path.join(dirpath, dirname)
rel = os.path.relpath(full, ROOT)

try:
st = os.lstat(full)
except OSError as e:
entries.append({
"path": rel,
"type": "error",
"error": str(e),
})
continue

if stat.S_ISLNK(st.st_mode):
file_type = "symlink"
elif stat.S_ISDIR(st.st_mode):
file_type = "directory"
else:
file_type = "other"

item = {
"path": rel,
"name": dirname,
"type": file_type,
"size": st.st_size,
"mtime": datetime.fromtimestamp(
st.st_mtime
).astimezone().isoformat(),
"symlink": stat.S_ISLNK(st.st_mode),
}

if item["symlink"]:
try:
item["target"] = os.readlink(full)
except OSError:
item["target"] = None

entries.append(item)

if dirname == "@eaDir" or item["symlink"]:
dirnames.remove(dirname)

for filename in filenames:
full = os.path.join(dirpath, filename)
rel = os.path.relpath(full, ROOT)

try:
st = os.lstat(full)
except OSError as e:
entries.append({
"path": rel,
"type": "error",
"error": str(e),
})
continue

if stat.S_ISLNK(st.st_mode):
file_type = "symlink"
elif stat.S_ISREG(st.st_mode):
file_type = "file"
else:
file_type = "other"

_, ext = os.path.splitext(filename)

item = {
"path": rel,
"name": filename,
"type": file_type,
"extension": ext.lower(),
"size": st.st_size,
"mtime": datetime.fromtimestamp(
st.st_mtime
).astimezone().isoformat(),
"symlink": stat.S_ISLNK(st.st_mode),
}

if item["symlink"]:
try:
item["target"] = os.readlink(full)
except OSError:
item["target"] = None

entries.append(item)

entries.sort(key=lambda x: x["path"])

# 第一行:inventory 自身的元数据
print(json.dumps({
"type": "inventory_meta",
"root": ROOT_ABS,
}, ensure_ascii=False, separators=(",", ":")))

# 后续每行:一个文件系统对象
for item in entries:
print(json.dumps(
item,
ensure_ascii=False,
separators=(",", ":"),
))
PY
  • 标题: 用 eza 取代 tree & ls 等命令以给 AI 提供更好的上下文
  • 作者: 三葉Leaves
  • 创建于 : 2026-08-16 00:00:00
  • 更新于 : 2026-09-20 23:26:04
  • 链接: https://blog.oksanye.com/7896cb8cbc0c/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论