Anonymous Anonymous - 1 year ago 133
Bash Question

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

How can I get the total size of the certain folders?

For example I have a list of specific folder names in users.txt

# cat users.txt
user1
user2
user3


all this folders are locate in /home/

I have tried to execute:

# 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


but I need a total size of all of this folders.

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

That uses the contents of users.txt, prepended with /home/, as the arguments to du, so it will sum them for you.