Linux `tree` 命令 - 树形结构显示文件夹目录结构 [list contents of directories in a tree-like format]

In computing, tree is a recursive directory listing command or program that produces a depth-indented listing of files.
tree 是一个递归列出目录的命令,或者说是一个产生带有深度缩进文件列表的计算机程序。

以树状图列出目录的内容。

tree - list contents of directories in a tree-like format.

1. Installation

(base) yongqiang@yongqiang:~$ tree

Command 'tree' not found, but can be installed with:

sudo apt install tree

(base) yongqiang@yongqiang:~$
(base) yongqiang@yongqiang:~$ sudo apt install tree
[sudo] password for yongqiang:
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 58 not upgraded.
Need to get 40.7 kB of archives.
After this operation, 105 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 tree amd64 1.7.0-5 [40.7 kB]
Fetched 40.7 kB in 2s (19.3 kB/s)
Selecting previously unselected package tree.
(Reading database ... 35027 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-5_amd64.deb ...
Unpacking tree (1.7.0-5) ...
Setting up tree (1.7.0-5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
(base) yongqiang@yongqiang:~$

2. 树形结构显示文件夹目录结构

2.1. tree

显示当前目录的树形结构。

(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ tree
.
├── CMakeLists.txt
├── cmake-build-debug
├── include
│   └── batch_norm.h
├── main.cpp
└── source
    └── batch_norm.cpp

3 directories, 4 files
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$

2.2. -L level

Max display depth of the directory tree. - 目录树的最大显示深度。

tree -L N - 查看当前第 N 级的目录和文件
tree -L 1 - 查看当前第 1 级的目录和文件

(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ tree -L 1
.
├── CMakeLists.txt
├── cmake-build-debug
├── include
├── main.cpp
└── source

3 directories, 2 files
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$

tree -L 2 - 查看当前第二级的目录和文件

(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ tree -L 2
.
├── CMakeLists.txt
├── cmake-build-debug
├── include
│   └── batch_norm.h
├── main.cpp
└── source
    └── batch_norm.cpp

3 directories, 4 files
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$

2.3. tree > /home/yongqiang/CLionProjects/yongqiang/tree.txt

目录结构信息保存到文本中。

(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ pwd
/home/yongqiang/CLionProjects/yongqiang
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ tree > /home/yongqiang/CLionProjects/yongqiang/tree.txt
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$ cat tree.txt
.
├── CMakeLists.txt
├── cmake-build-debug
├── include
│   └── batch_norm.h
├── main.cpp
├── source
│   └── batch_norm.cpp
└── tree.txt

3 directories, 5 files
(base) yongqiang@yongqiang:~/CLionProjects/yongqiang$

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐