Check total file size of a specific directory for a specific user
Asked Answered
E

2

8

I want to check how much of the total file storage for a specific user has been used on a specific directory. I am using ls -lR ./* | grep userid to list files belonging to a specific user. But then how I can get the total file sizes of them?

Eliason answered 11/7, 2011 at 0:42 Comment(0)
D
5

Use awk

ls -lR ./* | grep userid | awk '{sum = sum + $5} END {print sum}'
Deloris answered 11/7, 2011 at 3:53 Comment(2)
anyway to get this "human-readable" on the other side? thanks!Laborer
@shootingstars Simple version producing human-readable output is ls -lR ./* | grep userid | awk '{sum = sum + $5} END {printf "%iK\n", sum/1024}'. Because this is integer math, there is no rounding. You could extend this to MB or GB, but anything more complex than this and I would use Ruby.Deloris
G
9

You can do something like this, which will show you the size if each directory in the users home directory, and will print a total size of their home directory at the end (sum of all sub-directories).

du -sch /home/USER/*
Gailey answered 11/7, 2011 at 1:23 Comment(1)
but this one doesn't meet my requirement. We have a group directory which all group members can write to and I want to check how much storage space a specific user has used. Not just their home directory. Thanks anyway.Eliason
D
5

Use awk

ls -lR ./* | grep userid | awk '{sum = sum + $5} END {print sum}'
Deloris answered 11/7, 2011 at 3:53 Comment(2)
anyway to get this "human-readable" on the other side? thanks!Laborer
@shootingstars Simple version producing human-readable output is ls -lR ./* | grep userid | awk '{sum = sum + $5} END {printf "%iK\n", sum/1024}'. Because this is integer math, there is no rounding. You could extend this to MB or GB, but anything more complex than this and I would use Ruby.Deloris

© 2022 - 2024 — McMap. All rights reserved.