Self Service Script to calculate user disk use

monogrant
Contributor

I just finished a simple script to help users find top level offending folders in their user directory when they get the "Low Disk Space" warnings. Love those SSDs, but that warning is more frequent now.

This script uses du to get the data and requires CocoaDialog (I distribute that to each systems' /Applications/Utilities folder with a policy) to ask the user to confirm, display a barbershop pole, and output the du command results.

There might be a more elegant way to find the user folder. I have a mixed environment of directory users and older users with local folders so this method works. I'm using 'grep -Ev' to remove my local admin, Shared and .* directories from the output.

Sadly, CocoaDialog doesn't let you change the font with 'textbox' outputs. I've added some space with awk to help break up the text and size output columns.

#!/bin/bash
#
# This script will check the current user's home folder use and report it in a text box 

# Finding the /Users disk use while stripping out Shared, period and local admins
# 'du -d2 -h "/Users/ | grep -Ev localadmin | grep -Ev Shared, grep -EvF .'

# If your environment is cleaner and ALL home folders are named with the users short names
# you could $3 for the Username from the JSS
# 'du -d1 -h "/Users/'$3'"


# Variables
COCOA_DIALOG='/Applications/Utilities/cocoaDialog.app/Contents/MacOS/cocoadialog'

# Another home folder finding attempt....not working well with SS
# USER_HOME=$(eval echo ~${SUDO_USER})
# echo ${USER_HOME}

CHOICE=$( $COCOA_DIALOG msgbox 
        --title "Disk Use Report" --no-newline 
        --text "Would you like a disk use report generated?" 
        --informative-text "This is a disk use report of your user folder. It can take a minute." 
        --button1 "Yes" --button2 "No" )

if [[ $CHOICE -eq 1 ]]; then
    mkfifo /tmp/du
    touch /tmp/du-out
    $COCOA_DIALOG progressbar --indeterminate --title "Running report..." --text "Depending on size, this might take a minute..." < /tmp/du &
    exec 3<> /tmp/du
    echo -n . >&3
    du -d2 -h /Users/ | grep -Ev localadmin | grep -Ev Shared | grep -EvF . | awk 'BEGIN{OFS=FS="	"} $2 = "  		"$2' > /tmp/du-out
    exec 3>&-
    wait
    $COCOA_DIALOG textbox --informative-text "This displays the size of each folder" --text-from-file /tmp/du-out --button1 "Close"
    rm -f /tmp/du
    rm -f /tmp/du-out
    exit 0
elif [[ $CHOICE -eq 2 ]]; then
        exit 0
    fi
0 REPLIES 0