Linux topics for: multi-level directory tree display --tree
https://www.cnblogs.com/tp1226/p/8456539.html tree -L 3
Recently wrote a blog when occasionally need to file directory structure visually listed, such as packet structure of python.
So in Internet search, search, find the next Linux is also a good tool --tree
tree can visually display the multi-level directory structure.
1. Installation
Directly on Ubuntu
sudo apt install tree
2. A few of the more conventional usage:
1. Display the directory structure
[root@ Test]# tree . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt ├── Level-2-1 │ ├── L21-File-1.txt │ ├── Level-3-1 │ │ └── L31-File-1.txt │ ├── Level-3-2 │ │ └── L32-File-1.txt │ └── Level-3-3 │ └── L33-File-1.txt ├── Level-2-2 │ └── L22-File-1.txt └── Level-2-3 7 directories, 7 files
2. Include hidden files
[root@ Test]# tree -a . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt ├── .L1-hide.dat ├── Level-2-1 │ ├── L21-File-1.txt │ ├── .L2-hide.dat │ ├── Level-3-1 │ │ └── L31-File-1.txt │ ├── Level-3-2 │ │ └── L32-File-1.txt │ └── Level-3-3 │ └── L33-File-1.txt ├── Level-2-2 │ └── L22-File-1.txt └── Level-2-3 7 directories, 9 files
3. Control depth (assumed to be 3)
[root@ Test]# tree -L 3 . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt ├── Level-2-1 │ ├── L21-File-1.txt │ ├── Level-3-1 │ ├── Level-3-2 │ └── Level-3-3 ├── Level-2-2 │ └── L22-File-1.txt └── Level-2-3 7 directories, 4 files
4. Display directories only
[root@ Test]# tree -d . └── Level-1 ├── Level-2-1 │ ├── Level-3-1 │ ├── Level-3-2 │ └── Level-3-3 ├── Level-2-2 └── Level-2-3 7 directories
The document to be displayed on the filter
# Simply display a file "L2" of the string, and the filter filtering out empty directory also [root@ Test]# tree -P '*L2*' --prune . └── Level-1 ├── Level-2-1 │ └── L21-File-1.txt └── Level-2-2 └── L22-File-1.txt 3 directories, 2 files It does not include file # Displays "L2" of the string, and the filter filtering out empty directory also [root@ Test]# [root@ Test]# tree -I '*L2*' --prune . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt └── Level-2-1 ├── Level-3-1 │ └── L31-File-1.txt ├── Level-3-2 │ └── L32-File-1.txt └── Level-3-3 └── L33-File-1.txt 5 directories, 5 files
3. More options
Options | Explanation |
-a | Show all files including hidden files. |
-d | Display the directory only. |
-l | Follow symbolic links if the link is a directory, the directory as a process. |
-f | Show full path. |
-x | Show only this file system. |
-L level | Control directory displayed depth. |
-R | In the lower-level directory tree command is executed again and add '-o 00Tree.html' option, with -L, -H use. |
-P pattern | Only matching the pattern file (not a directory), support simple regular expressions. |
-I pattern | In contrast with the -P, show only the file does not match the pattern. |
--ignore-case | When using the -P or -I option, ignore case. |
--matchdirs | When using the -P option, contains the full path to the file name. |
--prune | Does not display an empty directory, there is no need to display if no directory or through -P -I after the next, but also as an empty directory. |
--noreport | The final statistics are not displayed. |
--charsetcharset | Specified character marks collection. |
--filelimit # | The number of file filter out more than # directory. |
--timefmt fmt | According to the modification time in the format specified print file. |
-o filename | The output to a file. |
-q | Instead of unprintable characters with a question mark. |
-N | Octal character in place of unprintable. |
-Q | Enclose the file name in quotes. |
-p | Type and display file permissions. |
-u | Username or display the file belongs UID. |
-g | Group displays the file belongs or GID. |
-s | The size of the file is displayed, the unit: byte. |
-h | The size of the file is displayed, the use of more humane display. |
--and | The size of the file is displayed, but using -h similar international metric units (1k = 1000). |
--of | For a directory, it shows the cumulative size of all files under it. |
-D | Last Modified Displays file. |
-F | Similarly ls -F, for different file types, plus at the end of a different character. |
--inodes | Inode file is displayed. |
--device | Display file belongs device number. |
-v | Show list of files in accordance with the version of the sort. |
-t | Display a list of files sorted according to the last modification time. |
-c | Show list of files sorted by time of last change of state. |
-U | No sorting process. |
-r | Reverse output list. |
--dirsfirst | Prioritize directory (same level) |
--sort[=name] | Specify how to sort, name (default), ctime, mtime, size, version. |
-i | Do not indent the output. |
-A | Use the ASCII character representation of the horizontal line indent. |
-S | 使用CP437的横线字符表示缩进。 |
-n | 关闭颜色显示。 |
-C | 打开颜色显示。 |
-X | 使能XML格式输出。 |
-J | 使能JSON格式输出。 |
-H baseHREF | 使能HTML格式输出,并包含基本http链接地址。 |
-T title | 在HTML格式输出中,设置标题和H1标签头 |
--nolinks | 在HTML格式输出中,不输出超链接。 |
最近写博客的时候偶尔会需要将文件目录结构直观地列出来,例如python的包结构。
于是在网上搜了搜,发现了一个Linux下还不错的工具--tree
tree 可以很直观地显示多级目录结构。
1. 安装方法
Ubuntu上直接
sudo apt install tree
2. 几个比较常规的用法:
1. 显示目录结构
[root@ Test]# tree . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt ├── Level-2-1 │ ├── L21-File-1.txt │ ├── Level-3-1 │ │ └── L31-File-1.txt │ ├── Level-3-2 │ │ └── L32-File-1.txt │ └── Level-3-3 │ └── L33-File-1.txt ├── Level-2-2 │ └── L22-File-1.txt └── Level-2-3 7 directories, 7 files
2. 包含隐藏文件
[root@ Test]# tree -a . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt ├── .L1-hide.dat ├── Level-2-1 │ ├── L21-File-1.txt │ ├── .L2-hide.dat │ ├── Level-3-1 │ │ └── L31-File-1.txt │ ├── Level-3-2 │ │ └── L32-File-1.txt │ └── Level-3-3 │ └── L33-File-1.txt ├── Level-2-2 │ └── L22-File-1.txt └── Level-2-3 7 directories, 9 files
3. 控制深度(假设为3)
[root@ Test]# tree -L 3 . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt ├── Level-2-1 │ ├── L21-File-1.txt │ ├── Level-3-1 │ ├── Level-3-2 │ └── Level-3-3 ├── Level-2-2 │ └── L22-File-1.txt └── Level-2-3 7 directories, 4 files
4. 只显示目录
[root@ Test]# tree -d . └── Level-1 ├── Level-2-1 │ ├── Level-3-1 │ ├── Level-3-2 │ └── Level-3-3 ├── Level-2-2 └── Level-2-3 7 directories
5. 对需要显示的文件进行过滤
# 只显示包含 "L2"字符串的文件,并将过滤后的空目录也同时过滤掉 [root@ Test]# tree -P '*L2*' --prune . └── Level-1 ├── Level-2-1 │ └── L21-File-1.txt └── Level-2-2 └── L22-File-1.txt 3 directories, 2 files # 只显示不包含 "L2"字符串的文件,并将过滤后的空目录也同时过滤掉 [root@ Test]# [root@ Test]# tree -I '*L2*' --prune . └── Level-1 ├── L1-File-1.txt ├── L1-File-2.txt └── Level-2-1 ├── Level-3-1 │ └── L31-File-1.txt ├── Level-3-2 │ └── L32-File-1.txt └── Level-3-3 └── L33-File-1.txt 5 directories, 5 files
3. 更多的选项
选项 | 说明 |
-a | 显示所有文件,包含隐藏文件。 |
-d | 只显示目录。 |
-l | 跟踪符号链接,如果链接的是一个目录,则当成目录处理。 |
-f | 显示完整路径。 |
-x | 只显示本文件系统。 |
-L level | 控制显示的目录深度。 |
-R | 在下级目录中,再次执行 tree 命令并且加上 '-o 00Tree.html'选项,配合-L,-H使用。 |
-P pattern | 只显示匹配了 pattern 的文件(不是目录),支持简单的正则表达式。 |
-I pattern | 与-P相反,只显示没有匹配 pattern的文件。 |
--ignore-case | 当使用了-P或-I选项时,忽略大小写。 |
--matchdirs | 当使用了-P选项时,文件名包含完整路径。 |
--prune | 不显示空目录,如果经过-P或-I后没有目录下没有需要显示的,也当作空目录。 |
--noreport | 不显示最后的统计信息。 |
--charsetcharset | 指定字符集。 |
--filelimit # | 过滤掉文件个数超过 # 的目录。 |
--timefmt fmt | 按照指定的格式打印文件的修改时间。 |
-o filename | 将结果输出到文件。 |
-q | 用问号代替不可打印的字符。 |
-N | 用八进制代替不可打印的字符。 |
-Q | 用引号将文件名括起来。 |
-p | 显示文件的类型和权限。 |
-u | 显示文件所属的用户名或者UID。 |
-g | 显示文件所属的组或者GID。 |
-s | 显示文件的大小,单位:字节。 |
-h | 显示文件的大小,使用更人性化的显示。 |
--si | 显示文件的大小,类似 -h 但是使用国际公制单位(1k=1000)。 |
--du | 对于目录,显示其下所有文件的累计大小。 |
-D | 显示文件的最后修改时间。 |
-F | 类似 ls -F,对不同的文件类型,在末尾加上不同的字符。 |
--inodes | 显示文件的索引节点。 |
--device | 显示文件所属的设备号。 |
-v | 显示的文件列表按照version排序。 |
-t | 显示的文件列表按照最后修改时间排序。 |
-c | 显示的文件列表按照最后的状态改变时间排序。 |
-U | 不进行排序处理。 |
-r | 反向输出列表。 |
--dirsfirst | 优先显示目录(同一级别 ) |
--sort[=name] | 指定排序方式,name(default), ctime, mtime, size, version. |
-i | 输出中不要进行缩进。 |
-A | 使用ASCII的横线字符表示缩进。 |
-S | CP437 character representation using a horizontal line indent. |
-n | Turn off the color display. |
-C | Open the color display. |
-X | Enabling XML format output. |
-J | Enable output JSON format. |
-H baseHREF | Enable output in HTML format, and contains basic http link address. |
-T title | In HTML output format, and provided the title tag header H1 |
--nolinks | In HTML output format, the hyperlink is not output. |