本文主要是介绍linux如下显示目录和子目录的树形结构,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目 录
一、需求
二、解决方法
三、tree工具的安装
1. centos中安装tree
2,在Ubuntu系统中安装tree
3,使用源码编译安装
四、tree的语法
1、语法
2、常用选项
一、需求
应用程序的目录中包含一些子目录,目录嵌套很多,特别是一些web应用程序,有时候需要了解这个应用的目录结构,方便后续的工作。有没有类似windows胡总的资源管理器的方式,在终端界面,可以一目了然的看到目录和子目录的树形结构?
二、解决方法
在Linux中,可以使用tree命令来显示目录和子目录的树形结构。以下是如何使用tree命令的一些基本示例:
1. 显示当前目录及其子目录的树形结构:
tree
2. 显示特定目录及其子目录的树形结构:
tree 指定目录的名称
3. 限制显示的目录深度:例如,只显示当前目录及其子目录的前三层:
tree -L 3
4. 显示文件和目录的详细信息:
tree -d
5. 显示隐藏文件和目录:默认情况下,tree命令不会显示以.开头的隐藏文件和目录。使用以下命令可以显示它们:
tree -a
6. 显示文件和目录的大小:
tree -h
7. 只显示目录,不显示文件:
tree -f
8. 以ASCII字符显示树形结构:这可以用于在文本终端上更好地可视化树形结构。
tree --ascii
9. 按照修改时间排序输出:
tree --sort=modtime
10. 按照文件名排序输出:
tree --sort=name
11. 按照文件大小排序输出:
tree --sort=size
三、tree工具的安装
1. centos中安装tree
以 root 用户或使用 sudo 命令执行以下命令来安装 tree:
[root@localhost nginx_web]#sudo yum install tree
或者如果你的 CentOS 系统使用的是 dnf(通常在较新的版本中),则执行:
[root@localhost nginx_web]#sudo dnf install tree
安装完成后,你可以使用 tree 命令来显示目录和子目录的树形结构。
2,在Ubuntu系统中安装tree
输入以下命令以更新软件包列表:
[root@localhost nginx_web]#sudo apt update
输入以下命令以安装tree命令:
[root@localhost nginx_web]#sudo apt install tree
3,使用源码编译安装
(1)下载tree的源码包。你可以在tree的官方网站或开源仓库中找到最新的源码包。通常的下载命令格式如下:
[root@localhost nginx_web]#wget http://www.some-mirror.org/path/to/tree-source.tar.gz
(2)解压源码包。使用以下命令解压源码包:
[root@localhost nginx_web]#tar -zxvf tree-source.tar.gz
(3)进入解压后的目录。使用以下命令进入目录:
[root@localhost nginx_web]#cd tree-new
(4)编译源码。使用以下命令编译源码:
[root@localhost tree-new]#make
(5)安装编译结果。使用以下命令安装tree命令:
[root@localhost tree-new]#sudo make install
完成安装。安装完成后,你就可以在终端中使用tree命令了。
四、tree的语法
1、语法
Linux centos系统的tree命令的基本语法如下:
tree [选项] [目录名]
在终端输入帮助命令: tree –help,显示如下:
[root@localhost nginx_web]# tree --help usage: tree [-acdfghilnpqrstuvxACDFQNSUX] [-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>] [<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.--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.--du Print directory sizes.--prune Prune empty directories from the output.-------- 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.-r Sort files in reverse alphanumeric order.-t Sort files by last modification time.-c Sort files by last status change time.-U Leave files unsorted.--dirsfirst List directories before files (-U disables).------- Graphics options -------i Don't print indentation lines.-A Print ANSI lines graphic indentation lines.-S Print with ASCII graphics indentation lines.-n Turn colorization off always (-C overrides).-C Turn colorization on always.------- XML/HTML options --------X Prints out an XML 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. [root@localhost nginx_web]#
2、常用选项
-a:显示所有文件和目录,包括隐藏文件和目录。
-d:只显示目录,不显示文件。
-f:显示文件的完整路径信息。
-i:不以ASCII字符显示连线符号,而是使用竖线和连线符号。
-L <num>:指定要显示的目录层数,例如-L 2表示只显示两层目录。
-p:显示文件和目录的权限信息。
-s:以文件大小的形式显示文件和目录,单位为KB。
-u:显示文件和目录的创建时间。
-D:以日期格式显示文件和目录的创建时间。
-h:以人类可读的方式显示文件和目录的大小。
这篇关于linux如下显示目录和子目录的树形结构的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!