linux(centos)du命令查看文件或目录的大小

linux(centos)du命令查看文件或目录的大小,第1张

linux(centos)du命令查看文件或目录大小

有时磁盘空间不足时,需要知道那个目录占用了大部分的磁盘空间,此时使用du命令可以方便地查看各个目录所占磁盘空间大小,以便及时清理不需要的数据。

du命令作用是估计文件系统的磁盘已使用量,常用于查看文件或目录所占磁盘容量。
du命令与df命令不同,df命令是统计磁盘使用情况。
du命令会直接到文件系统内查找所有文件数据,所以命令执行时会耗费一点儿时间。
在默认情况下,输出结果大小是以KB为单位的。如果想以MB为单位,使用-m参数即可,如果只想知道目录占了多少容量,使用-s参数即可。

示例1:列出目录下所有文件和目录的容量大小
[root@localhost test]# du  #默认情况下,只统计目录的容量大小。
8       ./dir2
12      ./dir1/dir1-dira
876     ./dir1
1092    .
[root@localhost test]# du -a   #统计目录和文件的容量大小。
100     ./file2
8       ./dir2
100     ./file1

示例2:统计各文件的大小,并显示总大小
[root@localhost test]# du  /home/test/    # 默认,不显示总大小
8       /home/test/dir2
12      /home/test/dir1/dir1-dira
876     /home/test/dir1
1092    /home/test/
[root@localhost test]# du -c /home/test/   #最下面显示总大小total
8       /home/test/dir2
12      /home/test/dir1/dir1-dira
876     /home/test/dir1
1092    /home/test/
1092    total
示例3:以易读的方式显示容量大小
[root@localhost test]# du -h /home/test
8.0K    /home/test/dir2
12K     /home/test/dir1/dir1-dira
876K    /home/test/dir1
1.1M    /home/test
示例4:仅显示目录的总大小
[root@localhost test]# du -s /home   #通过-s参数只统计总量
3208    /home

[root@localhost test]# du --max-depth=0 /home  #通过指定递归深度方式
3208    /home
示例5:显示指定目录下每个文件或目录的容量大小
[root@localhost test]# du -s /*   #使用-s参数
8320    /bin
6659    /boot
152     /dev
170328  /etc
3208    /home

[root@localhost test]# du --max-depth=1 /   #使用指定递归深度方式
436     /tmp
142868  /lib
0       /net
16      /opt
6659    /boot
0       /sys
8       /srv
2966147 /
示例6:显示指定目录下每个文件或目录的容量大小,并且以易读方式显示(常用)。
[root@localhost test]# du -sh /*
8.2M    /bin
6.6M    /boot
152K    /dev
167M    /etc

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5699789.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存