I started writing this post almost 8 months ago and still haven’t found the inspiration to wrap it up properly… So long story short, I once need to figure out a way to find how much space was taken by a specific user.
I knew about commands such as df -h
, which can give you space used by partition, and du -c
, which can give you the space used by all the files in some folder, but that wasn’t what I needed, because all files by a specific user aren’t limited to the content of /home/[thisuser]. For instance, du /path/to/test -h --max-depth=1
gives size by subfolder, but still not by user.
I eventually found the following script:
awk 'BEGIN{
path="/"
user="username"
total=0
cmd="find "path" -type f -user "user" -printf \"%s\\n\""
while (( cmd |getline line) >0){ total+=line }
close(cmd)
print "total size by user: "user" is: "total" bytes"
}'
(source: http://www.unix.com/filesystems-disks-memory/110427-disk-space.html)
I’m not entirely sure I understand all the magic in it, but basically it browses all subfolders of “path” (in the above snippet, set to the whole system, “/”) and sums up the size of all files belonging to user “username”. Note that it takes a while to run, since it will browse all files, but it will eventually give you the exact total size of files belonging to that specific user, with the only exception of the files/folders for which you don’t have read permission (won’t be a problem if you’re root).
script is working fine. thanks!!