通用功能——Mac 使用tree打印目录结构

摘要

最近在研究源码,需要快速打印当前目录的结构,于是找到便有了这篇文章,这里简要介绍一下tree的使用(该命令其实就是ls 命令的加强版本)。

[TOC]

安装

Mac机器上如果安装了brew,安装tree就很简单,直接运行命令brew install tree即可,安装成功后,可以执行which tree来验证是否安装成功。

PS: 如果没有安装brew,先输入如下命令安装:

1
ruby -e"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

或者:

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

使用

拿到一个命令,首先使用tree --help来查看其帮助文档了,运行结果如下:

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
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 目录下的所有文件(目录和文件)都会列出来
-d 目录下的所有目录都会列出来
-l 将替身文件的原身在后面展示出来
-f 打印出目录或文件在当前目录下的全路径
-x 只打印当前文件系统的的目录信息
-L level 打印的深度
-R 达到最大支持的文件数后退出tree命令
-P pattern 正则打印出符合规则的文件
-I pattern 正则过滤掉符合规则的文件
--ignore-case 正则匹配时忽略大小写
--matchdirs Include directory names in -P pattern matching.
--noreport 打印完成后不显示列出的目录和文件的数目
--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 将目录结构输出到指定的filename文件中
-------- File options ---------
-q 不支持的字符用问好替代.
-N 不支持的字符原样输出(如中文).
-Q 输出用“”
-p 打印出文件的权限
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s 文件的大小(以字节为单位)
-h 文件的大小(更智能的显示kb,mb等)
--si 文件的大小(但按1kb = 1000b来计算
-D 输出文件最后一次更改的时间
-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 根据版本对文件进行字母数字排序
-t 根据mtime来排序(配置-U时失效)
-c 根据ctime来排序(配置-U时失效)
-U 不排序
-r 当前排序逆序排列
--dirsfirst 目录优先(配置-U时失效)
--sort X 选择排序方式(支持name,version,size,mtime,ctime等)
------- Graphics options ------
-i 不打印缩进
-A Print ANSI lines graphic indentation lines.
-S Print with CP437 (console) graphics indentation lines.
-n 禁用文件着色
-C 启用文件着色.
------- 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 查看tree的版本
--help Print usage and this help message and exit.
-- Options processing terminator.

所有支持的命令都列举在上面了,下面看看几个常用的命令。

常用命令

  • tree --help,查看帮助
  • tree -d`,只列出目录
  • tree -N,字符原样打印(中文支持)
  • tree -L 3,指定打印层级为3

补充说明

文件的三个时间的概念

  • Modification time (mtime):当该文件的『内容数据』变更时,就会更新这个时间! 『内容数据』指的是文件中记录的内容,而不包括文件属性和权限等!
  • Change time (ctime):当该文件的『状态 (status)』改变时,就会更新这个时间,举例来说, 像是文件权限、属性、inode号等被更改了,都会更新这个时间。
  • Access time (atime):当我们访问该文件时,就会更新这个时间为最后一次访问该文件的时间 。 当我们使用 cat 、more、less等命令读取文件信息的时候,就会更新 atime 了。

采用 stat命令可以查看文件的三个时间。

具体的使用,可以根据自己的需要,查看帮助文档来按需实现