使用tree命令生成目录树结构

经常在一些开源项目中看到目录树结构,感觉很清晰,发现有工具可以一键生成,舒服
Snipaste_2019-09-02_19-19-54.png

References:

用到的工具为tree,是一个命令行工具。
首先在Linux下试试,在我的树莓派上如下:

[root@raspberrypi31 ~]# apt install tree
Reading package lists... Done
Building dependency tree        
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 41.4 kB of archives.
After this operation, 93.2 kB of additional disk space will be used.
Get:1 http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian stretch/main armhf tree armhf 1.7.0-5 [41.4 kB]
Fetched 41.4 kB in 5s (7,910 B/s)              
Selecting previously unselected package tree.
(Reading database ... 37449 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-5_armhf.deb ...
Unpacking tree (1.7.0-5) ...
Setting up tree (1.7.0-5) ...
Processing triggers for man-db (2.7.6.1-2) ...
[root@raspberrypi31 ~]# 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.
[root@raspberrypi31 ~]#

试一下:

[root@raspberrypi31 tmp]# tree bilibili/
bilibili/
├── auth
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   ├── com
│           │   │   └── racecoder
│           │   │       └── bilibili
│           │   │           ├── AuthApplication.java
│           │   │           ├── config
│           │   │           │   ├── OAuth2AuthorizationServerConfig.java
│           │   │           │   ├── OAuth2ResourceServerConfig.java
│           │   │           │   ├── OAuth2ServerConfig.java
│           │   │           │   ├── RedisConfig.java
│           │   │           │   ├── RSAProp.java
│           │   │           │   ├── SecurityConfig.java
│           │   │           │   └── TokenConfig.java
│           │   │           ├── controller
│           │   │           │   └── UserController.java
│           │   │           ├── repository
│           │   │           │   └── UserRepository.java
│           │   │           └── service
│           │   │               └── impl
│           │   │                   └── UserDetailsServiceImpl.java
│           │   └── META-INF
│           │       └── additional-spring-configuration-metadata.json
│           └── resources
│               ├── application.yml
│               ├── banner.txt
│               ├── logback-spring.xml
│               └── static
│                   └── favicon.ico
├── common
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── com
│                   └── racecoder
│                       └── bilibili
│                           ├── config
│                           │   └── RestTemplateConfig.java
│                           └── util
│                               ├── ConvertUtil.java
│                               ├── HttpUtil.java
│                               ├── ResultCode.java
│                               └── Result.java
├── config
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── racecoder
│           │           └── bilibili
│           │               └── ConfigApplication.java
│           └── resources
│               ├── application.yml
│               ├── banner.txt
│               ├── logback-spring.xml
│               └── static
│                   └── favicon.ico
├── config_repo
├── eureka
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── racecoder
│           │           └── bilibili
│           │               ├── config
│           │               │   └── SecurityConfig.java
│           │               └── EurekaApplication.java
│           └── resources
│               ├── application.yml
│               ├── banner.txt
│               ├── logback-spring.xml
│               └── static
│                   └── favicon.ico
├── gateway
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── racecoder
│           │           └── bilibili
│           │               └── GatewayApplication.java
│           └── resources
│               ├── application.yml
│               ├── banner.txt
│               ├── logback-spring.xml
│               └── static
│                   └── favicon.ico
├── pom.xml
├── turbine-dashboard
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── com
│           │       └── racecoder
│           │           └── bilibili
│           │               ├── config
│           │               │   └── SecurityConfig.java
│           │               └── TurbineDashboardApplication.java
│           └── resources
│               ├── application.yml
│               ├── banner.txt
│               ├── logback-spring.xml
│               └── static
│                   └── favicon.ico
├── user
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── com
│       │   │       └── racecoder
│       │   │           └── bilibili
│       │   │               ├── component
│       │   │               │   ├── CacheConst.java
│       │   │               │   ├── ControllerConfig.java
│       │   │               │   └── RedisScripts.java
│       │   │               ├── config
│       │   │               │   ├── MyBatisConfig.java
│       │   │               │   ├── RedisConfig.java
│       │   │               │   ├── RestTemplateConfig.java
│       │   │               │   ├── SecurityConfig.java
│       │   │               │   └── UserAspect.java
│       │   │               ├── dto
│       │   │               │   ├── Pager.java
│       │   │               │   └── SexEnum.java
│       │   │               ├── exception
│       │   │               │   └── UserNotFoundException.java
│       │   │               ├── model
│       │   │               │   ├── ProductExample.java
│       │   │               │   ├── Product.java
│       │   │               │   ├── PurchaseRecordExample.java
│       │   │               │   ├── PurchaseRecord.java
│       │   │               │   ├── UserExample.java
│       │   │               │   └── User.java
│       │   │               ├── repository
│       │   │               │   ├── PackageRepository.java
│       │   │               │   ├── ProductMapper.java
│       │   │               │   ├── PurchaseRecordMapper.java
│       │   │               │   ├── typehandler
│       │   │               │   │   └── SexTypeHandler.java
│       │   │               │   ├── UserMapper.java
│       │   │               │   └── xml
│       │   │               │       ├── ProductMapper.xml
│       │   │               │       ├── PurchaseRecordMapper.xml
│       │   │               │       └── UserMapper.xml
│       │   │               ├── service
│       │   │               │   ├── impl
│       │   │               │   │   ├── ProductServiceImpl.java
│       │   │               │   │   ├── PurchaseRecordServiceImpl.java
│       │   │               │   │   └── UserServiceImpl.java
│       │   │               │   ├── ProductService.java
│       │   │               │   ├── PurchaseRecordService.java
│       │   │               │   └── UserService.java
│       │   │               ├── UserApplication.java
│       │   │               └── web
│       │   │                   ├── PackageWeb.java
│       │   │                   ├── PurchaseController.java
│       │   │                   ├── RedisController.java
│       │   │                   ├── UserController.java
│       │   │                   └── UserFeignClient.java
│       │   └── resources
│       │       ├── application.yml
│       │       ├── banner.txt
│       │       ├── generatorConfig.xml
│       │       ├── logback-spring.xml
│       │       ├── scripts
│       │       │   ├── compare.lua
│       │       │   └── purchase.lua
│       │       └── static
│       │           └── favicon.ico
│       └── test
│           └── java
│               └── com
│                   └── racecoder
│                       └── bilibili
│                           └── DemoApplicationTests.java
└── video
    ├── pom.xml
    └── src
        └── main
            ├── java
            │   └── com
            │       └── racecoder
            │           └── bilibili
            │               ├── component
            │               │   └── ControllerConfig.java
            │               ├── config
            │               │   ├── RestTemplateConfig.java
            │               │   └── SecurityConfig.java
            │               ├── model
            │               │   └── User.java
            │               ├── VideoApplication.java
            │               └── web
            │                   ├── PackageWeb.java
            │                   ├── UserController.java
            │                   └── UserFeignClient.java
            └── resources
                ├── banner.txt
                ├── bootstrap.yml
                ├── logback-spring.xml
                └── static
                    └── favicon.ico

102 directories, 109 files
[root@raspberrypi31 tmp]#

效果还行,可以用流直接生成到文件中:

[root@raspberrypi31 tmp]# tree bilibili/ > arch.txt

看起来Linux上是没问题的,那Windows呢?找了下,发现Windows自带这个工具!!?直接tree搞起来。

D:\gitRepo\bilibili>tree /?
以图形显示驱动器或路径的文件夹结构。

TREE [drive:][path] [/F] [/A]

   /F   显示每个文件夹中文件的名称。
   /A   使用 ASCII 字符,而不使用扩展字符。


D:\gitRepo\bilibili>tree /a /f > output.txt

D:\gitRepo\bilibili>

可以看到Windows下选项很少,只有2个,如果要玩骚操作还是上Linux吧,但正常使用Windows的应该够了。

标签: none

添加新评论

ali-01.gifali-58.gifali-09.gifali-23.gifali-04.gifali-46.gifali-57.gifali-22.gifali-38.gifali-13.gifali-10.gifali-34.gifali-06.gifali-37.gifali-42.gifali-35.gifali-12.gifali-30.gifali-16.gifali-54.gifali-55.gifali-59.gif

加载中……