如何获取 bash 中某些文件夹的总大小?

how to get the total size of certain folders in bash?

如何获取特定文件夹的总大小?

例如,我在 users.txt

中有一个特定文件夹名称的列表
# cat users.txt
user1
user2
user3

所有这些文件夹都位于 /home/

我试过执行:

# for i in `cat users.txt`; do du -shc /home/$i/; done
3.9M    /home/user1/
3.9M    total
141M    /home/user2/
141M    total
75M /home/user3/
75M total

但我需要所有这些文件夹的总大小。

du -shc $(sed 's@^@/home/@' users.txt)

它使用 users.txt 的内容作为 /home/ 的参数作为 du 的参数,因此它会为您求和。

您可以将 du 的输出传递给 tail -n 1 或执行

du -s directory

只得到尺寸你可以做到

du -s directory | cut -f1