Skip to content

树形目录 tree 命令

Windows

Windows 自带的 MS-DOS 命令 tree.com 比较垃圾,默认的保存路径位于 C:\Windows\System32\。运行了 Windows 默认的 tree 命令之后,终端输出的内容样式不但丑陋,基本无法直接使用。想要删除此默认命令不用,重新安装合适的代替软件。Windows 系统一贯的臭毛病,C:\Windows\System32\ 此目录下任何文件的移动或删除都需要提供权限,一步一步地搜索、解锁、操作,极度恶心。

但是此命令文件必须先行删除,否则后面安装的命令(因为同名)将无法正常识别和使用。

安装

  1. 打开进入 Tree for Windows 页面,选择下载 Binaries zip 文件
  2. 解压得到 tree-1.5.2.2-bin 压缩包,找到压缩包内的 bin 目录,将 bin 目录下的 tree.exe 复制
  3. 找到 C:\Program Files\Git\usr\bin 目录,将 tree.exe 粘贴到该目录下,安装即完成

因为上述粘贴目录已经添加到环境变量,所以在 Windows 下无论使用 PowerShell,Bash 还是 Zsh,tree 命令都可以正常运行。

tree 命令行参数:

  • -a 显示所有文件和目录
  • -A 使用 ASNI 绘图字符显示树状图而非以 ASCII 字符组合
  • -C 在文件和目录清单加上色彩,便于区分各种类型
  • -d 显示目录名称而非内容
  • -D 列出文件或目录的更改时间
  • -f 在每个文件或目录之前,显示完整的相对路径名称
  • -F 在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","=","@","|"号
  • -g 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码
  • -i 不以阶梯状列出文件或目录名称
  • -I 不显示符合范本样式的文件或目录名称
  • -l 如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录
  • -n 不在文件和目录清单加上色彩
  • -N 直接列出文件和目录名称,包括控制字符
  • -p 列出权限标示
  • -P 只显示符合范本样式的文件或目录名称
  • -q 用"?"号取代控制字符,列出文件和目录名称
  • -s 列出文件或目录大小
  • -t 用文件和目录的更改时间排序
  • -u 列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码
  • -x 将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排除在寻找范围外

常用命令

  • tree >tree.txt:将当前目录的树形目录写入到 tree.txt 文件内,支持中文写入
  • tree -L 2:打印当前目录的 2 层树形结构,中文显示为乱码,搜索方案未得
  • Windows command chcp:Changes the active console code page. If used without parameters, chcp displays the number of the active console code page.

可能是由于 Windows 系统下的该 tree 命令版本过于陈旧,中文乱码问题难以解决,遂寻找代替方案。

代替方案 tree-cli

1、安装:

sh
npm install -g tree-cli

2、查看位置:

sh
PS D:\Teek> whereis tree
C:\Users\Lenovo\AppData\Roaming\npm\tree.ps1

3、使用:

PS D:\Teek> tree -l 2

D:\Teek
├── docs
|  ├── 00.目录页
|  ├── 01.terminal
|  ├── 02.shell
|  ├── 03.editor
|  ├── 05.tool
|  ├── 08.frontend
|  ├── 10.市场营销
|  ├── 12.Soundfreaq
|  ├── 13.亚马逊运营
|  ├── 15.京东运营
|  ├── 20.关于
|  ├── 30.Misc
|  ├── @pages
|  ├── index.md
|  └── public
├── LICENSE
├── node_modules
|  ├── @algolia
|  ├── @babel
|  ├── @docsearch
|  ├── @esbuild
|  ├── @iconify
|  ├── @iconify-json
|  ├── @jridgewell
|  ├── @rollup
|  ├── @shikijs
|  ├── @types
|  ├── @ungap
|  ├── @vitejs
|  ├── @vue
|  ├── @vueuse
|  ├── algoliasearch
|  ├── argparse
|  ├── birpc
|  ├── ccount
|  ├── character-entities-html4
|  ├── character-entities-legacy
|  ├── comma-separated-tokens
|  ├── copy-anything
|  ├── csstype
|  ├── dequal
|  ├── devlop
|  ├── emoji-regex-xs
|  ├── entities
|  ├── esbuild
|  ├── esprima
|  ├── estree-walker
|  ├── extend-shallow
|  ├── fdir
|  ├── focus-trap
|  ├── gray-matter
|  ├── hast-util-to-html
|  ├── hast-util-whitespace
|  ├── hookable
|  ├── html-void-elements
|  ├── is-extendable
|  ├── is-what
|  ├── js-yaml
|  ├── kind-of
|  ├── magic-string
|  ├── mark.js
|  ├── markdown-it-container
|  ├── mdast-util-to-hast
|  ├── micromark-util-character
|  ├── micromark-util-encode
|  ├── micromark-util-sanitize-uri
|  ├── micromark-util-symbol
|  ├── micromark-util-types
|  ├── minisearch
|  ├── mitt
|  ├── nanoid
|  ├── oniguruma-to-es
|  ├── perfect-debounce
|  ├── picocolors
|  ├── picomatch
|  ├── postcss
|  ├── preact
|  ├── property-information
|  ├── regex
|  ├── regex-recursion
|  ├── regex-utilities
|  ├── rfdc
|  ├── rollup
|  ├── search-insights
|  ├── section-matter
|  ├── shiki
|  ├── source-map-js
|  ├── space-separated-tokens
|  ├── speakingurl
|  ├── sprintf-js
|  ├── stringify-entities
|  ├── strip-bom-string
|  ├── superjson
|  ├── tabbable
|  ├── tinyglobby
|  ├── trim-lines
|  ├── unist-util-is
|  ├── unist-util-position
|  ├── unist-util-stringify-position
|  ├── unist-util-visit
|  ├── unist-util-visit-parents
|  ├── vfile
|  ├── vfile-message
|  ├── vite
|  ├── vitepress-plugin-auto-frontmatter
|  ├── vitepress-plugin-catalogue
|  ├── vitepress-plugin-doc-analysis
|  ├── vitepress-plugin-file-content-loader
|  ├── vitepress-plugin-md-h1
|  ├── vitepress-plugin-permalink
|  ├── vitepress-plugin-sidebar-resolve
|  ├── vue
|  └── zwitch
├── package-lock.json
├── package.json
├── pnpm-lock.yaml
└── README.md

directory: 112 file: 6 symboliclink: 3

tree-cli

macOS

Installation

sh
brew install tree

使用

在家目录下,分别测试运行以下三个命令,均可正常工作。

sh
tree
tree -L 1
tree >tree.txt

结论

Windows 系统下的 tree 命令比较陈旧,输出至终端时,对中文字符的支持不太友好,所以使用 Nodes 环境下的另外一个替代工具 tree-cli。macOS 下 tree 命令的安装和使用相对友好、易用。

最近更新