Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Move Chrome, Safari and iTunes Cache to Ramdisk.
#!/bin/bash
# Size at the end is * 2048 where 2048 = 1 MB, so 1572864 = 768 MB
#DISK=`/usr/bin/hdiutil attach -nobrowse -nomount ram://1572864`
DISK=`/usr/bin/hdiutil attach -nobrowse -nomount ram://2097152`
/usr/sbin/diskutil erasevolume HFS+ "RamDiskCache" $DISK
CACHEDIR="/Volumes/RamDiskCache/$USER"
# Chrome Cache
/bin/rm -rvf ~/Library/Caches/Google/Chrome/*
/bin/mkdir -pv $CACHEDIR/Google/Chrome/Default
/bin/ln -v -s $CACHEDIR/Google/Chrome/Default ~/Library/Caches/Google/Chrome/Default
# Chrome Canary Cache
/bin/rm -rvf ~/Library/Caches/Google/Chrome\ Canary/*
/bin/mkdir -pv $CACHEDIR/Google/Chrome\ Canary/Default
/bin/ln -v -s $CACHEDIR/Google/Chrome\ Canary/Default ~/Library/Caches/Google/Chrome\ Canary/Default
# Safari Cache
/bin/rm -rvf ~/Library/Caches/com.apple.Safari
/bin/mkdir -pv $CACHEDIR/Apple/Safari
/bin/ln -v -s $CACHEDIR/Apple/Safari ~/Library/Caches/com.apple.Safari
# iTunes Cache
/bin/rm -rvf ~/Library/Caches/com.apple.iTunes
/bin/mkdir -pv $CACHEDIR/Apple/iTunes
/bin/ln -v -s $CACHEDIR/Apple/iTunes ~/Library/Caches/com.apple.iTunes
# Spotify Cache
/bin/rm -rvf ~/Library/Caches/com.spotify.client
/bin/mkdir -pv $CACHEDIR/Spotify
/bin/ln -v -s $CACHEDIR/Spotify ~/Library/Caches/com.spotify.client
/usr/bin/chflags hidden /Volumes/RamDiskCache #Hide RamDisk directory
@transalp95

This comment has been minimized.

Copy link

commented Nov 9, 2014

Good script work very well, can you help me to start at boot in Yosemite?

@lebinho

This comment has been minimized.

Copy link

commented Jul 5, 2015

@nischu7

when I turn on programs I can't see anything in Finder when I look at RamDiskCache folder...
is there sth wrong with the code?

I created sth like this:

!/bin/bash

Size at the end is * 2048 where 2048 = 1 MB, so 1572864 = 768 MB

DISK=/usr/bin/hdiutil attach -nobrowse -nomount ram://819200

/usr/sbin/diskutil erasevolume HFS+ "RamDiskCache" $DISK
CACHEDIR=“/Volumes/RamDiskCache/$USER“

Chrome Canary Cache

/bin/rm -rvf ~/Library/Caches/Google/Chrome\ Canary/*
/bin/mkdir -pv $CACHEDIR/Google/Chrome\ Canary/Default
/bin/ln -v -s $CACHEDIR/Google/Chrome\ Canary/Default ~/Library/Caches/Google/Chrome\ Canary/Default

Safari Cache

/bin/rm -rvf ~/Library/Caches/com.apple.Safari
/bin/mkdir -pv $CACHEDIR/Apple/Safari
/bin/ln -v -s $CACHEDIR/Apple/Safari ~/Library/Caches/com.apple.Safari

iTunes Cache

/bin/rm -rvf ~/Library/Caches/com.apple.iTunes
/bin/mkdir -pv $CACHEDIR/Apple/iTunes
/bin/ln -v -s $CACHEDIR/Apple/iTunes ~/Library/Caches/com.apple.iTunes

Spotify Cache

/bin/rm -rvf ~/Library/Caches/com.spotify.client
/bin/mkdir -pv $CACHEDIR/Spotify
/bin/ln -v -s $CACHEDIR/Spotify ~/Library/Caches/com.spotify.client

Chrome Cache

/bin/rm -rvf ~/Library/Caches/Google/Chrome/*
/bin/mkdir -pv /Volumes/RamDiskCache/Google
/bin/ln -v -s /Volumes/RamDiskCache/Google ~/Library/Caches/Google/Chrome/Default

Hide RamDisk directory

/usr/bin/chflags hidden /Volumes/RamDiskCache

@taidoka

This comment has been minimized.

Copy link

commented Aug 7, 2015

@transalp95
To create a startup script in OS X you'll have to build an Automator app.
Start Automator. With CMD + N to you'll create a new workflow. In the first dialogue you choose for creating an app.
Then you'll search for 'shell'. Drag it. Paste this script. Save it - probably to your app folder.
In System Settings go to Users and groups. Click on Log-on objects and add your recently created app. That would do it.

@rdjg22

This comment has been minimized.

Copy link

commented Oct 6, 2015

How would I move Safari Cache back to original folder? I've deleted the RAM Disk but com.apple.Safari isn't in Library/Cache after reboot.

@amorphid

This comment has been minimized.

Copy link

commented Mar 11, 2017

@nischu7 if you don't mind answering, the /dev/rdisk2 size of 1024 MB is ~50% the size of the ram://2097152. That seems to be consistent for various different file systems. Is there a short explanation the size difference, or can you recommend what I might read up on to better understand what's going on?

$ DISK=`/usr/bin/hdiutil attach -nobrowse -nomount ram://2097152`
$ /usr/sbin/diskutil erasevolume HFS+ "RamDiskCache" $DISK
Started erase on disk2
Unmounting disk
Erasing
Initialized /dev/rdisk2 as a 1024 MB case-insensitive HFS Plus volume
Mounting disk
Finished erase on disk2 RamDiskCache
@amorphid

This comment has been minimized.

Copy link

commented Mar 11, 2017

Found the answer using diskutil info $DISK

....
Total Size:               1.1 GB (1073741824 Bytes) (exactly 2097152 512-Byte-Units)
....
@blk-swn

This comment has been minimized.

Copy link

commented Jun 6, 2017

Hi I'm wondering if I put this script in my ~/Library/StartupItems directory, will it run at startup?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.