tree 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

tree 命令安装:


-bash/zsh: tree command not found

#Debian
apt-get install tree

#Ubuntu
apt-get install tree

#Alpine
apk add tree

#Arch Linux
pacman -S tree

#Kali Linux
apt-get install tree

#CentOS
yum install tree

#Fedora
dnf install tree

#OS X
brew install tree

#Raspbian
apt-get install tree

#Docker
docker run cmd.cat/tree tree

tree 命令补充说明:


tree 命令,是一个递归目录列表程序,它生成一个深度缩进的文件列表(如果设置了 LS_COLORS 环境变量,它会被着色)并且输出到 tty。 没有参数,树列出当前目录中的文件。 当给出目录参数时,树依次列出在给定目录中找到的所有文件和 / 或目录。 tree 然后返回列出的文件和 / 或目录的总数。

默认情况下,当遇到符号链接时,符号链接所指的路径以格式打印在链接名称之后:

name -> real-path

tree 命令语法:


tree [-adfgilnopqrstuxACDFNS] [-L level [-R]] [-H baseHREF] [-T title] 
     [-o file name] [--nolinks] [-P pattern] [-I pattern] [--inodes] 
     [--device] [--noreport] [--dirsfirst] [--version] [--help] 
     [--filelimit #] [--si] [--prune] [--du] [--timefmt format] 
     [directory ...]

tree 命令选项:


--help         Outputs a verbose usage listing.
--version      Outputs the version of tree.
-a             All files are printed. By default, tree does not print hidden files (those beginning with a dot `.'). In no event does tree print the file system constructs `.' (current directory) and `..' (previous directory).
-d             List directories only.
-f             Prints the full path prefix for each file.
-i             Tree does not print the indentation lines. Useful when used in conjunction with the -f option.
-l             Follows symbolic links to directories as if they were directories. Links that would result in a recursive loop are avoided.
-x             Stay on the current file system only, as with find -xdev.

-P pattern     List only those files that match the wildcard pattern. Note: you must use the -a option to also consider those files beginning with a dot `.' for matching. Valid wildcard operators are `*' (any zero or more characters), `?' (any single character), `[...]' (any single character listed between brackets (optional - (dash) for character range may be used: ex: [A-Z]), and `[^...]' (any single character not listed in brackets) and `|' separates alternate patterns.

-I pattern     Do not list those files that match the wildcard pattern.

--prune           Makes tree prune empty directories from the output, useful when used in conjunction with -P or -I.
--filelimit #     Do not descend directories that contain more than # entries.
--timefmt format  Prints (implies -D) and formats the date according to the format string which uses the strftime syntax.
--noreport        Omits printing of the file and directory report at the end of the tree listing.

-p             Print the protections for each file (as per ls -l).
-s             Print the size of each file with the name.
-u             Print the username, or UID # if no username is available, of the file.
-g             Print the group name, or GID # if no group name is available, of the file.
-D             Print the date of the last modification time for the file listed.
--inodes       Prints the inode number of the file or directory
--device       Prints the device number to which the file or directory belongs
-F             Append a `/' for directories, a `=' for socket files, a `*' for executable files and a `|' for FIFO's, as per ls -F
-q             Print non-printable characters in file names as question marks instead of the default carrot notation.
-N             Print non-printable characters as is instead of the default carrot notation.
-r             Sort the output in reverse alphabetic order.
-t             Sort the output by last modification time instead of alphabetically.
--dirsfirst    List directories before files.
-n             Turn colorization off always, overridden by the -C option.
-C             Turn colorization on always, using built-in color defaults if the LS_COLORS environment variable is not set. Useful to colorize output to a pipe.
-A             Turn on ANSI line graphics hack when printing the indentation lines.
-S             Turn on ASCII line graphics (useful when using linux console mode fonts). This option is now equivalent to `--charset=IBM437' and eventually is depreciated.
-L level       Max display depth of the directory tree.
-R             Recursively cross down the tree each level directories (see -L option), and at each of them execute tree again adding `-o 00Tree.html' as a new option.
-H baseHREF    Turn on HTML output, including HTTP references. Useful for ftp sites. baseHREF gives the base ftp location when using HTML output. That is, the local directory may be `/local/ftp/pub', but it must be referenced as `ftp://host-name.organization.domain/pub' (baseHREF should be `ftp://hostname.organization.domain'). Hint: don't use ANSI lines with this option, and don't give more than one directory in the directory list. If you want to use colors via CSS stylesheet, use the -C option in addition to this option to force color output.

-T title             Sets the title and H1 header string in HTML output mode.
--charset charset    Set the character set to use when outputting HTML and for line drawing.
--nolinks            Turns off hyperlinks in HTML output.
-o file name         Send output to file name.

tree 命令实例:


tree 显示高达 num 级深度的文件和目录(其中 1 表示当前目录):

tree -L num
--------------------
$ tree -L 2
.
├── conf
│   └── nutcracker.yml
├── sbin
│   └── nutcracker
├── share
│   └── man
└── twemproxy.log

4 directories, 3 files

tree 仅显示目录:

tree -d
--------------------
$ tree -d
.
├── conf
├── sbin
└── share
    └── man
        └── man8

5 directories

tree 也显示隐藏文件:

tree -a
--------------------
$ tree -a
.
├── conf
│   └── nutcracker.yml
├── sbin
│   └── nutcracker
├── share
│   └── man
│       └── man8
│           └── nutcracker.8
└── twemproxy.log

5 directories, 4 files

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

tree -i -f
--------------------
$ tree -i -f
.
./conf
./conf/nutcracker.yml
./sbin
./sbin/nutcracker
./share
./share/man
./share/man/man8
./share/man/man8/nutcracker.8
./twemproxy.log

5 directories, 4 files

tree 以人类可读的格式打印旁边每个节点的大小:

tree -s -h
--------------------
$ tree -s -h
.
├── [  28]  conf
│   └── [ 471]  nutcracker.yml
├── [  24]  sbin
│   └── [885K]  nutcracker
├── [  17]  share
│   └── [  18]  man
│       └── [  26]  man8
│           └── [2.1K]  nutcracker.8
└── [   0]  twemproxy.log

5 directories, 4 files

tree 使用通配符 (glob) 模式过滤树:

tree -P *.txt
--------------------
$ tree -P *.yml
.
├── conf
│   └── nutcracker.yml
├── sbin
└── share
    └── man
        └── man8

5 directories, 1 file

tree 忽略与通配符 (glob) 模式匹配的条目:

tree -I *.txt
--------------------
$ tree -I *.yml
.
├── conf
├── sbin
│   └── nutcracker
├── share
│   └── man
│       └── man8
│           └── nutcracker.8
└── twemproxy.log

5 directories, 3 files

tree 扩展阅读:




tree 命令评论

共收录到 489Linux 命令