Posts RSS Comments RSS 48 Posts and 155 Comments till now

Check how much disk space is left using AppleScript

This is a fairly simple one but someone out there may need this. My boss recently had a weird problem where his disk was suddenly filling up. A reboot fixed it but we were having a hard time finding the process that was suddenly eating 15 GB of disk space with no warning. So, I came up with this script that just gets how much free space is left on his disk. We ran it as a cron job every 15 minutes so we could hopefully get some warning before things filled up and became unusable. The script displays a warning when the disk has 5% or less of free space. You can edit it for your own uses. It also has the ability to email the report to you. Comment out the section you don’t want to use. To have it send emails when you are not logged in use something like Lingon for a launchd item or Cronnix for a cron job. Save your script in the appropriate location and have the launchd/cron job call it using the “osascript” command. So, if the file was saved in the /Users/Shared folder the line would read:

osascript /Users/Shared/disk_check.scpt

[codesyntax lang=”applescript”]
set mysubject to “Disk Usage Report”
set myrecipient to “sysadmin@example.com”

–get the amount of free space
set dSize to (do shell script “df -h / | grep %”) as text

–pull out the percent used from the result
set theTotal to word 6 in dSize as number

–if the total used space is 95% or more put up a warning. Comment out if running the email section
if theTotal is greater than or equal to 95 then
display dialog “Your disk is ” & theTotal & “% full”
end if

–comment out this section if no email is desired.
set mybody to (“Free Space: ” & theTotal & “%”)
do shell script (“echo \”” & mybody & “\” | mail -s \”” & mysubject & “\” ” & myrecipient)
[/codesyntax]

Click here to download a copy: Disk Check script

4 Responses to “Check how much disk space is left using AppleScript”

  1. on 03 May 2011 at 7:51 pmGabe

    I need something similar and maybe you can point me in the right direction. I have a mac server with some mounted shares, and two partitions on the physical machine. I want to write a script that will send me an email once a week with the current usage of all mounted shares and hard disk partitions. Any ideas? Thanks!

  2. on 05 May 2011 at 11:49 amwebmaster

    Gabe – a good thought. I’ve updated the code and the script to incorporate sending an email as an option. You can get a list of mount point paths using “diskutil list” and then adapt the script to check them all. I adapted the sendmail section from this article at Mac OS X Hints

  3. on 21 Jun 2013 at 2:26 pmchuckadile

    This has been extremely helpful. How can I adapt this to check all the drives in my mac pro?

  4. on 28 Mar 2014 at 4:45 amMike

    Running OS X Mavericks, I had to change this line:

    “set theTotal to word 8 in dSize as number”

    to

    “set theTotal to word 19 in dSize as number”