Menu Close

Disk Usage Report for Jamf Pro Self Service (macOS Monterey-compatible)

After stumbling across one of my Jamf Nation posts from June 2015 — which had a comment from yesterday — it seemed like it was time to provide the macOS Monterey-compatible version

Jamf Pro Self Service Disk Usage Report Policy

Background

To aid end-users in determining where all their hard drive space has gone, we have a Self Service policy which leverages du and outputs text files of the top 50 directories of the root volume and the user’s home folder. The utilitarian results are sorted by size, in gigabytes, and saved to the user’s desktop.

Note: Disk Space is reported the same as Disk Utility, not what is shown in Finder.

Amount of space available, which can include both free space and “purgeable space” — or space that macOS can free up when needed by removing files from your computer. You can’t manually remove the files that are designated purgeable, but macOS removes them as space is required.

Get detailed information about a disk in Disk Utility on Mac

Self Service Description

Please see [KB8675309](https://servicenow.company.com/support?id=kb_article_view&sysparm_article=KB8675309) for a detailed usage instructions.

Outputs a text file to your Desktop, which includes a listing of the largest 50 directories of your user folder.

The result, sorted by size (in gigabytes), is opened in Safari and saved to your desktop as: **{YourUserName}-DiskUsage.txt**.

(Execution time can be in excess of 10 minutes.)

Disk Usage: Home Directory

Latest version available on GitHub.

#!/bin/bash
####################################################################################################
#
# ABOUT
#
#   Disk Usage: Home Directory
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 8-Dec-2014, Dan K. Snelson (@dan-snelson) (@dan-snelson)
#       Original version
#   Version 1.1, 8-Jun-2015, Dan K. Snelson (@dan-snelson)
#       See: https://jamfnation.jamfsoftware.com/discussion.html?id=14701
#   Version 1.2, 4-Jan-2017, Dan K. Snelson (@dan-snelson)
#       Updated for macOS 10.12
#   Version 1.3, 4-Jul-2018, Dan K. Snelson (@dan-snelson)
#       Updated for macOS 10.13
#   Version 1.4, 11-Nov-2020, Dan K. Snelson (@dan-snelson)
#       Added Time Machine Local Snapshot Information
#   Version 1.5, 28-May-2022, Dan K. Snelson (@dan-snelson)
#       See: https://snelson.us/2022/05/disk-usage-report-monterey-compatible/
#
####################################################################################################



####################################################################################################
#
# Variables
#
####################################################################################################

scriptVersion="1.5"
scriptResult="Version ${scriptVersion}; "
loggedInUser=$( /bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }' )
loggedInUserHome=$( /usr/bin/dscl . -read /Users/$loggedInUser NFSHomeDirectory | /usr/bin/awk '{print $NF}' ) # mm2270
machineName=$( /usr/sbin/scutil --get LocalHostName )
volumeName=$( /usr/sbin/diskutil info / | /usr/bin/grep "Volume Name:" | /usr/bin/awk '{print $3,$4}' )



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Calculate Free Space
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

FreeSpace=$( /usr/sbin/diskutil info / | /usr/bin/grep  -E 'Free Space|Available Space|Container Free Space' | /usr/bin/awk -F ":\s*" '{ print $2 }' | awk -F "(" '{ print $1 }' | xargs )
FreeBytes=$( /usr/sbin/diskutil info / | /usr/bin/grep -E 'Free Space|Available Space|Container Free Space' | /usr/bin/awk -F "(\\\(| Bytes\\\))" '{ print $2 }' )
DiskBytes=$( /usr/sbin/diskutil info / | /usr/bin/grep -E 'Total Space' | /usr/bin/awk -F "(\\\(| Bytes\\\))" '{ print $2 }' )
FreePercentage=$(echo "scale=2; $FreeBytes*100/$DiskBytes" | bc)
diskSpace="$FreeSpace free (${FreePercentage}% available)"
outputFileName="$loggedInUserHome/Desktop/$loggedInUser-DiskUsage-`date '+%Y-%m-%d-%H%M%S'`.txt"



####################################################################################################
#
# Functions
#
####################################################################################################

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# JAMF Display Message
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

function jamfDisplayMessage() {
    echo "${1}"
    scriptResult+="${1}; "
    /usr/local/jamf/bin/jamf displayMessage -message "${1}" &
}



####################################################################################################
#
# Program
#
####################################################################################################

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Logging preamble
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

echo "Disk Usage: Home Directory (${scriptVersion})"



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Display message
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

jamfDisplayMessage "Please wait about three minutes while the disk usage for \"$loggedInUser\" is analyzed …"







# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Output to log
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

echo "### Disk usage for \"$loggedInUserHome\" for volume \"$volumeName\" on computer \"$machineName\"  ###"
echo "Disk Space: $diskSpace"
echo "Report Location: $outputFileName"



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Output to user
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

/bin/echo "-----------------------------------------------------------------------------------------------------------------------------------------" > $outputFileName
/bin/echo "Disk usage for \"$loggedInUserHome\" for volume \"$volumeName\" on computer \"$machineName\" " >> $outputFileName
/bin/echo "Disk Space: $diskSpace" >> $outputFileName
/bin/echo "Report Location: $outputFileName" >> $outputFileName
/bin/echo "-----------------------------------------------------------------------------------------------------------------------------------------" >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo "GBs    Directory or File" >> $outputFileName
/bin/echo " " >> $outputFileName
/usr/bin/du -axrg $loggedInUserHome | /usr/bin/sort -nr | /usr/bin/head -n 50 >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo "-----------------------------------------------------------------------------------------------------------------------------------------" >> $outputFileName



# To Do: ~/Library/Developer/CoreSimulator
#xcrun simctl delete unavailable



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Time Machine Local Snapshot Information
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

/bin/echo "
###
# Time Machine Information
###
" >> $outputFileName

tmDestinationInfo=$( /usr/bin/tmutil destinationinfo )

if [[ "${tmDestinationInfo}" == *"No destinations configured"* ]]; then

    # Time Machine destination NOT configured.
    /bin/echo "WARNING: Time Machine destination NOT configured." >> $outputFileName

else

    # List Time Machine Local Snapshots
    #/bin/echo "Time Machine Local Snapshots:" >> $outputFileName
    /usr/bin/tmutil listlocalsnapshots / >> $outputFileName

    /bin/echo "
---
- Thin Local Time Machine Snapshots
---

Thinning local Time Machine snapshots can quickly free up disk space by PERMANENTLY deleting local Time Machine snapshots.

man tmutil

    thinlocalsnapshots mount_point [purge_amount] [urgency]

        Thin local Time Machine snapshots for the specified volume.

        When purge_amount and urgency are specified, tmutil will attempt (with urgency level 1-4)
        to reclaim purge_amount in bytes by thinning snapshots.

        If urgency is not specified, the default urgency will be used.



ABSOLUTELY UNSUPPORTED EXAMPLES TO BE USED AT YOUR OWN RISK:

# Free 20 GB of snapshots stored on the boot drive (with maximum urgency)
tmutil thinlocalsnapshots / 21474836480 4

# Free 36 GB of snapshots stored on the boot drive (with maximum urgency)
tmutil thinlocalsnapshots / 38654705664 4



" >> $outputFileName

fi



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Open in Safari
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

if [ -f $outputFileName ]; then
    /usr/bin/su - $loggedInUser -c "open -a safari $outputFileName"
fi

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exit
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

scriptResult+="End-of-line"
echo ${scriptResult}

exit 0        ## Success
exit 1        ## Failure

Disk Usage: Root Volume

Latest version available on GitHub.

#!/bin/bash
####################################################################################################
#
# ABOUT
#
#    Disk Usage: Root Volume
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 8-Dec-2014, Dan K. Snelson (@dan-snelson) (@dan-snelson)
#       Original version
#   Version 1.1, 8-Jun-2015, Dan K. Snelson (@dan-snelson)
#       See: https://jamfnation.jamfsoftware.com/discussion.html?id=14701
#   Version 1.2, 4-Jan-2017, Dan K. Snelson (@dan-snelson)
#       Updated for macOS 10.12
#   Version 1.3, 4-Jul-2018, Dan K. Snelson (@dan-snelson)
#       Updated for macOS 10.13
#   Version 1.4, 11-Nov-2020, Dan K. Snelson (@dan-snelson)
#       Increased version number to match "Disk Usage Home Directory"
#   Version 1.5, 28-May-2022, Dan K. Snelson (@dan-snelson)
#       See: https://snelson.us/2022/05/disk-usage-report-monterey-compatible/
#
####################################################################################################



####################################################################################################
#
# Variables
#
####################################################################################################

scriptVersion="1.5"
scriptResult="Version ${scriptVersion}; "
loggedInUser=$( /bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }' )
loggedInUserHome=$( /usr/bin/dscl . -read /Users/$loggedInUser NFSHomeDirectory | /usr/bin/awk '{print $NF}' ) # mm2270
machineName=$( /usr/sbin/scutil --get LocalHostName )
volumeName=$( /usr/sbin/diskutil info / | /usr/bin/grep "Volume Name:" | /usr/bin/awk '{print $3,$4}' )



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Calculate Free Space
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
FreeSpace=$( /usr/sbin/diskutil info / | /usr/bin/grep  -E 'Free Space|Available Space|Container Free Space' | /usr/bin/awk -F ":\s*" '{ print $2 }' | awk -F "(" '{ print $1 }' | xargs )
FreeBytes=$( /usr/sbin/diskutil info / | /usr/bin/grep -E 'Free Space|Available Space|Container Free Space' | /usr/bin/awk -F "(\\\(| Bytes\\\))" '{ print $2 }' )
DiskBytes=$( /usr/sbin/diskutil info / | /usr/bin/grep -E 'Total Space' | /usr/bin/awk -F "(\\\(| Bytes\\\))" '{ print $2 }' )
FreePercentage=$(echo "scale=2; $FreeBytes*100/$DiskBytes" | bc)
diskSpace="$FreeSpace free (${FreePercentage}% available)"
outputFileName="$loggedInUserHome/Desktop/$machineName-ComputerDiskUsage-`date '+%Y-%m-%d-%H%M%S'`.txt"



####################################################################################################
#
# Functions
#
####################################################################################################

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# JAMF Display Message
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

function jamfDisplayMessage() {
    echo "${1}"
    scriptResult+="${1}; "
    /usr/local/jamf/bin/jamf displayMessage -message "${1}" &
}



####################################################################################################
#
# Program
#
####################################################################################################

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Logging preamble
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

echo "Disk Usage: Root Volume (${scriptVersion})"



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Display message
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

jamfDisplayMessage "Please wait 15 minutes (or more) while the disk usage for the entire \"$volumeName\" is analyzed."



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Output to log
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

echo "### Disk usage for volume \"$volumeName\" on computer \"$machineName\"  ###"
echo "Disk Space: $diskSpace"
echo "Report Location: $outputFileName"



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Output to user
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

/bin/echo "-----------------------------------------------------------------------------------------------------------------------------------------" > $outputFileName
/bin/echo "Disk usage for volume \"$volumeName\" on computer \"$machineName\" " >> $outputFileName
/bin/echo "Disk Space: $diskSpace" >> $outputFileName
/bin/echo "Report Location: $outputFileName" >> $outputFileName
/bin/echo "-----------------------------------------------------------------------------------------------------------------------------------------" >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo "GBs    Directory or File" >> $outputFileName
/bin/echo " " >> $outputFileName
/usr/bin/du -axrg / 2>&1 | /usr/bin/sort -nr | /usr/bin/head -n 50 >> $outputFileName
/bin/echo " " >> $outputFileName
/bin/echo "-----------------------------------------------------------------------------------------------------------------------------------------" >> $outputFileName



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Open in Safari
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

if [ -f $outputFileName ]; then
    /usr/bin/su - $loggedInUser -c "open -a safari $outputFileName"
fi



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exit
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

scriptResult+="End-of-line"
echo ${scriptResult}

exit 0        ## Success
exit 1        ## Failure

Storage Management Launch

Launches the Storage Management.app as the currently logged-in user; latest version available on GitHub.

#!/bin/bash
####################################################################################################
#
# ABOUT
#
#   Launch Storage Management.app
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0.0, 24-May-2018, Dan K. Snelson
#       Original version
#
####################################################################################################



####################################################################################################
#
# Variables
#
####################################################################################################

scriptVersion="1.0.0"
scriptResult="Version ${scriptVersion}; "
loggedInUser=$( /bin/echo "show State:/Users/ConsoleUser" | /usr/sbin/scutil | /usr/bin/awk '/Name :/ { print $3 }' )



####################################################################################################
#
# Program
#
####################################################################################################

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Logging preamble
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

echo "Launch Storage Management.app (${scriptVersion})"



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Launch Storage Management.app as the currently logged-in user
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

/usr/bin/su \- "${loggedInUser}" -c "/usr/bin/open '/System/Library/CoreServices/Applications/Storage Management.app'"

scriptResult+="Launched Storage Management.app as ${loggedInUser}; "



# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exit
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

scriptResult+="End-of-line"
echo ${scriptResult}

exit 0        ## Success
exit 1        ## Failure

Thin Local Time Machine Snapshots

Even after using the Disk Usage Report Self Service policy and Apple articles, users may still be unable to free up enough desired disk space.

If users have Time Machine enabled, they may be able to “thin” local snapshots to quickly free up many gigabytes of space.

If Time Machine is enabled, the bottom of the user’s Disk Usage Report will list all the local Time Machine snapshots and macOS Terminal commands to thin the local snapshots.

###
# Time Machine Information
###

Snapshots for disk /:
com.apple.TimeMachine.2022-05-27-031554.local
com.apple.TimeMachine.2022-05-27-190913.local
com.apple.TimeMachine.2022-05-27-200953.local
com.apple.TimeMachine.2022-05-27-221100.local
com.apple.TimeMachine.2022-05-27-231244.local
com.apple.TimeMachine.2022-05-28-001553.local
com.apple.TimeMachine.2022-05-28-011613.local
com.apple.TimeMachine.2022-05-28-021828.local
com.apple.TimeMachine.2022-05-28-031909.local
com.apple.TimeMachine.2022-05-28-041926.local
com.apple.TimeMachine.2022-05-28-051941.local
com.apple.TimeMachine.2022-05-28-062058.local
com.apple.TimeMachine.2022-05-28-072138.local
com.apple.TimeMachine.2022-05-28-085744.local
com.apple.TimeMachine.2022-05-28-095758.local
com.apple.TimeMachine.2022-05-28-105833.local
com.apple.TimeMachine.2022-05-28-115853.local
com.apple.TimeMachine.2022-05-28-130049.local
com.apple.TimeMachine.2022-05-28-140117.local
com.apple.TimeMachine.2022-05-28-150241.local
com.apple.TimeMachine.2022-05-28-160306.local
com.apple.TimeMachine.2022-05-28-170536.local
com.apple.TimeMachine.2022-05-28-180817.local
com.apple.TimeMachine.2022-05-28-190834.local

---
- Thin Local Time Machine Snapshots
---

Thinning local Time Machine snapshots can quickly free up disk space by PERMANENTLY deleting local Time Machine snapshots.

man tmutil

    thinlocalsnapshots mount_point [purge_amount] [urgency]

        Thin local Time Machine snapshots for the specified volume.

        When purge_amount and urgency are specified, tmutil will attempt (with urgency level 1-4)
        to reclaim purge_amount in bytes by thinning snapshots.

        If urgency is not specified, the default urgency will be used.



ABSOLUTELY UNSUPPORTED EXAMPLES TO BE USED AT YOUR OWN RISK:

# Free 20 GB of snapshots stored on the boot drive (with maximum urgency)
tmutil thinlocalsnapshots / 21474836480 4

# Free 36 GB of snapshots stored on the boot drive (with maximum urgency)
tmutil thinlocalsnapshots / 38654705664 4
Posted in Device Management, Jamf Pro, Scripts, Tips & Tricks

Related Posts