Space usage by top 5 users in a filesystem

I want to see top 5 users,who have occupied most amount of disk space in a filesystem.
But not sure how to do it.
I can get the usage for a particular user

find . -user user -type f exec df -h {} \;|awk '{ s = s+$1 } END { print "Total used: ",s }'

But how to get without specifying any user for a filesystem ?

Then I can sort for top 5 usages.

Please advise..Thanks in advance !!

Try

find /home -exec ls -ls {} + | awk '{sum[$4]+=$1} END {for (u in sum) print u, sum}'
 0
user1 2753844
sys 2888
root 16872

For disk usage you'll need to sum up the disk blocks allocated, not the actual file sizes, so we need to run ls -ls .

1 Like

could you plz explain how this works because I am not sure whether the space it's showing is correct or not..

ls -ls (on my linux and FreeBSD systems) shows the files' and directories' allocated 1k-blocks in the first column. All the script does is sum these up per user. Test it on small directories to verify it's true/correct.

1 Like

Thank u so much for ur advise.
I tried these two commands below :

find /home -exec ls -ls {} + | awk '{sum[$4]+=$1} END {for (u in sum) print u, sum}'
 0
ra60 385344
ls -ls |awk '{s=s+$1} END { print "Total used: ",s }'
Total used:  192392

As per my understanding these two should return the same amount of space used because I tried this in my home directory and there are files owned only by me.

But, these are different.

There are alternatives not using awk.
If the du command is your cup-o-tea, you could run something like this as root user.

$ for u in $(ls /home/); do printf "%s\n" "$(du -sh /home/$u)"; done | sort >> outfile && head -n 5 outfile
------------
256M    /home/brutus/
176M    /home/userA/
126M    /home/izzy/
36M     /home/linus/
31M     /home/maud/

So - run the two command along with each other without piping through awk and compare line by line. Are you the only user in the /home directory?
Try to list directories only:

find /home -type d -exec ls -ls {} + | awk '{sum[$4]+=$1} END {for (u in sum) print u, sum}'