Skip to content

Instantly share code, notes, and snippets.

@jayotterbein
Last active May 30, 2022 21:35
Show Gist options
  • Save jayotterbein/9b9e9c2d022a7e2c30da494d0b19857b to your computer and use it in GitHub Desktop.
Save jayotterbein/9b9e9c2d022a7e2c30da494d0b19857b to your computer and use it in GitHub Desktop.
Shell script to clone a VMWare fusion VM and rename all the appropriate files
#!/bin/bash
shopt -s nullglob # When expanding wildcards into files, return null when no matches are found
set -e # Abort script on any error
src="Windows 10 x64" # Default source VM
pushd "$HOME/Documents/Virtual Machines.localized" # Avoid full path usage everywhere
if [ $# -eq 0 ]; then
echo "Usage: $(basename $0) newvm"
echo "$src is used for the source vm"
exit
fi
# Add the current date to the vmware destination path
dst="$@ $(date +%Y-%m-%d)"
echo "Cloning: $src -> $dst"
# VMWare ends all VM directories in .vmwarevm
dstDir="$dst.vmwarevm"
srcDir="$src.vmwarevm"
# Ensure the source directory exists
if [ ! -d "$srcDir" ]; then
echo "Unable to find directory '$srcDir'"
exit
fi
# Copy the source vm except logs and caches
rsync -av --delete --progress --exclude="*.log" --exclude="caches" --exclude="Applications" "$srcDir/" "$dstDir"
# Rename any files for the VM
pushd "$dstDir"
find . -name "$src.*" | while read f; do
filename=$(basename "$f")
ext=${filename##*.}
mv -v "$filename" "$dst.$ext"
done
# Fix references in any files to the new names
grep --null -rl -I "$src" * | xargs -0 -n1 -I{} sed -i '' "s#$src#$dst#g" "{}"
# Import the VM into Fusion and show the Finder window
open .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment