Linux-du(disk useage)命令

 du 命令用于查看文件和目录占用的磁盘空间。
du [选项] [文件或目录]
-  -h (human-readable): 
 将输出格式转为人类可读的形式,使用 KB、MB 等单位。du -h /path/to/directory1.5M /path/to/directory/subdir1 2.3M /path/to/directory/subdir2 4.8M /path/to/directory
-  -s (summarize): 
 仅显示指定目录或文件的总大小,而不是列出所有子目录。du -sh /path/to/directory4.8M /path/to/directory
-  -a (all): 
 显示所有文件和目录的大小,不仅仅是目录。du -ah /path/to/directory1.0K /path/to/directory/file1.txt 1.5M /path/to/directory/subdir1 2.3M /path/to/directory/subdir2 4.8M /path/to/directory
-  -c (total): 
 显示所有列出的文件和目录的总计。du -ch /path/to/directory/*1.0K /path/to/directory/file1.txt 1.5M /path/to/directory/subdir1 2.3M /path/to/directory/subdir2 4.8M total
-  –max-depth=N: 
 限制输出的目录深度,N 是层级数。du -h --max-depth=1 /path/to/directory只显示该目录下的一级子目录的大小: 1.5M /path/to/directory/subdir1 2.3M /path/to/directory/subdir2 4.8M /path/to/directory
博主在生产环境用的比较多的情况(供参考):
1. 检查 /yushifu 目录的磁盘使用情况
 
$ du -sh /yushifu
1.5G    /yushifu
总共占用了 1.5 GB 的空间。
2. 查看当前目录下所有文件的大小
$ du -ah .
4.0K    ./file1.txt
1.1M    ./file2.log
256K    ./folder1
2.5G    ./folder2
1.5G    ./folder2/file3.dat
1.6G    .
列出当前目录下每个文件和子目录的大小,最后一行是当前目录的总大小(1.6 GB)。
3. 查找最大的目录
$ du -h --max-depth=1 /path/to/directory | sort -hr
2.5G    ./folder2
1.1M    ./file2.log
4.0K    ./file1.txt
256K    ./folder1
列出 /path/to/directory 下的所有子目录和文件的大小,并按大小排序,folder2 是占用空间最大的目录(2.5 GB)。



















