阡陌 发表于 2024-1-9 19:06:55

在 Git Bash 中使用 tree 命令


## 关于 tree 命令

tree 命令可以以 ASCII 树状图的方式列出目录的结构。

```
$ tree ./tree-1.5.2.2-bin -A
./tree-1.5.2.2-bin
├── bin
│   └── tree.exe
├── contrib
│   └── tree
│       └── 1.5.2.2
│         ├── tree-1.5.2.2
│         ├── tree-1.5.2.2-GnuWin32.README
│         └── tree-1.5.2.2-src
│               ├── CHANGES
│               ├── INSTALL
│               ├── LICENSE
│               └── README
├── man
│   └── cat1
│       └── tree.1.txt
└── manifest
    ├── tree-1.5.2.2-bin.mft
    └── tree-1.5.2.2-bin.ver

9 directories, 9 files

```

注:-A 参数在绘制线条时使用 ASCII 扩展字符,这会更好看一些。

这对我们在命令行中查看目录的组织方式还是很有作用的。但 git bash 默认并没有集成该命令。我们可以自行安装一下。

## 安装 tree 命令

下载:

到 <https://gnuwin32.sourceforge.net/packages/tree.htm> 下载 Windows 版预编译二进制文件:

![](https://file.mculoop.com/images/2024/01/09/202401091821529.png)

从下载到的压缩包中解压出可执行程序 `tree.exe` (单独一个文件即可),放置于 git 的安装目录 `/usr/bin/`(默认安装情况下绝对路径通常是 `C:\Program Files\Git\usr\bin`)。

这时打开 Git Bash 就可以用 tree 命令了。

```
$ tree --help
usage: tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]
      [-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
      [--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
      [--filelimit #] [<directory list>]
-a            All files are listed.
-d            List directories only.
-l            Follow symbolic links like directories.
-f            Print the full path prefix for each file.
-i            Don't print indentation lines.
-q            Print non-printable characters as '?'.
-N            Print non-printable characters as is.
-p            Print the protections for each file.
-u            Displays file owner or UID number.
-g            Displays file group owner or GID number.
-s            Print the size in bytes of each file.
-h            Print the size in a more human readable way.
-D            Print the date of last modification.
-F            Appends '/', '=', '*', or '|' as per ls -F.
-v            Sort files alphanumerically by version.
-r            Sort files in reverse alphanumeric order.
-t            Sort files by last modification time.
-x            Stay on current filesystem only.
-L level      Descend only level directories deep.
-A            Print ANSI lines graphic indentation lines.
-S            Print with ASCII graphics indentation lines.
-n            Turn colorization off always (-C overrides).
-C            Turn colorization on always.
-P pattern    List only those files that match the pattern given.
-I pattern    Do not list files that match the given pattern.
-H baseHREF   Prints out HTML format with baseHREF as top directory.
-T string   Replace the default HTML title and H1 header with string.
-R            Rerun tree when max dir level reached.
-o file       Output to file instead of stdout.
--inodes      Print inode number of each file.
--device      Print device ID number to which each file belongs.
--noreport    Turn off file/directory count at end of tree listing.
--nolinks   Turn off hyperlinks in HTML output.
--dirsfirst   List directories before files.
--charset X   Use charset X for HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
```

通常命令跟文件夹即可,文件夹可以是相对路径也可以是绝对路径,并且可以指定多个文件夹。

## 使用 Windows 自带的 tree 命令

我们要知道 Windows 的命令行工具自带了 tree 命令,展示细节上差点意思,将就一下也还行。

```
PS D:\root> tree tree-1.5.2.2-bin /F
卷 Data 的文件夹 PATH 列表
卷序列号为 6EDF-788C
D:\ROOT\TREE-1.5.2.2-BIN
├─bin
│      tree.exe

├─contrib
│└─tree
│      └─1.5.2.2
│          │tree-1.5.2.2-GnuWin32.README
│          │
│          ├─tree-1.5.2.2
│          └─tree-1.5.2.2-src
│                  CHANGES
│                  INSTALL
│                  LICENSE
│                  README

├─man
│└─cat1
│          tree.1.txt

└─manifest
      tree-1.5.2.2-bin.mft
      tree-1.5.2.2-bin.ver
```

不过由于该命令是集成在命令行程序的,所以不能被 git bash 调用。不过我想了一个注意,在 git bash 中先执行 `cmd`,进入 Windows 命令行环境后再执行 `tree` 命令,怎么样,这招是不是很高明 {:lol:}

![](https://file.mculoop.com/images/2024/01/09/202401091904188.png)



页: [1]
查看完整版本: 在 Git Bash 中使用 tree 命令