首页·终端命令

tree

文件与目录

用法

tree -L 2 [path]

把目录结构画成缩进的树;macOS 和精简的 Linux 镜像里默认没有,得先安装。

常用选项

选项含义
-LLimit the depth to N levels
-aInclude hidden files
-dDirectories only
-ISkip names matching a pattern
-fPrint the full path of each entry
-hShow readable sizes

示例

tree -L 2

Two levels deep, so the output stays readable.

tree -I 'node_modules|.git'

Skips the noisy directories.

tree -d -L 1

Just the top-level folders.

移动、删除和查找。有些命令不会再问一遍,所以顺手加 -i 是在救自己。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. tree 是做什么的?

把目录结构画成缩进的树;macOS 和精简的 Linux 镜像里默认没有,得先安装。

Q. 怎么写?

tree -L 2 [path] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man tree。这条命令属于文件与目录。

相关命令

man 手册: man tree