Linux 实用工具——Tree 命令,文件目录列表

简介

浏览他人技术博客的时候,会看到用文本列出漂亮的文件夹目录,实际大部分都是使用了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.txt
demo1
CMakeCache.txt
CMakeFiles
cmake_install.cmake
CMakeLists.txt
Demo
main.cc
Makefile
demo2

显示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命令在平时使用中可以帮助我们快速了解当前的目录/文件结构,也可以辅助写处更漂亮的博客。

上一篇:java从头开始--包和访问权限修饰符


下一篇:CentOS 6.4编译安装和部署Zabbix 2.0版本监控(中文)