Skip to content

Instantly share code, notes, and snippets.

@iryston
Created June 9, 2020 17:49
Show Gist options
  • Save iryston/e21fb2b671906d748b1f078aac2653ab to your computer and use it in GitHub Desktop.
Save iryston/e21fb2b671906d748b1f078aac2653ab to your computer and use it in GitHub Desktop.
Create RAM Disk in MacOS and move some cache folders to RAM
#!/bin/bash
## Read and edit this file.
## Run it once as a root.
FILE="/Library/Scripts/ramdisk.sh"
if [[ $EUID -ne 0 ]]; then
echo "You must be a root user" 2>&1
exit 1
else
cat > "$FILE" << 'EOF'
#!/bin/bash
# Size at the end is * 2048 where 2048 = 1 MB, so 8388608 = 4096 MB
if ! test -e "/Volumes/RAMD"; then
diskutil erasevolume HFS+ "RAMD" `hdiutil attach -nobrowse -nomount ram://8388608`
fi
CACHEDIR="/Volumes/RAMD/.Cache"
if test -e "/Volumes/RAMD"; then
# Create CacheDir
mkdir -p "$CACHEDIR"
# Google Chrome Cache
mkdir -p "$CACHEDIR/GoogleChrome/com.google.Chrome"
mkdir -p "$CACHEDIR/GoogleChrome/Default"
mkdir -p "$CACHEDIR/GoogleChrome/Service Worker/CacheStorage"
mkdir -p "$CACHEDIR/GoogleChrome/Service Worker/ScriptCache"
if [ ! -L "$HOME/Library/Caches/com.google.Chrome" ]
then
rm -rf "$HOME/Library/Caches/com.google.Chrome"
ln -s "$CACHEDIR/GoogleChrome/com.google.Chrome" "$HOME/Library/Caches/com.google.Chrome"
fi
if [ ! -L "$HOME/Library/Caches/Google/Chrome/Default" ]
then
rm -rf "$HOME/Library/Caches/Google/Chrome/Default"
ln -s "$CACHEDIR/GoogleChrome/Default" "$HOME/Library/Caches/Google/Chrome/Default"
fi
if [ ! -L "$HOME/Library/Application Support/Google/Chrome/Default/Service Worker/CacheStorage" ]
then
rm -rf "$HOME/Library/Application Support/Google/Chrome/Default/Service Worker/CacheStorage"
ln -s "$CACHEDIR/GoogleChrome/Service Worker/CacheStorage" "$HOME/Library/Application Support/Google/Chrome/Default/Service Worker/CacheStorage"
fi
if [ ! -L "$HOME/Library/Application Support/Google/Chrome/Default/Service Worker/ScriptCache" ]
then
rm -rf "$HOME/Library/Application Support/Google/Chrome/Default/Service Worker/ScriptCache"
ln -s "$CACHEDIR/GoogleChrome/Service Worker/ScriptCache" "$HOME/Library/Application Support/Google/Chrome/Default/Service Worker/ScriptCache"
fi
# Chromium Cache
mkdir -p "$CACHEDIR/Chromium/Default"
mkdir -p "$CACHEDIR/Chromium/Service Worker/CacheStorage"
mkdir -p "$CACHEDIR/Chromium/Service Worker/ScriptCache"
if [ ! -L "$HOME/Library/Caches/Chromium/Default" ]
then
rm -rf "$HOME/Library/Caches/Chromium/Default"
ln -s "$CACHEDIR/Chromium/Default" "$HOME/Library/Caches/Chromium/Default"
fi
if [ ! -L "$HOME/Library/Application Support/Chromium/Default/Service Worker/CacheStorage" ]
then
rm -rf "$HOME/Library/Application Support/Chromium/Default/Service Worker/CacheStorage"
ln -s "$CACHEDIR/Chromium/Service Worker/CacheStorage" "$HOME/Library/Application Support/Chromium/Default/Service Worker/CacheStorage"
fi
if [ ! -L "$HOME/Library/Application Support/Chromium/Default/Service Worker/ScriptCache" ]
then
rm -rf "$HOME/Library/Application Support/Chromium/Default/Service Worker/ScriptCache"
ln -s "$CACHEDIR/Chromium/Service Worker/ScriptCache" "$HOME/Library/Application Support/Chromium/Default/Service Worker/ScriptCache"
fi
# Opera Cache
mkdir -p "$CACHEDIR/Opera/com.operasoftware.Opera"
if [ ! -L "$HOME/Library/Caches/com.operasoftware.Opera" ]
then
rm -rf "$HOME/Library/Caches/com.operasoftware.Opera"
ln -s "$CACHEDIR/Opera/com.operasoftware.Opera" "$HOME/Library/Caches/com.operasoftware.Opera"
fi
# Safari Cache
mkdir -p "$CACHEDIR/Safari/com.apple.Safari"
mkdir -p "$CACHEDIR/Safari/com.apple.Safari.SafeBrowsing"
if [ ! -L "$HOME/Library/Caches/com.apple.Safari" ]
then
rm -rf "$HOME/Library/Caches/com.apple.Safari"
ln -s "$CACHEDIR/Safari/com.apple.Safari" "$HOME/Library/Caches/com.apple.Safari"
fi
if [ ! -L "$HOME/Library/Caches/com.apple.Safari.SafeBrowsing" ]
then
rm -rf "$HOME/Library/Caches/com.apple.Safari.SafeBrowsing"
ln -s "$CACHEDIR/Safari/com.apple.Safari.SafeBrowsing" "$HOME/Library/Caches/com.apple.Safari.SafeBrowsing"
fi
# Apple Music Cache
mkdir -p "$CACHEDIR/com.apple.Music"
if [ ! -L "$HOME/Library/Caches/com.apple.Music" ]
then
rm -rf "$HOME/Library/Caches/com.apple.Music"
ln -s "$CACHEDIR/com.apple.Music" "$HOME/Library/Caches/com.apple.Music"
fi
# Spotify Cache
mkdir -p "$CACHEDIR/Spotify/com.spotify.client"
mkdir -p "$CACHEDIR/Spotify/PersistentCache"
if [ ! -L "$HOME/Library/Caches/com.spotify.client" ]
then
rm -rf "$HOME/Library/Caches/com.spotify.client"
ln -s "$CACHEDIR/Spotify/com.spotify.client" "$HOME/Library/Caches/com.spotify.client"
fi
if [ ! -L "$HOME/Library/Application Support/Spotify/PersistentCache" ]
then
rm -rf "$HOME/Library/Application Support/Spotify/PersistentCache"
ln -s "$CACHEDIR/Spotify/PersistentCache" "$HOME/Library/Application Support/Spotify/PersistentCache"
fi
# VSCodium Cache
mkdir -p "$CACHEDIR/VSCodium/Cache"
mkdir -p "$CACHEDIR/VSCodium/CachedData"
if [ ! -L "$HOME/Library/Application Support/VSCodium/Cache" ]
then
rm -rf "$HOME/Library/Application Support/VSCodium/Cache"
ln -s "$CACHEDIR/VSCodium/Cache" "$HOME/Library/Application Support/VSCodium/Cache"
fi
if [ ! -L "$HOME/Library/Application Support/VSCodium/CachedData" ]
then
rm -rf "$HOME/Library/Application Support/VSCodium/CachedData"
ln -s "$CACHEDIR/VSCodium/CachedData" "$HOME/Library/Application Support/VSCodium/CachedData"
fi
# JetBrains Products Cache & Logs
mkdir -p "$CACHEDIR/JetBrains/Caches"
if [ ! -L "$HOME/Library/Caches/JetBrains" ]
then
rm -rf "$HOME/Library/Caches/JetBrains"
ln -s "$CACHEDIR/JetBrains/Caches" "$HOME/Library/Caches/JetBrains"
fi
mkdir -p "$CACHEDIR/JetBrains/Logs"
if [ ! -L "$HOME/Library/Logs/JetBrains" ]
then
rm -rf "$HOME/Library/Logs/JetBrains"
ln -s "$CACHEDIR/JetBrains/Logs" "$HOME/Library/Logs/JetBrains"
fi
# /usr/bin/chflags hidden "/Volumes/RAMD" #Hide RAMD volume
fi
unset CACHEDIR
EOF
fi
if [ -f "$FILE" ]; then
sudo defaults write com.apple.loginwindow LoginHook "$FILE"
fi
chmod a+x $FILE
unset FILE
@Waitsnake
Copy link

Waitsnake commented Sep 18, 2022

Yes, that seems to be the case that this is no longer working for Chrome/Safari. According to this Japanese here it is because of the sandboxing around Safari and Chrome are now even more strong so that they even block symlinks going out of the sandbox allowed paths. And symlinks leading to a Ramdisk under /Volumes folder are blocked (if the google translation of his site is correct). So he created two separat Ramdisks (one for Chrome and one for Safari) directly at paths where the caches are placed.
${HOME}/Library/Caches/Google
${HOME}/Library/Containers/com.apple.Safari/Data/Library/

I just tired the script for Safari 16 only (I comment out the Chrome part in the script since I don't use Chrome) and according to FSMonitor it seems to work.

Here is the Japanese website with the article and the bash script:
https://sakura.lazycat.info/archives/1018.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment