Linux 實用工具——Tree 命令,文件目錄列表

簡介

瀏覽他人技術博客的時候,會看到用文本列出漂亮的文件夾目錄,實際大部分都是使用了Linux下的Tree命令。如下簡單介紹下Tree命令的格式和例子。node

安裝

通常Linux系統是不自帶Tree命令工具的,能夠經過如下命令獲取和安裝:shell

sudo apt-get install tree

格式

經過上述命令獲取後,能夠經過一下命令顯示使用方法:ide

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 、ui

舉例

雖然如今功能愈來愈強大,可是對於大部分人來講經常使用的並很少,如下舉幾個經常使用的例子:this

顯示所有層級的目錄和文件

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命令在平時使用中能夠幫助咱們快速瞭解當前的目錄/文件結構,也能夠輔助寫處更漂亮的博客。code

相關文章
相關標籤/搜索