开发工具系列004-Mac系统中Tree的使用

20 阅读3分钟

最近工作中需要以树状图的方式列出当前目录下面的文件结构,因此花了几分钟时间研究了下Tree的使用,故有此文。

如果是在Linux和Windows系统下,可以直接使用tree命令来查看目录的文件树,但是Mac OSX系统中提示没有相应的命令。

Mac OSX系统中想以文件树的方式查看目录,有两种方式;

① 执行下面的shell命令

$ find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

上面的命令行可以帮助我们完成需求,但这种方式相对不太友好,所以不推荐。

② 下载Tree组件使用(推荐)

在Mac OSX系统中,我们可以通过brew来下载Tree使用,下载后使用的方式和linux下保持一致。

说明:如果系统中没有brew,那么需要先自行安装。 [Homebrew](https://brew.sh/)是一个包管理器,用于在Mac上安装一些OS X没有的UNIX工具。 Homebrew将这些工具统统安装到了 /usr/local/Cellar 目录中,并在 /usr/local/bin 中创建符号链接。 Homebrew基于Ruby,安装过程请参考官网示例。

brew安装好之后,使用下面的命令来安装Tree组件。

$ brew install tree

Tree组件安装完成之后,可以通过 $ tree --version命令来查看版本并验证安装是否成功。 目前,最新的版本为v1.7.0,更详细的信息请移步Tree组件官网

另外,Tree提供了很多的命令选项来帮助我们对目录打印进行更精细的控制,可以通过$ tree --help命令来查看具体信息。

usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ]
	[-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version]
	[--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst]
	[--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]<f>]
	[--sort[=]<name>] [--matchdirs] [--ignore-case] [--] [<directory list>]
  ------- Listing options -------
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  -R            Rerun tree when max dir level reached.
  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
  --ignore-case Ignore case when pattern matching.
  --matchdirs   Include directory names in -P pattern matching.
  --noreport    Turn off file/directory count at end of tree listing.
  --charset X   Use charset X for terminal/HTML and indentation line output.
  --filelimit # Do not descend dirs with more than # files in them.
  --timefmt <f> Print and format time according to the format <f>.
  -o filename   Output to file instead of stdout.
  -------- File options ---------
  -q            Print non-printable characters as '?'.
  -N            Print non-printable characters as is.
  -Q            Quote filenames with double quotes.
  -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.
  --si          Like -h, but use in SI units (powers of 1000).
  -D            Print the date of last modification or (-c) status change.
  -F            Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
  --inodes      Print inode number of each file.
  --device      Print device ID number to which each file belongs.
  ------- Sorting options -------
  -v            Sort files alphanumerically by version.
  -t            Sort files by last modification time.
  -c            Sort files by last status change time.
  -U            Leave files unsorted.
  -r            Reverse the order of the sort.
  --dirsfirst   List directories before files (-U disables).
  --sort X      Select sort: name,version,size,mtime,ctime.
  ------- Graphics options ------
  -i            Don't print indentation lines.
  -A            Print ANSI lines graphic indentation lines.
  -S            Print with CP437 (console) graphics indentation lines.
  -n            Turn colorization off always (-C overrides).
  -C            Turn colorization on always.
  ------- XML/HTML/JSON options -------
  -X            Prints out an XML representation of the tree.
  -J            Prints out an JSON representation of the tree.
  -H baseHREF   Prints out HTML format with baseHREF as top directory.
  -T string     Replace the default HTML title and H1 header with string.
  --nolinks     Turn off hyperlinks in HTML output.
  ---- Miscellaneous options ----
  --version     Print version and exit.
  --help        Print usage and this help message and exit.
  --            Options processing terminator.

常用的命令参数列出如下:

-------清单选项-------
  -a 列出所有文件和目录。
  -d 仅仅列出目录名称。
  -f 打印显示每个文件的完整路径前缀。
  -L 后面跟数字,控制列出几级目录。
  --------文件选项---------
  -q 用"?"号取代控制字符,列出文件和目录名称。
  -N 直接列出文件和目录名称,包括控制字符。
  -Q 用双引号引用文件名。
  -p 列出文件的权限标示。
  -u 列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。
  -g 列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码。
  -s 打印每个文件的字节大小。
  -------排序选项-------
  -v 按文本字母数字排序文件。
  -t 按上次修改时间对文件进行排序。
  -c 按上次状态更改时间对文件进行排序。
  -U 保留文件未排序。
  -r 反转排序的顺序。
  --dirsfirst列出文件之前的目录(-U禁用)。
  -------图形选项------
  -i 不要打印缩进线。
  -A 打印ANSI线图形缩进线。
  -S 使用CP437(控制台)图形缩进线打印。
  -n 始终关闭着色(-C覆盖)。
  -C 总是打开彩色。
  ------- XML / HTML / JSON选项-------
  -X 打印树的XML表示。
  -J 打印出树的JSON表示。
  -H baseHREF以baseHREF作为顶层目录打印HTML格式。
  ----其他选项----
  --version打印版本并退出。

使用示例:

wendingding:wendingdingDemo wendingding$ cd /Users/文顶顶/Desktop/wendingdingDemo
wendingding:wendingdingDemo wendingding$ tree -L 1
.
├── Gruntfile.js
├── demo
├── demo.html
├── dist
├── index.html
├── node_modules
├── package-lock.json
├── package.json
└── src

4 directories, 5 files