du 查看文件夹大小¶
在 Linux 中使用 ls -l 命令列出的目录内容中,文件夹的大小仅显示 4KB,这是用来存储文件夹的元数据的大小。
使用 du 命令可以查看目录或文件的大小。默认使用递归方式总结磁盘使用情况,以获取目录及其子目录的大小。
命令格式:¶
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
常用选项:¶
-h, --human-readable
以K,M,G为单位,显示文件的大小
-s, --summarize
只显示总计的文件大小
-S, --separate-dirs
显示时并不含其子文件夹的大小
-d, --max-depth=N
显示子文件夹的深度(层级)
使用实例:¶
[Linux]$ ls
Chapter01 Chapter03 Chapter05 Chapter07 index.rst TOC
Chapter02 Chapter04 Chapter06 Images preface.rst
# 默认显示
[Linux]$ du
1740 ./Images
184 ./Chapter01
16 ./Chapter05
32 ./Chapter03
52 ./Chapter06
32 ./TOC
12 ./Chapter07/shell 内部命令
32 ./Chapter07/images
96 ./Chapter07
92 ./Chapter04
108 ./Chapter02
2364 .
# 以人类可读的方式显示
[Linux]$ du -h
1.7M ./Images
192K ./Chapter01
16K ./Chapter05
32K ./Chapter03
52K ./Chapter06
32K ./TOC
12K ./Chapter07/shell 内部命令
32K ./Chapter07/images
96K ./Chapter07
92K ./Chapter04
108K ./Chapter02
2.4M .
# 按大小排列
[Linux]$ du -h | sort -nr
# 只显示 Images 文件夹的大小
[Linux]$ du -h Images/
1.7M Images/
# 只显示当前文件夹的总大小
[Linux]$ du -hs
2.4M .
# 显示文件夹大小时不包含子文件夹的大小
[Linux]$ du -hS
1.7M ./Images
196K ./Chapter01
16K ./Chapter05
32K ./Chapter03
52K ./Chapter06
32K ./TOC
12K ./Chapter07/shell 内部命令
32K ./Chapter07/images
52K ./Chapter07
92K ./Chapter04
108K ./Chapter02
12K .
# 显示 2 级文件夹的深度
[Linux]$ du -h -d 2 ../docs/
1.7M ../docs/Images
200K ../docs/Chapter01
16K ../docs/Chapter05
32K ../docs/Chapter03
52K ../docs/Chapter06
32K ../docs/TOC
12K ../docs/Chapter07/shell.type.rst
32K ../docs/Chapter07/images
96K ../docs/Chapter07
92K ../docs/Chapter04
108K ../docs/Chapter02
2.4M ../docs/
# 文件夹的深度从 0 级开始
[Linux]$ du -h --max-depth=0 ../docs/
2.4M ../docs/
# 列出当前目录及子目录下所有的 .jpg 文件
[Linux]$ du -ch */*.jpg
56K Images/console.01.jpg
44K Images/console.02.jpg
52K Images/console.03.jpg
48K Images/console.04.jpg
40K Images/console.05.jpg
28K Images/git.01.jpg
32K Images/locale.01.jpg
24K Images/locale.02.jpg
324K total