linux系统中du命令用于统计文件和目录所占用的磁盘空间的大小
1、创建测试数据
[root@linuxprobe test]# dd if=/dev/zero bs=1024 count=10240 of=a.txt ## 创建一个大小为10MB的测试文件
10240+0 records in
10240+0 records out
10485760 bytes (10 MB) copied, 0.0103431 s, 1.0 GB/s
[root@linuxprobe test]# ll -h
total 10M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
[root@linuxprobe test]# cat a.txt a.txt > b.txt ## 创建大小为20MB的测试文件
[root@linuxprobe test]# mkdir test01 ## 创建测试目录
[root@linuxprobe test]# cp b.txt test01/ ## 放入一个20MB的文件
[root@linuxprobe test]# ll -h
total 30M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
-rw-r--r--. 1 root root 20M Oct 23 23:34 b.txt
drwxr-xr-x. 2 root root 18 Oct 23 23:34 test01
[root@linuxprobe test]# tree ## 查看目录结构
.
├── a.txt
├── b.txt
└── test01
└── b.txt
1 directory, 3 files
2、直接测试
3、加 -h 参数,以人类友好的方式显示
[root@linuxprobe test]# du -h
20M ./test01
50M .
4、-a参数
[root@linuxprobe test]# du -a ## 加-a参数,显示了当前目录小所有文件及目录各自的大小和当前目录总的大小,可以理解为all
10240 ./a.txt
20480 ./b.txt
20480 ./test01/b.txt
20480 ./test01
51200 .
[root@linuxprobe test]# du -ah
10M ./a.txt
20M ./b.txt
20M ./test01/b.txt
20M ./test01
50M .
5、加 -s参数
[root@linuxprobe test]# du -s ## 当前目录总的大小,可理解为sum
51200 .
[root@linuxprobe test]# du -sh
50M .
6、du后面可以直接加文件和目录
[root@linuxprobe test]# du b.txt
20480 b.txt
[root@linuxprobe test]# du -h b.txt
20M b.txt
[root@linuxprobe test]# du test01/
20480 test01/
[root@linuxprobe test]# du -h test01/
20M test01/
7、-c选项
[root@linuxprobe test]# cat a.txt b.txt > c.txt
[root@linuxprobe test]# ll -h
total 60M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
-rw-r--r--. 1 root root 20M Oct 23 23:34 b.txt
-rw-r--r--. 1 root root 30M Oct 23 23:45 c.txt
drwxr-xr-x. 2 root root 18 Oct 23 23:34 test01
[root@linuxprobe test]# du -c a.txt c.txt ## -c选项可用于计算几个文件大小的和
10240 a.txt
30720 c.txt
40960 total
[root@linuxprobe test]# du -ch a.txt c.txt
10M a.txt
30M c.txt
40M total
[root@linuxprobe test]# ll -h
total 60M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
-rw-r--r--. 1 root root 20M Oct 23 23:34 b.txt
-rw-r--r--. 1 root root 30M Oct 23 23:45 c.txt
drwxr-xr-x. 2 root root 18 Oct 23 23:34 test01
[root@linuxprobe test]# ln b.txt b.hard ## 创建b.txt文件的硬链接
[root@linuxprobe test]# ll -h
total 80M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
-rw-r--r--. 2 root root 20M Oct 23 23:34 b.hard
-rw-r--r--. 2 root root 20M Oct 23 23:34 b.txt
-rw-r--r--. 1 root root 30M Oct 23 23:45 c.txt
drwxr-xr-x. 2 root root 18 Oct 23 23:34 test01
[root@linuxprobe test]# du -h ## 系统检测到硬链接,直接忽略掉
20M ./test01
80M .
[root@linuxprobe test]# du -lh ## 加 -l选项,统计硬链接文件大小
20M ./test01
100M .
[root@linuxprobe test]# ll -h
total 80M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
-rw-r--r--. 2 root root 20M Oct 23 23:34 b.hard
-rw-r--r--. 2 root root 20M Oct 23 23:34 b.txt
-rw-r--r--. 1 root root 30M Oct 23 23:45 c.txt
drwxr-xr-x. 2 root root 18 Oct 23 23:34 test01
[root@linuxprobe test]# ln -s c.txt c.link ## 创建c.txt的软链接
[root@linuxprobe test]# ll -h
total 80M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
-rw-r--r--. 2 root root 20M Oct 23 23:34 b.hard
-rw-r--r--. 2 root root 20M Oct 23 23:34 b.txt
lrwxrwxrwx. 1 root root 5 Oct 23 23:52 c.link -> c.txt
-rw-r--r--. 1 root root 30M Oct 23 23:45 c.txt
drwxr-xr-x. 2 root root 18 Oct 23 23:34 test01
[root@linuxprobe test]# du -h c.link ## 直接统计大小为0
0 c.link
[root@linuxprobe test]# du -L c.link ## 加-L选项,可以统计大小
30720 c.link
[root@linuxprobe test]# du -Lh c.link
30M c.link
10、指定输出单位
[root@linuxprobe test]# du -b
20971538 ./test01
83886184 .
[root@linuxprobe test]# du -k
20480 ./test01
81920 .
[root@linuxprobe test]# du -m
20 ./test01
80 .
[root@linuxprobe test]# ll -h
total 60M
-rw-r--r--. 1 root root 10M Oct 23 23:34 a.txt
-rw-r--r--. 1 root root 20M Oct 23 23:34 b.txt
-rw-r--r--. 1 root root 30M Oct 23 23:59 c.txt
[root@linuxprobe test]# du -sh
60M .
[root@linuxprobe test]# du -sh --exclude=c.txt
30M .
[root@linuxprobe test]# du -sh --exclude=a.txt
50M .
[root@linuxprobe test]# du -sh --exclude=b.txt
40M .
12、文件大小排序
[root@linuxprobe test]# du -sh *
10M a.txt
20M b.txt
30M c.txt
60M test01
[root@linuxprobe test]# du -sh * | sort -r
60M test01
30M c.txt
20M b.txt
10M a.txt
[root@linuxprobe test]# du -sk * | sort -r
61440 test01
30720 c.txt
20480 b.txt
10240 a.txt
[root@linuxprobe test]# du -sm * | sort -r
60 test01
30 c.txt
20 b.txt
10 a.txt
[root@linuxprobe test]# du -sm * | sort
10 a.txt
20 b.txt
30 c.txt
60 test01
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。