如何在一行中获取目录中所有内容的总大小?

AGa*_*yer 35 ls shell bash directory disk-usage

我知道我可以使用 du -h 输出目录的总大小。但是当它包含其他子目录时,输出将类似于:

du -h /root/test

.
.
.
.
24K   /root/test/1
64K   /root/test/2
876K  /root/test/3
1.1M  /root/test/4
15M   /root/test/5
17M   /root/test
Run Code Online (Sandbox Code Playgroud)

我只想要最后一行,因为目录中的小目录太多/root/test。我能做什么?

Joh*_*ohn 57

添加--max-depth值为 0的参数:

du -h --max-depth=0 /root/test
Run Code Online (Sandbox Code Playgroud)

或者,使用-s(摘要)选项:

du -sh /root/test
Run Code Online (Sandbox Code Playgroud)

其中任何一个都应该给你你想要的。供以后参考,man du很有帮助。

  • +1 备注:“du”的某些实现虽然没有“--max-depth”选项(例如,Busybox)。但是在这种情况下 `-s` 选项可以解决问题 (5认同)
  • `-s` 是标准的,`--max-depth` 是 GNU 特定的。简短版本 (`-d`) 也适用于 FreeBSD。`-h` 不是标准的,但有一些实现支持。 (5认同)
  • @AwQiruiGuo:`2>/dev/null` 会导致 stderr 被忽略 (2认同)