Extension Attribute for Document Folder Size

olcikas_e
New Contributor III

I'm hoping someone can help our school district out with creating an extension attribute to poll our staff computers for the size of their Documents folder. Or some other way to do it entirely? We're looking to determine storage needs for backup purposes as we move to CrashPlan. Thanks for any help you can offer!

9 REPLIES 9

jacob_salmela
Contributor II

Maybe something like this?

du -hc /Users/username/Documents | awk '/total/ {print $1}'

mm2270
Legendary Contributor III

Yeah, the only real way to get a folder size is with du, and be warned, du can be very slow, especially if it runs into a large Documents folder, so if you plan on putting that into an Extension Attribute it will increase your recon times on all Macs.
Also, do you only have one account (minus any hidden admin accounts) per Mac? If you have multiple accounts on each system you'd need to do a loop and drop results into an array, or something like that.

olcikas_e
New Contributor III

Thanks for the help so far, Jacob's command definitely gets me what I want. Is there a way to run a policy against the staff machines at login to run that command and dump the data somewhere? I really need just a one time response from each staff computer. I definitely don't want to slow things down during reoccurring inventory updates.

mm2270
Legendary Contributor III

If you want it to run only at login, you can try replacing "username" with $3 in Jacob's script above.
Just dump the results to a local file somewhere and pick it up later in an Extension Attribute script

du -hc /Users/$3/Documents | awk '/total/ {print $1}' > /private/var/docFolderSize

And then for the EA-

#!/bin/sh

echo "<result>$( cat /private/var/docFolderSize )</result>"

The use of $3 is a little hit or miss in my experience, so if it doesn't work, you'll need to get the logged in user account another way, such as with-

ls -l /dev/console | awk '{print $3}'

brock_walters
Contributor
Contributor

You could also try $USER in place of $3, i.e.:

du -hc /Users/$USER/Documents/

karthikeyan_mac
Valued Contributor

Hi,

You can try the following Extension attribute. This loops through all the users documents folder and displays its size.

#!/bin/bash
echo "<result>"
groupAdmin=`dscl . read /Groups/admin GroupMembership | cut -d " " -f 2- | sed 's/ /|/Users//g'`
UserList=`dscl . list /Users UniqueID | awk '$2 > 500 { print $1 }'` 

for u in $UserList ; 
do 
results=`du -sh /Users/$u/Documents/ | grep -v "[Back" | grep -v ".plist" | awk '{print $1}'`
echo "$u - $results"
done
echo "</result>"
exit 0

Output will be like
username - size

Thanks & Regards,
Karthikeyan

KCH080208
New Contributor II

This is fantastic! Was looking for a way to just see how much storage users are actually using. I just removed the Documents part and tried it on my machine. WORKS GREAT!!

stevewood
Honored Contributor II
Honored Contributor II

I'd be careful running du as part of an EA. Each time a machine submits inventory, du is going to run, and as @mm2270 pointed out above, that can be a bit time consuming.

Instead, consider setting up a policy that runs once a week, or once a month, that runs du and saves the output to a hidden plist file. The scrape the plist with an EA. Using the script posted above, this is one way you could write the plist file:

#!/bin/bash

plistLoc="/private/var/myhiddenfolder/com.example.foldersize.plist"
UserList=`find /Users -mindepth 1 -type d -maxdepth 1 -not -name Shared`

for u in $UserList ; 
    do 
        results=`du -sh $u | grep -v "[Back" | grep -v ".plist" | awk '{print $1}'`

        defaults write ${plistLoc} ${u} -string ${results}

    done

exit 0

And then a very quick, down and dirty, no formatting method of scraping would be an EA like this:

#!/bin/bash

echo "<result>"
defaults read /private/var/myhiddenfolder/com.example.foldersize.plist
echo "</result>"

Obviously in both of those scripts you'd need to edit the plist location to place it somewhere hidden (Not in /tmp obviously).

You could use a Python script, or a Bash script for that matter, to clean up the EA output to be just the folder path and the size. I just wrote this one quickly to prove a point.

brock_walters
Contributor
Contributor

Hi guys -

This might seem kind of weird...

#!/bin/bash
usrnm=($(dscl . list /Users | sed 's/_.*//'))
usrls=($(find /Users -mindepth 1 -type d -maxdepth 1 -not -name Shared | sed 's,/Users/,,g'))
total="${#usrls[@]}"
for i in "${usrnm[@]}"
do
    for ((counter=0;counter<"$total";counter++))
    {
        echo "${usrls[$counter]}" | grep "$i"
    }
done

Here I'm comparing 2 arrays. The 1st array contains the output of dscl . -list /Users minus the hidden OS X System accounts. The 2nd array contains all the directories in the /Users directory minus the /Shared directory. Then, I am using the members of the 2nd array to loop through the members of the 1st array, i.e., I want to find all of the Users according to dscl that have home folders in /Users. Why? Because there may be Users that don't have home folders (like Sharing Only Users) & there may be folders in /Users that aren't really home folders or that actually belong to anyone. Especially with computers in the hands of inexperienced end users or students who knows what could happen?

The result of this comparison is pretty solid ground on which to start looking at folder sizes. You can run this safely on any computer to try it. I like the idea or writing out the results of du to a file & then copying that into an extension attribute, but, some of the techniques above I'm not really sure would get the results you might want every time for finding Users (things like looking at only user accounts above 500 e.g. & grep to awk kind of things...) which is why I added this.

Thanks!