Command-line RAM Disk creation and removal on OSX

Going a bit further with the loopback files, I decided to experiment with RAM filesystems on OS X.  Quite simple, and I created a couple of scripts to automate creation and removal of RAM disks.  Nothing fancy, but figured I’d share. – first parameter is amount of ram to use in mb, second parameter is optional and is the disk name. (no spaces)  By default it’s called “RAMDisk”. – first parameter is optional, and is the disk name.  It defaults to “RAMDisk” if no parameter is provided.


if [ -z $1 ]; then
echo “parameters necessary.”
echo “$0 [ ram use in Mb ] <Disk Name>”
exit 1

if [ ! -z $2 ]; then
echo “Name being set to something different”
RAM=$( expr $1 \* 2048 );
diskutil erasevolume HFS+ “${NAME}” `hdiutil attach -nomount ram://${RAM}`



if [ ! -z $1 ]; then

diskutil unmount “${NAME}”
DISK=`diskutil list | grep -i ${NAME} | awk ‘{ print $5 }’`
hdiutil detach /dev/${DISK}

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.