场景

在某些情况下,我们需要将java项目或其他项目的目录结构通过控制台打印出来,更直观的看到整个项目的情况,而不是通过代码遍历的形式打印。

基于这种应用场景,在mac和window操作系统下都可以使用tree工具来进行打印。这里重点基于mac操作系统来进行相应的安装和演示。以下操作均基于Mac操作系统。

安装tree

打开控制台,使用brew包管理工具安装tree。执行命令:

brew install tree

如果执行过程中遇到如下问题:

ershixiongdeMacBook-Pro:~ zzs$ brew install tree
Error: You have not agreed to the Xcode license. Please resolve this by running:
  sudo xcodebuild -license accept

按照指示,执行命令:

sudo xcodebuild -license accept

输入电脑密码,再次执行上面的安装tree命令。系统首先会更新Homebrew,可能会花费一些时间,等待信息:

Updating Homebrew...

等待HomeBrew更新完成,会自动安装tree,安装结果显示如下信息:

==> Downloading https://homebrew.bintray.com/bottles/tree-1.8.0.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring tree-1.8.0.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/tree/1.8.0: 8 files, 117KB
==> `brew cleanup` has not been run in 30 days, running now...
Pruned 2 symbolic links and 3 directories from /usr/local

命令操作

安装完成可通过help命令查看使用方法,并打印出如下信息:

ershixiongdeMacBook-Pro:~ zzs$ 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.

然后,通过命令行进入需要打印项目路径的项目下,直接输入命令tree,即可打印该目录下的所有层级及文件结构。

当然,也通过“-L”参数来控制打印目录的层级。比如,以下为打印Spring Boot项目2层目录结果的操作:

ershixiongdeMacBook-Pro:spring-boot zzs$ tree -L 2
.
├── pom.xml
├── spring-boot.iml
├── src
│   ├── main
│   └── test
└── target
    ├── checkstyle-cachefile
    ├── checkstyle-checker.xml
    ├── checkstyle-header.txt
    ├── checkstyle-result.xml
    ├── checkstyle-suppressions.xml
    ├── classes
    ├── generated-sources
    ├── maven-archiver
    ├── maven-status
    ├── spring-boot-2.2.0.BUILD-SNAPSHOT-sources.jar
    └── spring-boot-2.2.0.BUILD-SNAPSHOT.jar

8 directories, 9 files
ershixiongdeMacBook-Pro:spring-boot zzs$ cd ..
ershixiongdeMacBook-Pro:spring-boot-project zzs$ tree -L 2
.
├── pom.xml
├── spring-boot
│   ├── pom.xml
│   ├── spring-boot.iml
│   ├── src
│   └── target
├── spring-boot-actuator
│   ├── README.adoc
│   ├── pom.xml
│   ├── spring-boot-actuator.iml
│   ├── src
│   └── target
├── spring-boot-actuator-autoconfigure
│   ├── pom.xml
│   ├── spring-boot-actuator-autoconfigure.iml
│   ├── src
│   └── target
├── spring-boot-autoconfigure
│   ├── pom.xml
│   ├── spring-boot-autoconfigure.iml
│   ├── src
│   └── target
├── spring-boot-cli
│   ├── pom.xml
│   ├── samples
│   ├── spring-boot-cli.iml
│   ├── src
│   └── test-samples
├── spring-boot-dependencies
│   ├── pom.xml
│   ├── spring-boot-dependencies.iml
│   ├── src
│   └── target
├── spring-boot-devtools
│   ├── pom.xml
│   ├── spring-boot-devtools.iml
│   ├── src
│   └── target
├── spring-boot-docs
│   ├── pom.xml
│   ├── spring-boot-docs.iml
│   └── src
├── spring-boot-parent
│   ├── pom.xml
│   ├── spring-boot-parent.iml
│   └── target
├── spring-boot-project.iml
├── spring-boot-properties-migrator
│   ├── pom.xml
│   ├── spring-boot-properties-migrator.iml
│   ├── src
│   └── target
├── spring-boot-starters
│   ├── README.adoc
│   ├── pom.xml
│   ├── spring-boot-starter
│   ├── spring-boot-starter-activemq
│   ├── spring-boot-starter-actuator
│   ├── spring-boot-starter-amqp
│   ├── spring-boot-starter-aop
│   ├── spring-boot-starter-artemis
│   ├── spring-boot-starter-batch
│   ├── spring-boot-starter-cache
│   ├── spring-boot-starter-cloud-connectors
│   ├── spring-boot-starter-data-cassandra
│   ├── spring-boot-starter-data-cassandra-reactive
│   ├── spring-boot-starter-data-couchbase
│   ├── spring-boot-starter-data-couchbase-reactive
│   ├── spring-boot-starter-data-elasticsearch
│   ├── spring-boot-starter-data-jdbc
│   ├── spring-boot-starter-data-jpa
│   ├── spring-boot-starter-data-ldap
│   ├── spring-boot-starter-data-mongodb
│   ├── spring-boot-starter-data-mongodb-reactive
│   ├── spring-boot-starter-data-neo4j
│   ├── spring-boot-starter-data-redis
│   ├── spring-boot-starter-data-redis-reactive
│   ├── spring-boot-starter-data-rest
│   ├── spring-boot-starter-data-solr
│   ├── spring-boot-starter-freemarker
│   ├── spring-boot-starter-groovy-templates
│   ├── spring-boot-starter-hateoas
│   ├── spring-boot-starter-integration
│   ├── spring-boot-starter-jdbc
│   ├── spring-boot-starter-jersey
│   ├── spring-boot-starter-jetty
│   ├── spring-boot-starter-jooq
│   ├── spring-boot-starter-json
│   ├── spring-boot-starter-jta-atomikos
│   ├── spring-boot-starter-jta-bitronix
│   ├── spring-boot-starter-log4j2
│   ├── spring-boot-starter-logging
│   ├── spring-boot-starter-mail
│   ├── spring-boot-starter-mustache
│   ├── spring-boot-starter-oauth2-client
│   ├── spring-boot-starter-oauth2-resource-server
│   ├── spring-boot-starter-parent
│   ├── spring-boot-starter-quartz
│   ├── spring-boot-starter-reactor-netty
│   ├── spring-boot-starter-rsocket
│   ├── spring-boot-starter-security
│   ├── spring-boot-starter-test
│   ├── spring-boot-starter-thymeleaf
│   ├── spring-boot-starter-tomcat
│   ├── spring-boot-starter-undertow
│   ├── spring-boot-starter-validation
│   ├── spring-boot-starter-web
│   ├── spring-boot-starter-web-services
│   ├── spring-boot-starter-webflux
│   ├── spring-boot-starter-websocket
│   ├── spring-boot-starters.iml
│   ├── src
│   └── target
├── spring-boot-test
│   ├── pom.xml
│   ├── spring-boot-test.iml
│   ├── src
│   └── target
├── spring-boot-test-autoconfigure
│   ├── pom.xml
│   ├── spring-boot-test-autoconfigure.iml
│   ├── src
│   └── target
└── spring-boot-tools
    ├── pom.xml
    ├── spring-boot-antlib
    ├── spring-boot-autoconfigure-processor
    ├── spring-boot-configuration-docs
    ├── spring-boot-configuration-metadata
    ├── spring-boot-configuration-processor
    ├── spring-boot-gradle-plugin
    ├── spring-boot-loader
    ├── spring-boot-loader-tools
    ├── spring-boot-maven-plugin
    ├── spring-boot-test-support
    ├── spring-boot-tools.iml
    └── target

105 directories, 32 files

看起来是不是很帅气,很友好。那么如果想把打印的内容输出到文件中,比如项目的README.md中。可以通过如下命令:

tree -L 2 >README.md

这样,上面打印出来的内容便可以输入到同目录下的README.md文件内了。



Mac下使用tree打印项目目录结构或目录树插图

关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台

除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接

本文链接:http://choupangxia.com/2019/09/22/mac%e4%b8%8b%e4%bd%bf%e7%94%a8tree%e6%89%93%e5%8d%b0%e9%a1%b9%e7%9b%ae%e7%9b%ae%e5%bd%95%e7%bb%93%e6%9e%84%e6%88%96%e7%9b%ae%e5%bd%95%e6%a0%91/