浏览他人技术博客的时候,会看到用文本列出漂亮的文件夹目录,实际大部分都是使用了linux下的Tree命令。以下简单介绍下Tree命令的格式和例子。
安装一般linux系统是不自带Tree命令工具的,可以通过以下命令获取和安装:
sudo apt-get install tree格式
通过上述命令获取后,可以通过一下命令显示使用方法:
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.
可以看到最新的Tree命令工具在老版本基础上已经做了很多扩展,分别有Listing、file 、Graphics、 XML/HTML/JsON 、
举例虽然现在功能越来越强大,但是对于大部分人来说常用的并不多,以下举几个常用的例子:
显示全部层级的目录和文件tree -a
.├── 1.txt├── demo1│ ├── CMakeCache.txt│ ├── CMakefiles│ │ ├── 3.5.1│ │ │ ├── CMakeCCompiler.cmake│ │ │ ├── CMakeCXXCompiler.cmake│ │ │ ├── CMakeDetermineCompilerABI_C.bin│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin│ │ │ ├── CMakeSystem.cmake│ │ │ ├── CompilerIDC│ │ │ │ ├── a.out│ │ │ │ └── CMakeCCompilerID.c│ │ │ └── CompilerIDCXX│ │ │ ├── a.out│ │ │ └── CMakeCXXCompilerID.cpp│ │ ├── cmake.check_cache│ │ ├── CMakeDirectoryinformation.cmake│ │ ├── CMakeOutput.log│ │ ├── CMakeTmp│ │ ├── Demo.dir│ │ │ ├── build.make│ │ │ ├── cmake_clean.cmake│ │ │ ├── CXX.includecache│ │ │ ├── DependInfo.cmake│ │ │ ├── depend.internal│ │ │ ├── depend.make│ │ │ ├── flags.make│ │ │ ├── link.txt│ │ │ ├── main.cc.o│ │ │ └── progress.make│ │ ├── feature_tests.bin│ │ ├── feature_tests.c│ │ ├── feature_tests.cxx│ │ ├── Makefile2│ │ ├── Makefile.cmake│ │ ├── progress.marks│ │ └── TargetDirectorIEs.txt│ ├── cmake_install.cmake│ ├── CMakeLists.txt│ ├── Demo│ ├── main.cc│ └── Makefile└── demo2显示指定层级的目录和文件
tree -L 2
// -L 指定目录深度.├── 1.txt├── demo1│ ├── CMakeCache.txt│ ├── CMakefiles│ ├── cmake_install.cmake│ ├── CMakeLists.txt│ ├── Demo│ ├── main.cc│ └── Makefile└── demo2显示目录/文件地址
tree -L 2 -f
.├── ./1.txt├── ./demo1│ ├── ./demo1/CMakeCache.txt│ ├── ./demo1/CMakefiles│ ├── ./demo1/cmake_install.cmake│ ├── ./demo1/CMakeLists.txt│ ├── ./demo1/Demo│ ├── ./demo1/main.cc│ └── ./demo1/Makefile└── ./demo2仅显示目录不显示文件
tree -L3 -d
.├── demo1│ └── CMakefiles│ ├── 3.5.1│ ├── CMakeTmp│ └── Demo.dir└── demo2区分目录和文件
tree -L 2 -F
.├── 1.txt├── demo1/│ ├── CMakeCache.txt│ ├── CMakefiles/│ ├── cmake_install.cmake│ ├── CMakeLists.txt│ ├── Demo*│ ├── main.cc│ └── Makefile└── demo2/不显示目录树中的层级线
tree -L 2 -i
.1.txtdemo1CMakeCache.txtCMakefilescmake_install.cmakeCMakeLists.txtDemomain.ccMakefiledemo2显示XML格式
tree -L 2 -X
<?xml version="1.0" enCoding="UTF-8"?><tree> <directory name="."> <file name="1.txt"></file> <directory name="demo1"> <file name="CMakeCache.txt"></file> <directory name="CMakefiles"> </directory> <file name="cmake_install.cmake"></file> <file name="CMakeLists.txt"></file> <file name="Demo"></file> <file name="main.cc"></file> <file name="Makefile"></file> </directory> <directory name="demo2"> </directory> </directory> <report> <directorIEs>3</directorIEs> <files>7</files> </report></tree>保存内容
tree -L 2 >> 1.txt总结
作为常用命令,Tree命令在平时使用中可以帮助我们快速了解当前的目录/文件结构,也可以辅助写处更漂亮的博客。
@H_404_0@ 总结以上是内存溢出为你收集整理的Linux 实用工具——Tree 命令,文件目录列表全部内容,希望文章能够帮你解决Linux 实用工具——Tree 命令,文件目录列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)