Last active
November 1, 2026 09:44
-
Star
(118)
You must be signed in to star a gist -
Fork
(43)
You must be signed in to fork a gist
-
-
Save rxin/5085564 to your computer and use it in GitHub Desktop.
ramdisk create/delete on Mac OS X.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # From http://tech.serbinn.net/2010/shell-script-to-create-ramdisk-on-mac-os-x/ | |
| # | |
| ARGS=2 | |
| E_BADARGS=99 | |
| if [ $# -ne $ARGS ] # correct number of arguments to the script; | |
| then | |
| echo " " | |
| echo "To create a RAMDISK -> Usage: `basename $0` create SIZE_IN_MB" | |
| echo "To delete a RAMDISK -> Usage: `basename $0` delete DISK_ID" | |
| echo " " | |
| echo "Currently this script only supports one RAMDISK. Will update soon." | |
| echo "DISK_ID can be shown with 'mount'. usually /dev/disk* where * is a number" | |
| echo " " | |
| echo " " | |
| exit $E_BADARGS | |
| fi | |
| if [ "$1" = "create" ] | |
| then | |
| echo "Create ramdisk..." | |
| RAMDISK_SIZE_MB=$2 | |
| RAMDISK_SECTORS=$((2048 * $RAMDISK_SIZE_MB)) | |
| DISK_ID=$(hdiutil attach -nomount ram://$RAMDISK_SECTORS) | |
| echo "Disk ID is :" $DISK_ID | |
| diskutil erasevolume HFS+ "ramdisk" ${DISK_ID} | |
| fi | |
| if [ "$1" = "delete" ] | |
| then | |
| echo "Delete/unmount ramdisk $2" | |
| umount -f $2 | |
| hdiutil detach $2 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since
tmpfsdropped on macOS 15 (Sequoia), you can also use it to create RAM disks, and this approach has two massive advantages over thehdiutil/diskutil-based implementations:You can easily mount a
tmpfsvolume with the following commands:sudo mount_tmpfs -s 10G path/to/dir— mounts the volume and temporarily hides the original folder contentssudo chown $(whoami):staff path/to/dir— to get write permissions back inside the volumeSame goes for unmounting:
sudo umount path/to/dir— heads up, all volume contents vanish instantly, original folder files reappearTo bypass these mount/unmount "hiccups", I built a utility called RAM Folder that turns any of your folders into a
tmpfsvolume by importing the data into it when mounting, and saves the volume's contents back to the folder when unmounting.Turns out the transition between a standard folder and a RAM disk is so seamless that I decided to call it: RAM Folder.
It's not open-source (yet) but you can check it out on the RAM Folder product page and there's a 14-day trial free trial.
It's super performant for build caches, hosting a unit test DB, or honestly any operation that's IO-bound.
Hope you find this interesting and take the time to give the utility a spin!