Last active
May 30, 2022 21:35
-
-
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
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 | |
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