display free disk space shell script

Hi,
I would like to write a shell script to return the amount of available disk space for a local volume. I can get it to return the whole line of info for the local volume but I don’t know how to return just the “Avail” size on its own. Any suggestions greatly appreciated!

set theResult to do shell script "df -H | grep disk0s2"

Thanks,
Nik

try this


set theResult to do shell script "/bin/df -H | /usr/bin/awk '/disk0s2/ {print $4}'"

Perfect as usual Stefan!
if possible could you give a brief explanation of the command and arguments?
Many Thanks,
Nik

awk is a pattern-directed scanning and processing language, which works similar as grep.
It filters all lines containing the first parameter (disk0s2) and prints the 4th field separated by the current field separator( tab or space)

Wow, I thought grep was powerful.

Thanks,
Nik

G’day Bend3

The disk space is also available via an Applescript command

Regards

Santa


			set free_bytes to free space of disk YourDiskName -- the number of free bytes left on the disk
			set free_Gbytes to (free_bytes / (1024 * 1024 * 0.1024) div 100) / 100
			

Needs the Finder, Santa:

tell application "Finder" to set free_bytes to free space of disk "ACB-G5_Leopard" -- the number of free bytes left on the disk
set free_Gbytes to (free_bytes / (1024 * 1024 * 0.1024) div 100) / 100

. or System Events :wink:

I am using
set dSize to (do shell script “df -H /Volumes/| grep /dev/disk | cut -d ‘G’ -f 3 | cut -d ’ ’ -f 4-5”) as text
now how I can compare with some number (say 10 or 10 GB) so that I can display dialog

What’s wrong with the System Events version?

tell application "System Events" to set freeBytes to free space of disk "Macintosh HD"
set freeGigaBytes to freeBytes / 1.0E+9 div 1 -- never system versions use 1000 Bytes = 1 kB

or the Cocoa way (edit: including a smart formatting)

Edit: The coercion to record handles the type of the file size (unsigned long long) automatically.

use framework "Foundation"

set fileManager to current application's NSFileManager's defaultManager()
set attributes to fileManager's attributesOfFileSystemForPath:"/" |error|:(missing value)
set freeBytes to (attributes as record)'s NSFileSystemFreeSize
set formattedFreeBytes to current application's NSByteCountFormatter's stringFromByteCount:freeBytes countStyle:(current application's NSByteCountFormatterCountStyleFile)
display dialog (formattedFreeBytes as text)

With this
tell application “System Events” to set freeBytes to free space of disk “Macintosh HD”
set freeGigaBytes to freeBytes / 1.0E+9 div 1 – never system versions use 1000 Bytes = 1 kB

HDD name should be “Macintosh HD” and in a large number this can be different (many user like to change the name).
Can we set it to get free space info from Boot volume

posix file "/" as string
tell application "System Events" to set freeBytes to free space of startup disk

Thanks DJ Bazzie Wazzie

Thanks Stefan

I was looking for the same
working for me
Thanks All

Ah, didn’t saw you using system events. In Mavericks (haven’t tested it on new machines), somehow system events return wrong values. Two of my Maverick machines return wrong values, one returns 1 and the other returns a negative value. In a tell application “Finder” block the right values are returned.

On systems supporting AppleScriptObjC I’d recommend those version anyway.

Then I recommend not to use integerValue() for free disk space. It’s better to use longLongValue() instead to be certain that you always use a 64-bit integer. When running in 32-bit mode integerValue() will only be accurate till 2GB free space (4GB for unsignedIntegerValue()).

Absolutely right, thanks. Strictly spoken the value is unsigned, so it’s even unsignedLongLongValue()

Edit: A coercion to record handles the proper type implicitly. I updated the post above.

Here is StefanK’s proposed version returning the number of free bytes.

use framework "Foundation"

set fileManager to current application's NSFileManager's defaultManager()
set attributes to fileManager's attributesOfFileSystemForPath:"/" |error|:(missing value)
set freeBytes to (attributes's objectForKey:(current application's NSFileSystemFreeSize))'s longLongValue()
set freeBytes to my formatnumber:freeBytes
--> "277 390 725 120"

# Borrowed from Shane STANLEY
on formatnumber:theNumber
	set theResult to current application's NSNumberFormatter's localizedStringFromNumber:theNumber numberStyle:(current application's NSNumberFormatterDecimalStyle)
	return theResult as text
end formatnumber:

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) lundi 30 mai 2016 16:11:57