linux命令tree的使用

时间:2022-07-22
本文章向大家介绍linux命令tree的使用,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

有时候我们新建完项目,想查看一下项目的目录结构,此时我们就可以使用tree命令了,但是mac电脑没有自带tree命令,我们需要安装tree。

我们可以使用brew工具进行安装,安装命令如下:

brew install tree

安装完成之后,我们可以运行 help指令查看tree有哪些指令:

tree --help

打印如下:

usage: tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]
        [-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
        [--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
        [--filelimit #] [<directory list>]
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -i            Don't print indentation lines.
  -q            Print non-printable characters as '?'.
  -N            Print non-printable characters as is.
  -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.
  -D            Print the date of last modification.
  -F            Appends '/', '=', '*', or '|' as per ls -F.
  -v            Sort files alphanumerically by version.
  -r            Sort files in reverse alphanumeric order.
  -t            Sort files by last modification time.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  -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.
  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
  -H baseHREF   Prints out HTML format with baseHREF as top directory.
  -T string     Replace the default HTML title and H1 header with string.
  -R            Rerun tree when max dir level reached.
  -o file       Output to file instead of stdout.
  --inodes      Print inode number of each file.
  --device      Print device ID number to which each file belongs.
  --noreport    Turn off file/directory count at end of tree listing.
  --nolinks     Turn off hyperlinks in HTML output.
  --dirsfirst   List directories before files.
  --charset X   Use charset X for HTML and indentation line output.
  --filelimit # Do not descend dirs with more than # files in them.

这里命令很多,这里只简单介绍下常用的几个指令:

- 显示深度达到 “级数” 级的文件和目录(其中 1 表示当前目录):
    tree -L 级数
- 只显示目录:
    tree -d
- 同时显示隐藏文件:
    tree -a
- 忽略文件或目录:
    tree -I 文件名称/目录名称

此时我们还可以借助另外一个命令行工具tldr来显示tree命令工具的常用命令,安装tldr有多种方式,这里我们采用npm来安装,

 npm install tldr -g

之后运行:

tldr tree 

打印如下:

tree

  以树的形式显示当前目录的内容.

  - 显示深度达到 “级数” 级的文件和目录(其中 1 表示当前目录):
    tree -L 级数

  - 只显示目录:
    tree -d

  - 同时显示隐藏文件:
    tree -a

  - 打印没有缩进行的树,显示完整路径(使用-N不转义空格和特殊字符):
    tree -i -f

  - 以可读格式打印每个文件节点的大小,目录显示其累积大小(类似在du命令中所示):
    tree -s -h --du

  - 使用通配符(glob)模式在树层次结构中查找文件,并删除不包含匹配文件的目录:
    tree -P '*.txt' --prune

  - 在树层次结构中查找目录,删除不属于所需目录的目录:
    tree -P 文件夹名 --matchdirs --prune


See also: du

以上便是tree命令的使用,希望对你有所帮助。