说明
du命令用于统计磁盘空间使用情况,这个命令可以让我们找出哪个文件过多的占用了磁盘空间
格式
du [option] [file]
常用参数说明
- -a:显示所有文件大小
- -h:以人类刻度的方式查看大小,以K、M、G为单位
- -s:显示文件的总大小
- —exclude=文件或目录:忽略指定的文件或目录
- -X,—exclude-from=FILE:从文件读取需要忽略的目录或文件
- —max-depth=N:显示N级子目录的大小
示例
1、-a参数使用,显示当前目录下的所有文件大小,包括隐藏文件
[root@localhost test1]# du -a
0 ./data1
0 ./data2
4 ./test.txt
4 .
2、-s参数的使用
[root@localhost test1]# du -s
4 .
3、-h参数使用
[root@localhost test1]# du -h
0 ./data1
0 ./data2
4.0K .
[root@localhost test1]# du -sh #结合s选项
4.0K .
[root@localhost test1]# du -ah #结合a选项
0 ./data1
0 ./data2
4.0K ./test.txt
4.0K .
4、显示指定层级的目录大小
[root@localhost test1]# du -h --max-depth=1 /usr/local/ #只显示第一层的大小
0 /usr/local/bin
0 /usr/local/etc
0 /usr/local/games
0 /usr/local/include
0 /usr/local/lib
0 /usr/local/lib64
0 /usr/local/libexec
0 /usr/local/sbin
4.0K /usr/local/share
0 /usr/local/src
8.0K /usr/local/
[root@localhost test1]# du -h --max-depth=2 /usr/local/ #只显示第一二层的大小
0 /usr/local/bin
0 /usr/local/etc
0 /usr/local/games
0 /usr/local/include
0 /usr/local/lib
0 /usr/local/lib64
0 /usr/local/libexec
0 /usr/local/sbin
0 /usr/local/share/applications
0 /usr/local/share/info
4.0K /usr/local/share/man
4.0K /usr/local/share
0 /usr/local/src
8.0K /usr/local/
5、排除指定的目录
[root@localhost test1]# du -h --max-depth=2 /usr/local/ --exclude=/usr/local/include
0 /usr/local/bin
0 /usr/local/etc
0 /usr/local/games
0 /usr/local/lib
0 /usr/local/lib64
0 /usr/local/libexec
0 /usr/local/sbin
0 /usr/local/share/applications
0 /usr/local/share/info
4.0K /usr/local/share/man
4.0K /usr/local/share
0 /usr/local/src
8.0K /usr/local/
6、根据指定文件的内容来排除
[root@localhost test1]# cat file.txt
/usr/local/etc
/usr/local/games
/usr/local/lib
/usr/local/lib64
/usr/local/libexec
[root@localhost test1]# du -h --max-depth=2 /usr/local/ --exclude-from=file.txt
0 /usr/local/bin
0 /usr/local/include
0 /usr/local/sbin
0 /usr/local/share/applications
0 /usr/local/share/info
4.0K /usr/local/share/man
4.0K /usr/local/share
0 /usr/local/src
8.0K /usr/local/