目录
11. shell命令--tree
功能说明
tree 是一个在 Linux 和类 Unix 系统中常用的命令,它用于以树状图列出目录的内容,包括所有文件、子目录等。这对于快速浏览目录结构非常有用。
语法格式
tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式>][目录...]
SYNOPSIS
tree [-acdfghilnpqrstuvxACDFQNSUX] [-L level [-R]] [-H baseHREF] [-T title] [-o file‐
name] [--nolinks] [-P pattern] [-I pattern] [--inodes] [--device] [--noreport] [--dirs‐
first] [--version] [--help] [--filelimit #] [--si] [--prune] [--du] [--timefmt format]
[directory ...]
选项说明
- -a:显示所有文件和目录,包括隐藏文件(以.开头的文件)。
- -A:使用ASNI绘图字符显示树状图而非以ASCII字符组合。
- -C:在文件和目录清单加上色彩,便于区分各种类型。
- -d:只列出目录名,不显示文件。
- -D:列出文件或目录的更改时间。
- -L level:限制目录列表的级别。例如,-L 2 将只显示到第二级子目录。
- -f:在每个目录项后面附加完整的路径名。
- -F:在执行文件,目录,Socket,符号连接,管道名称名称,各自加上"*","/","=","@","|"号。
- -g:列出文件或目录的所属群组名称,没有对应的名称时,则显示群组识别码。
- -G:显示组名或 GID
- -n:不在文件和目录清单加上色彩。
- -N:直接列出文件和目录名称,包括控制字符。
- -i:不显示以.开头的文件和目录。
- -l:如遇到性质为符号连接的目录,直接列出该连接所指向的原始目录。
- -s:在每个文件或目录旁边显示其大小。
- -q:用 "?" 号取代控制字符,列出文件和目录名称。
- -h:与 -s 一起使用时,以人类可读的格式(例如 KB、MB)显示文件大小。
- -P pattern:只显示符合模式的文件和目录。例如,-P "*.txt" 将只显示 .txt 文件。
- -p:显示文件的权限属性。
- -t:用文件和目录的更改时间排序。
- -u:列出文件或目录的拥有者名称,没有对应的名称时,则显示用户识别码。
- -x:将范围局限在现行的文件系统中,若指定目录下的某些子目录,其存放于另一个文件系统上,则将该子目录予以排除在寻找范围外。
实践操作
1. 检查是否有 tree 命令,大多数 Linux 发行版都预装了 tree 命令。但如果没有,你可以使用包管理器来安装它
#sudo apt install tree #基于 Debian 的系统上,你可以使用 apt
#sudo yum install tree #基于 Red Hat 的系统上,这对于较旧的版本
#sudo dnf install tree #基于 Red Hat 的系统上,对于较新的版本
cat /etc/*release #查看 Linux 版本信息,演示的系统为 CentOS Linux release 7.9.2009 (Core)
rpm -qa tree #查询 tree 命令是否安装,显示则已经安装,若未安装,执行以下操作安装
#ping www.baidu.com #查看是否能够连接互联网,按 Ctrl + c 结束
#yum install tree -y #使用yum,进行安装 tree 命令(需要上网和配置好 yum 源),这里演示的系统已经安装过了。
#LANG=en_US.UTF-8 #临时调整系统字符集,防止树形显示乱码
2. 不加任何参数执行 tree 命令
mkdir -p /test/tree #创建测试目录
cd /test/tree #切换到测试目录
mkdir -p dir_one{1..2}/dir_two{1..2}
touch dir_one{1..2}/file{1..2}
touch dir_one{1..2}/dir_two{1..2}/{1..2}.txt
ls
tree #使用 tree 命令进行查看
3. 以树形结构显示目录下的所有内容( -a 的功能)
touch .file
ls
ls -A
tree
tree -a
4. 只列出指定目录指定层数目录的结构( -L 的功能)
tree -L 1
tree -L 2
5. 只显示所有目录,不显示文件
ls
tree dir_one1
tree -d dir_one1
ls -l dir_one1
6. 使用 tree 命令区分目录和文件的方法
tree -L 2 -F #如果是目录的话后面加 /
tree -L 2 -F |grep /$ #过滤以斜线结尾的所有内容
tree -L 2 -d #使用 -d 参数只显示目录树
7. 显示当前目录的目录结构,并显示文件大小
tree -sh
8. 显示当前目录的目录结构,并只显示 .txt 文件
tree -P "*.txt" #大写 P
命令示例:1. 检查是否有 tree 命令
cat /etc/*release
rpm -qa tree
输出结果:
[root@MineGi ~]# cat /etc/*release
CentOS Linux release 7.9.2009 (Core)
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
CentOS Linux release 7.9.2009 (Core)
CentOS Linux release 7.9.2009 (Core)
[root@MineGi ~]# rpm -qa tree
tree-1.6.0-10.el7.x86_64
[root@MineGi ~]#
命令示例:2. 不加任何参数执行 tree 命令
mkdir -p /test/tree
cd /test/tree
mkdir -p dir_one{1..2}/dir_two{1..2}
touch dir_one{1..2}/file{1..2}
touch dir_one{1..2}/dir_two{1..2}/{1..2}.txt
ls
tree
输出结果:
[root@MineGi ~]# mkdir -p /test/tree
[root@MineGi ~]# cd /test/tree
[root@MineGi /test/tree]# mkdir -p dir_one{1..2}/dir_two{1..2}
[root@MineGi /test/tree]# touch dir_one{1..2}/file{1..2}
[root@MineGi /test/tree]# touch dir_one{1..2}/dir_two{1..2}/{1..2}.txt
[root@MineGi /test/tree]# ls
dir_one1 dir_one2
[root@MineGi /test/tree]# tree
.
├── dir_one1
│ ├── dir_two1
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── dir_two2
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── file1
│ └── file2
└── dir_one2
├── dir_two1
│ ├── 1.txt
│ └── 2.txt
├── dir_two2
│ ├── 1.txt
│ └── 2.txt
├── file1
└── file2
6 directories, 12 files
[root@MineGi /test/tree]#
命令示例:3. 以树形结构显示目录下的所有内容( -a 的功能)
touch .file
ls
ls -A
tree
tree -a
输出结果:
[root@MineGi /test/tree]# touch .file
[root@MineGi /test/tree]# ls
dir_one1 dir_one2
[root@MineGi /test/tree]# ls -A
dir_one1 dir_one2 .file
[root@MineGi /test/tree]# tree
.
├── dir_one1
│ ├── dir_two1
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── dir_two2
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── file1
│ └── file2
└── dir_one2
├── dir_two1
│ ├── 1.txt
│ └── 2.txt
├── dir_two2
│ ├── 1.txt
│ └── 2.txt
├── file1
└── file2
6 directories, 12 files
[root@MineGi /test/tree]# tree -a
.
├── dir_one1
│ ├── dir_two1
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── dir_two2
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── file1
│ └── file2
├── dir_one2
│ ├── dir_two1
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── dir_two2
│ │ ├── 1.txt
│ │ └── 2.txt
│ ├── file1
│ └── file2
└── .file
6 directories, 13 files
[root@MineGi /test/tree]#
命令示例:4. 只列出指定目录指定层数目录的结构( -L 的功能)
tree -L 1
tree -L 2
输出结果:
[root@MineGi /test/tree]# tree -L 1
.
├── dir_one1
└── dir_one2
2 directories, 0 files
[root@MineGi /test/tree]# tree -L 2
.
├── dir_one1
│ ├── dir_two1
│ ├── dir_two2
│ ├── file1
│ └── file2
└── dir_one2
├── dir_two1
├── dir_two2
├── file1
└── file2
6 directories, 4 files
[root@MineGi /test/tree]#
命令示例:5. 只显示所有目录,不显示文件
ls
tree dir_one1
tree -d dir_one1
ls -l dir_one1
输出结果:
[root@MineGi /test/tree]# ls
dir_one1 dir_one2
[root@MineGi /test/tree]# tree dir_one1
dir_one1
├── dir_two1
│ ├── 1.txt
│ └── 2.txt
├── dir_two2
│ ├── 1.txt
│ └── 2.txt
├── file1
└── file2
2 directories, 6 files
[root@MineGi /test/tree]# tree -d dir_one1
dir_one1
├── dir_two1
└── dir_two2
2 directories
[root@MineGi /test/tree]# ls -l dir_one1
总用量 0
drwxr-xr-x 2 root root 32 11月 25 14:10 dir_two1
drwxr-xr-x 2 root root 32 11月 25 14:10 dir_two2
-rw-r--r-- 1 root root 0 11月 25 14:10 file1
-rw-r--r-- 1 root root 0 11月 25 14:10 file2
[root@MineGi /test/tree]#
命令示例:6. 使用 tree 命令区分目录和文件的方法
tree -L 2 -F
tree -L 2 -F |grep /$
tree -L 2 -d
输出结果:
[root@MineGi /test/tree]# tree -L 2 -F
.
├── dir_one1/
│ ├── dir_two1/
│ ├── dir_two2/
│ ├── file1
│ └── file2
└── dir_one2/
├── dir_two1/
├── dir_two2/
├── file1
└── file2
6 directories, 4 files
[root@MineGi /test/tree]# tree -L 2 -F |grep /$
├── dir_one1/
│ ├── dir_two1/
│ ├── dir_two2/
└── dir_one2/
├── dir_two1/
├── dir_two2/
[root@MineGi /test/tree]# tree -L 2 -d
.
├── dir_one1
│ ├── dir_two1
│ └── dir_two2
└── dir_one2
├── dir_two1
└── dir_two2
6 directories
[root@MineGi /test/tree]#
命令示例:7. 显示当前目录的目录结构,并显示文件大小
tree -sh
输出结果:
[root@MineGi /test/tree]# tree -sh
.
├── [ 64] dir_one1
│ ├── [ 32] dir_two1
│ │ ├── [ 0] 1.txt
│ │ └── [ 0] 2.txt
│ ├── [ 32] dir_two2
│ │ ├── [ 0] 1.txt
│ │ └── [ 0] 2.txt
│ ├── [ 0] file1
│ └── [ 0] file2
└── [ 64] dir_one2
├── [ 32] dir_two1
│ ├── [ 0] 1.txt
│ └── [ 0] 2.txt
├── [ 32] dir_two2
│ ├── [ 0] 1.txt
│ └── [ 0] 2.txt
├── [ 0] file1
└── [ 0] file2
6 directories, 12 files
[root@MineGi /test/tree]#
命令示例:8. 显示当前目录的目录结构,并只显示 .txt 文件
tree -P "*.txt"
输出结果:
[root@MineGi /test/tree]# tree -P "*.txt"
.
├── dir_one1
│ ├── dir_two1
│ │ ├── 1.txt
│ │ └── 2.txt
│ └── dir_two2
│ ├── 1.txt
│ └── 2.txt
└── dir_one2
├── dir_two1
│ ├── 1.txt
│ └── 2.txt
└── dir_two2
├── 1.txt
└── 2.txt
6 directories, 8 files
[root@MineGi /test/tree]#
注意事项
- tree 命令的输出可能会很长,特别是在大型目录结构中。你可以使用 less 或 more 命令与 tree 结合使用,以便逐页查看输出。例如:tree | less。
- tree 命令的输出格式可能因版本和发行版而异。你可以通过查看手册页(man tree)来了解你系统上 tree 命令的完整选项和详细信息。
除了基本用法和主要选项,tree 命令还有一些高级特性和用法,可以帮助用户更深入地理解和操作目录结构。
高级特性
自定义目录和文件颜色
tree 命令允许你通过环境变量来定制目录和文件的颜色输出。例如,你可以设置 LS_COLORS 环境变量来改变不同类型文件和目录的颜色,使得输出更加醒目和易于区分。
排除特定目录
如果你想在输出中排除某些特定的目录,你可以使用 --exclude 或 -I 选项。例如,tree -I 'node_modules|tmp' 会排除名为 node_modules 和 tmp 的目录。
显示目录的链接数
使用 -l 选项,tree 命令可以显示每个目录的链接数。这对于识别硬链接和查找潜在的循环引用非常有用。
显示目录的权限和所有者
通过 -p 和 -u 选项,你可以查看每个目录的权限和所有者信息。这对于系统管理员或需要了解目录权限的用户来说非常有用。
结合其他命令使用
tree 命令的输出可以很容易地与其他命令和工具结合使用,以进行更复杂的操作。
查找特定类型的文件
你可以将 tree 的输出与 grep 命令结合使用,以查找特定类型的文件。例如,tree -f | grep "\.jpg$" 会列出所有 .jpg 图片文件的完整路径。
按大小排序目录
结合 sort 命令,你可以根据目录的大小对 tree 的输出进行排序。例如,tree -sh | sort -h -r -k2 会按文件大小降序排列目录和文件。
将目录结构保存为文件
你可以将 tree 的输出重定向到一个文件,以便稍后查看或分析。例如,tree > directory_structure.txt 会将目录结构保存到 directory_structure.txt 文件中。
总结
tree 命令是一个强大的工具,可以帮助你更好地理解和浏览 Linux 和类 Unix 系统的目录结构。通过掌握其基本用法、主要选项以及高级特性和与其他命令的结合使用,你可以更有效地管理和操作文件系统。无论你是系统管理员、开发人员还是普通用户,掌握 tree 命令都将使你的工作更加高效和便捷。希望这些高级特性和用法能进一步提升你对 tree 命令的理解和使用能力!