PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

linux du是什么

青灯夜游
青灯夜游 原创
2022-04-18 19:18:20 7018浏览

linux中,du全称“disk usage”,是统计目录或文件所占磁盘空间大小的命令,语法为“du [选项] [目录或文件名]”。du命令支持多种选项:1、“-h”,能以易读的单位显示大小;2、“-s”,可显示目录总大小;3、“-d”等。

本教程操作环境:linux5.9.8系统、Dell G3电脑。

linux du命令

du 是 Disk Usage 的缩写, Linux 上最受欢迎的命令之一,du 是统计目录或文件所占磁盘空间大小的命令

du 命令的格式如下:

du [选项] [目录或文件名]

常用的选项如下:

  • -a: 显示目录中所有文件以及文件夹大小

  • -h: 以 Kb、Mb 、Gb 等易读的单位显示大小

  • --si: 类似 -h 选项,但是计算是用 1000 为基数而不是1024

  • -s: 显示目录总大小

  • -d: 是 --max-depth=N 选项的简写,表示深入到第几层目录,超过指定层数目录则忽略

  • -c: 除了显示目录大小外,额外一行显示总占用量

  • --time: 显示每一个目录下最近修改文件的时间

  • -t: 是 --threshold=SIZE 的简写,过滤掉小于 SIZE 大小的文件以及目录

  • --exclude=PATTERN:过滤与 PATTERN 匹配的文件名或者目录名

使用示例

显示所有目录及文件大小

下面的例子显示了所有目录以及目录下文件的大小,单位默认是 Kb

[root@ecs-centos-7 tt]# du -a temp/
4       temp/suba.txt
4       temp/test/abc.txt
4       temp/test/ha/ha.txt
8       temp/test/ha
16      temp/test
4       temp/time.txt
28      temp/

注意: 上面的例子如果不使用 -a 选项,默认情况下只显示目录大小,不显示文件大小。即执行du temp/ 只会显示目录大小,请看以下的例子:

[root@ecs-centos-7 tt]# du temp
8       temp/test/ha
16      temp/test
28      temp

以易读的方式显示

默认显示的大小只有一个孤零零的数字,连单位也没有,让人第一眼看上去有点疑惑,通过 -h 选项可以让大小显示成人类易读的方式,这个选项应该是最常用的了

[root@ecs-centos-7 tt]# du -b temp/
4117    temp/test/ha
8218    temp/test
12326   temp/
[root@ecs-centos-7 tt]# du -h temp/
8.0K    temp/test/ha
16K     temp/test
28K     temp/
[root@ecs-centos-7 tt]# du --si temp/
8.2k    temp/test/ha
17k     temp/test
29k     temp/

上面的例子中, -h 选项默认计算基数是 1024 , --si 选项默认计算基数是 1000
所以temp/test/ha目录以 -h 选项计算的大小是 8.0K,而以 --si 选项计算的大小是 8.2K

-h 以及 --si 选项的大小单位随着目录及文件的大小自动的调整

目录总大小

有时我们只需要知道一个目录的总大小,不需要知道子目录及子目录下文件的大小,可以通过 -s 选项获取目录总大小

[root@ecs-centos-7 tt]# du -sh .
72K     .
[root@ecs-centos-7 tt]# du -sh temp/
28K     temp/

上面的例子分别获取当前目录的总大小以及 temp/ 目录的总大小

通过 -c 选项也能获取目录总大小,不过它先显示子目录大小,最后一行显示总大小,下面例子最后一行 total 字符串前面的 28K 表示 temp/ 目录的总大小

[root@ecs-centos-7 tt]# du -ch temp/
8.0K    temp/test/ha
16K     temp/test
28K     temp/
28K     total

指定目录深度

如果一个目录有很多子目录,只想显示指定层数目录大小的话,可以使用 -d 选项实现

temp/ 的子目录结构如下:

[root@ecs-centos-7 tt]# tree -d temp/
temp/
└── test
    └── ha

2 directories

指定目录深度

[root@ecs-centos-7 tt]# du -d 0 temp/
28      temp/
[root@ecs-centos-7 tt]# du -d 1 temp/
16      temp/test
28      temp/
[root@ecs-centos-7 tt]# du --max-depth=2 temp/
8       temp/test/ha
16      temp/test
28      temp/

du -d 0 temp/: 显示第0层目录,也即当前目录总大小,此时相当于 -s 选项

du -d 1 temp/: 显示第1层目录,也即 temp/test 目录的总大小

du --max-depth=2 temp/: 显示第2层目录,也即 temp/test/ha 目录总大小

显示最近修改时间

[root@ecs-centos-7 tt]# du --time temp
8       2020-07-21 20:11        temp/test/ha
16      2020-07-21 20:11        temp/test
28      2020-07-21 20:13        temp

上面的例子中显示了每个目录最近修改时间,时间的粒度只精确到分钟

如果想显示粒度更细些的话,可以用 --time-syle=STYLE 选项来指定时间的输出格式,其中 STYLE 表示日期的格式化输出字符串,和 date 命令的格式化输出的格式一样的

例1:显示 UTC 时间的秒数(从1970年1月1日到现在的秒数)

[root@ecs-centos-7 tt]# du --time --time-style="+%s" temp/      
8       1595333498      temp/test/ha
16      1595333514      temp/test
28      1595333582      temp/

例2:显示完整的年月日时分秒

[root@ecs-centos-7 tt]# du --time --time-style="+%F %T" temp/  
8       2020-07-21 20:11:38     temp/test/ha
16      2020-07-21 20:11:54     temp/test
28      2020-07-21 20:13:02     temp/

按照大小过滤

从显示的结果中,过滤掉指定大小的目录以及文件

[root@ecs-centos-7 tt]# du -b temp/
4117    temp/test/ha
8218    temp/test
12326   temp/
[root@ecs-centos-7 tt]# du -b -t 4118 temp/
8218    temp/test
12326   temp/

上面的例子中,过滤掉小于 4118 bytes 的目录

按照目录名或文件名过滤

假如一个目录下子目录太多,我们可以根据子目录名或者文件名和指定的模式串匹配,从而过滤掉匹配上的目录和文件

[root@ecs-centos-7 tt]# du -a temp
4       temp/suba.txt
4       temp/test/abc.txt
4       temp/test/ha/ha.txt
8       temp/test/ha
16      temp/test
4       temp/time.txt
28      temp
[root@ecs-centos-7 tt]# du -a --exclude=*a* temp/
4       temp/test
4       temp/time.txt
12      temp/

上面的例子中, 过滤的模式串是: *a*

它表示过滤掉目录名或者文件名中含有字符 a的目录或文件,例子中前面四行的目录或文件名中都包含了 a字符,所以都被过滤掉了

占满磁盘的都是什么文件

开发者经常遇到的问题是磁盘满了,这时我们可以组合使用 du 和 sort 来查出 “元凶”

  • 当前目录下文件从大到小排序
[root@ecs-centos-7 tt]# du -sh temp/* | sort -hr
10M     temp/clpay.tar
16K     temp/test
4.0K    temp/time.txt
4.0K    temp/lnsuba
  • 当前目录以及子目录从大到小排序
[root@ecs-centos-7 tt]# du -ah temp/* | sort -hr
10M     temp/clpay.tar
16K     temp/test
8.0K    temp/test/ha
4.0K    temp/time.txt
4.0K    temp/test/ha/ha.txt
4.0K    temp/test/abc.txt
4.0K    temp/lnsuba
  • 磁盘占用最大的三个目录以及子目录
[root@ecs-centos-7 tt]# du -ah temp/* | sort -hr | head -n 3
10M     temp/clpay.tar
16K     temp/test
8.0K    temp/test/ha

相关推荐:《Linux视频教程

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。