Mac下使用tree命令展示文件树

背景

在写代码文档的时候,经常会用到展示项目架构,这时候如果可以有命令直接打印出目录树那就再好不过了,免的截图了。

思路

网上找了下,果然是有这种工具的,Mac - tree命令。

Mac默认是没有tree命令的,需要手工安装下:

1
brew install tree

安装好之后,看下帮助文档:

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
61
62
63
64
$ 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] [--fromfile] [--]
[<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.
------- Input options -------
--fromfile Reads paths from files (.=stdin)
------- Miscellaneous options -------
--version Print version and exit.
--help Print usage and this help message and exit.
-- Options processing terminator.

可以添加的参数很多,那么该用那些呢?

  • 在一个python项目中,先只加文件夹名看下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    $ tree app
    app
    ├── __init__.py
    ├── __pycache__
    │   └── __init__.cpython-37.pyc
    ├── main
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-37.pyc
    │   │   ├── functions.cpython-37.pyc
    │   │   └── views.cpython-37.pyc
    │   ├── functions.py
    │   └── views.py
    └── module
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-37.pyc
    │   ├── functions.cpython-37.pyc
    │   └── views.cpython-37.pyc
    ├── functions.py
    └── views.py

    5 directories, 14 files
  • pyc是编译的临时文件,我们要把删掉,看下说明,可以用-I来:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $ tree -I *.pyc app
    app
    ├── __init__.py
    ├── __pycache__
    ├── main
    │   ├── __init__.py
    │   ├── __pycache__
    │   ├── functions.py
    │   └── views.py
    └── module
    ├── __init__.py
    ├── __pycache__
    ├── functions.py
    └── views.py

    5 directories, 7 files
  • __pycache__也是临时文件,也把删掉:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    tree -I *.pyc -I __pycache__  app
    app
    ├── __init__.py
    ├── main
    │   ├── __init__.py
    │   ├── functions.py
    │   └── views.py
    └── module
    ├── __init__.py
    ├── functions.py
    └── views.py

    2 directories, 7 files

    可以看出-I是可以加多个的,每个-I后面加一个pattern

    在上面的例子中,其实所有的.pyc文件都在__pychache__文件夹下,可以直接忽略该文件夹即可:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    $ tree -I __pycache__  app
    app
    ├── __init__.py
    ├── main
    │   ├── __init__.py
    │   ├── functions.py
    │   └── views.py
    └── module
    ├── __init__.py
    ├── functions.py
    └── views.py

    2 directories, 7 files
  • 那么如果只要文件夹的结构呢?-d参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ tree -d app
    app
    ├── __pycache__
    ├── main
    │   └── __pycache__
    └── module
    └── __pycache__

    5 directories
  • 忽略__pycache__文件夹:

    1
    2
    3
    4
    5
    6
    $ tree -d -I __pycache__ app
    app
    ├── main
    └── module

    2 directories

总结

通过brew安装tree工具之后,即可在命令行中使用tree命令展示文件\文件夹目录树:

  • 直接加对应的文件夹来展示某文件夹范围内的文件树

    1
    $ tree app
  • 使用-I参数来忽略不展示的文件或子文件夹,可添加多个-I

    1
    $ tree -I *.pyc -I __pycache__  app
  • 使用-d来仅展示文件夹树

    1
    $ tree -d app
  • 多参数可以混合使用

    1
    $ tree -d -I __pycache__ app
  • 更多的参数使用,可以在有需要的时候参考--help内容

    1
    $ tree --help