Linux 基础命令 -- tree

命令介绍

命令:tree

用法: tree [-acdfghilnpqrstuvxACDFQNSUX] [-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[=]] tree 目录

命令选项

[root@fp-21 opt]# tree --help

  ------- Listing options -------		# 列表选项
  -a            # 列出所有文件
  -d            # 仅列出目录
  -f            # 打印每个文件的完整路径前缀
  -x            # 只保留当前文件系统
  -l		   # 用于显示除被通配符匹配外的所有目录和文件
  -L level      # 递归显示的深度
  -R            # 递归地遍历树的每一级目录,并在每一级目录处再次执行树
  -P pattern    # 用于显示通配符匹配模式的目录和文件
  -I pattern    # 不显示通配符匹配模式的目录和文件
  -o filename   # 将内容输出到文件
  --du          # 打印目录大小
  --prune       # 跳过空目录输出
  
  -------- File options ---------		# 文件选项
  -q            # 将不可打印的字符打印为"?"
  -N            # 按原样打印不可打印字符
  -Q            # 用双引号引用文件名
  -p            # 打印文件权限
  -u            # 打印文件所有者或UID
  -g            # 打印文件所属组或GID
  -s            # 打印每个文件的大小
  -h            # 以单位打印文件大小
  --si          # 以字节形式文件大小
  -D            # 打印最近一次修改的时间
  -F            # 根据不同类型文件在文件名后追加不同符号
  --inodes      # 打印每个文件的索引节点号
  --device      # 打印每个文件的所属ID号
  
  ------- Sorting options -------		# 排序选项
  -v            # 按版本对文件进行数字字母排序
  -r            # 按倒序数字字母排序
  -t            # 按上次修改时间对文件进行排序
  -c            # 按上次状态更改时间对文件排序
  -U            # 不排序文件
  --dirsfirst   # 在文件前列出目录
  
  ------- Graphics options ------		# 图形选项
  -i            # 不缩进显示
  -A            # 使用 ANSI 打印线图形锁进图
  -S            # 使用 ASCII 打印图形缩进图
  -n            # 禁用文件高亮显示
  -C            # 启用文件高亮显示
  
  ------- XML/HTML options -------		# XML / HTML 选项
  -X            #  XML 的形式打印树
  -H baseHREF   # 打印出HTML格式,并将baseHREF作为顶部目录
  -T string     # 用字符串替换默认的HTML标题和H1标题 
  --nolinks     # 关闭HTML输出中的超链接
  
  ---- Miscellaneous options ----		# 其它选项
  --version     # 版本信息
  --help        # 帮助文档

命令实例

# 列出所有文件
[root@fp-21 opt]# tree /opt/a
/opt/a
└── a

# 仅列出目录
[root@fp-21 tmp]# mkdir test_dir
[root@fp-21 tmp]# touch tets_file
[root@fp-21 tmp]# tree -d \/tmp/
/tmp/
├── test_dir
└── vmware-root_6811-3879048823
    
# 打印每个文件的完整路径前缀
[root@fp-21 tmp]# tree -f /tmp/
/tmp
├── /tmp/a.txt
├── /tmp/test_dir
├── /tmp/tets_file
└── /tmp/vmware-root_6811-3879048823

# 递归显示的深度
[root@fp-21 tmp]# mkdir test_dir/1
[root@fp-21 tmp]# mkdir test_dir/1/2
[root@fp-21 tmp]# mkdir test_dir/1/2/3
[root@fp-21 tmp]# tree -L 2
.
├── a.txt
├── test_dir
│   └── 1
├── tets_file
└── vmware-root_6811-3879048823

# 递归地遍历树的每一级目录,并在每一级目录处再次执行树
[root@fp-21 tmp]# tree -R /tmp/
/tmp/
├── a.txt
├── test_dir
│   └── 1
│       └── 2
│           └── 3
├── tets_file
└── vmware-root_6811-3879048823

# 不显示通配符匹配模式的目录和文件
[root@fp-21 tmp]# tree -I "a.*"
.
├── test_dir
│   └── 1
│       └── 2
│           └── 3
├── tets_file
└── vmware-root_6811-3879048823

# 将内容输出到文件
[root@fp-21 tmp]# tree -o /tmp/show_tree
[root@fp-21 tmp]# cat !$
cat /tmp/show_tree
.
├── a.txt
├── show_tree
├── test_dir
│   └── 1
│       └── 2
│           └── 3
├── tets_file
└── vmware-root_6811-3879048823

# 打印目录大小
[root@fp-21 tmp]# tree -ds
.
├── [         15]  test_dir
│   └── [         15]  1
│       └── [         15]  2
│           └── [          6]  3
└── [          6]  vmware-root_6811-3879048823

# 跳过空目录输出
[root@fp-21 tmp]# tree --prune -R /tmp/
/tmp/
├── a.txt
├── show_tree
└── tets_file
  
# 用双引号引用文件名
[root@fp-21 tmp]# tree -Q /tmp/
"/tmp/"
├── "a.txt"
├── "show_tree"
├── "test_dir"
│   └── "1"
│       └── "2"
│           └── "3"
├── "tets_file"
└── "vmware-root_6811-3879048823"

# 打印文件权限
[root@fp-21 tmp]# tree -p /tmp/
/tmp/
├── [-rw-r--r--]  a.txt
├── [-rw-r--r--]  show_tree
├── [drwxr-xr-x]  test_dir
│   └── [drwxr-xr-x]  1
│       └── [drwxr-xr-x]  2
│           └── [drwxr-xr-x]  3
├── [-rw-r--r--]  tets_file
└── [drwx------]  vmware-root_6811-3879048823

# 打印文件所有者或UID
[root@fp-21 tmp]# chown -R tom:jack /tmp
[root@fp-21 tmp]# tree -u /tmp/
/tmp/
├── [tom     ]  a.txt
├── [tom     ]  show_tree
├── [tom     ]  test_dir
│   └── [tom     ]  1
│       └── [tom     ]  2
│           └── [tom     ]  3
├── [tom     ]  tets_file
└── [tom     ]  vmware-root_6811-3879048823

# 打印文件所属组或GID
[root@fp-21 tmp]# chown -R tom:jack /tmp
[root@fp-21 tmp]# tree -g /tmp/
/tmp/
├── [jack    ]  a.txt
├── [jack    ]  show_tree
├── [jack    ]  test_dir
│   └── [jack    ]  1
│       └── [jack    ]  2
│           └── [jack    ]  3
├── [jack    ]  tets_file
└── [jack    ]  vmware-root_6811-3879048823

# 打印每个文件的大小
[root@fp-21 tmp]# tree -s /tmp/
/tmp/
├── [        385]  a.txt
├── [        211]  show_tree
├── [         15]  test_dir
│   └── [         15]  1
│       └── [         15]  2
│           └── [          6]  3
├── [          0]  tets_file
└── [          6]  vmware-root_6811-3879048823

# 以单位打印文件大小
[root@fp-21 tmp]# tree -h /tmp/
/tmp/
├── [ 385]  a.txt
├── [ 211]  show_tree
├── [  15]  test_dir
│   └── [  15]  1
│       └── [  15]  2
│           └── [   6]  3
├── [   0]  tets_file
└── [   6]  vmware-root_6811-3879048823

# 打印最近一次修改的时间
[root@fp-21 tmp]# tree -D /tmp/
/tmp/
├── [Feb 21 15:29]  a.txt
├── [Feb 21 16:26]  show_tree
├── [Feb 21 16:22]  test_dir
│   └── [Feb 21 16:22]  1
│       └── [Feb 21 16:22]  2
│           └── [Feb 21 16:22]  3
├── [Feb 21 16:19]  tets_file
└── [Feb 20 21:54]  vmware-root_6811-3879048823

# 根据不同类型文件在文件名后追加不同符号
[root@fp-21 tmp]# tree -F /tmp/
/tmp/
├── a.txt
├── show_tree
├── test_dir/
│   └── 1/
│       └── 2/
│           └── 3/
├── tets_file
└── vmware-root_6811-3879048823/

# 打印每个文件的索引节点号
[root@fp-21 tmp]# tree --inodes /tmp/
/tmp/
├── [16810042]  a.txt
├── [16811073]  show_tree
├── [33611056]  test_dir
│   └── [51211767]  1
│       └── [  13364]  2
│           └── [17380161]  3
├── [16810043]  tets_file
└── [16777289]  vmware-root_6811-3879048823

# 打印每个文件的所属ID号
[root@fp-21 tmp]# tree --device /tmp/
/tmp/
├── [64768]  a.txt
├── [64768]  show_tree
├── [64768]  test_dir
│   └── [64768]  1
│       └── [64768]  2
│           └── [64768]  3
├── [64768]  tets_file
└── [64768]  vmware-root_6811-3879048823
  
# 按版本对文件进行数字字母排序
[root@fp-21 tmp]# tree -v /tmp/
/tmp/
├── a.txt
├── show_tree
├── test_dir
│   └── 1
│       └── 2
│           └── 3
├── tets_file
└── vmware-root_6811-3879048823

# 按倒序数字字母排序
[root@fp-21 tmp]# tree -r /tmp/
/tmp/
├── vmware-root_6811-3879048823
├── tets_file
├── test_dir
│   └── 1
│       └── 2
│           └── 3
├── show_tree
└── a.txt

# 在文件前列出目录
[root@fp-21 tmp]# tree -dirsfirst /tmp/
/tmp
[         15]  /tmp/test_dir
[         15]  /tmp/test_dir/1
[         15]  /tmp/test_dir/1/2
[          6]  /tmp/test_dir/1/2/3
[          6]  /tmp/vmware-root_6811-3879048823
  
 # 不缩进显示
 [root@fp-21 tmp]# tree -i /tmp/
/tmp/
a.txt
show_tree
test_dir
1
2
3
tets_file
vmware-root_6811-3879048823

# 使用 ASCII 打印图形缩进图
[root@fp-21 tmp]# tree -S /tmp/
/tmp/
Ą a.txt
Ą show_tree
Ą test_dir
³   D 1
³       D 2
³           D 3
Ą tets_file
D vmware-root_6811-3879048823

# 以 XML 的形式打印树
[root@fp-21 tmp]# tree -X /tmp/
<?xml version="1.0" encoding="UTF-8"?>
<tree>
  <directory name="/tmp/">
    <file name="a.txt"></file>
    <file name="show_tree"></file>
    <directory name="test_dir">
      <directory name="1">
        <directory name="2">
          <directory name="3">
          </directory>
        </directory>
      </directory>
    </directory>
    <file name="tets_file"></file>
    <directory name="vmware-root_6811-3879048823">
    </directory>
  </directory>
  <report>
    <directories>5</directories>
    <files>3</files>
  </report>
</tree>

link 查看 Linux 基础命令

只有注入思想的博客才是好的博客

发布了33 篇原创文章 · 获赞 145 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/xtlhxl/article/details/104429481