Skip to content

Instantly share code, notes, and snippets.

@YuanLiou
Created October 20, 2025 03:33
Show Gist options
  • Select an option

  • Save YuanLiou/ba8d6edaff829b89d0e94a39de8b63db to your computer and use it in GitHub Desktop.

Select an option

Save YuanLiou/ba8d6edaff829b89d0e94a39de8b63db to your computer and use it in GitHub Desktop.
#!/bin/bash
# ==============================================================================
#
# Title: archive_to_gdrive.sh
# Description: Compresses a specified folder, adds a timestamp to the filename,
# and moves the resulting .zip file to a user-defined
# Google Drive folder on macOS.
# Author: Gemini and ray liu
#
# Usage: ./archive_to_gdrive.sh /path/to/your/folder
#
# ==============================================================================
# --- Step 1: Validate Input ---
# Check if a folder path was provided as an argument.
if [ -z "$1" ]; then
echo "❌ Error: No folder specified."
echo "Usage: $0 /path/to/your/folder"
exit 1
fi
SOURCE_FOLDER="$1"
# Check if the provided path is a valid directory.
if [ ! -d "$SOURCE_FOLDER" ]; then
echo "❌ Error: '$SOURCE_FOLDER' is not a valid directory."
exit 1
fi
# --- Step 2: Prepare Filenames and Paths ---
# Remove any trailing slash from the folder path for consistency.
SOURCE_FOLDER_CLEAN="${SOURCE_FOLDER%/}"
# Extract just the folder name from the path.
FOLDER_BASENAME=$(basename "$SOURCE_FOLDER_CLEAN")
# Get the current date and time in YYYYMMDD-HHMMSS format.
TIMESTAMP=$(date +"%Y%m%d-%H%M%S")
# Construct the final filename for the zip archive.
ZIP_FILENAME="${FOLDER_BASENAME}_${TIMESTAMP}.zip"
echo "πŸ“‚ Source folder: $SOURCE_FOLDER_CLEAN"
echo "πŸ“¦ Archive name will be: $ZIP_FILENAME"
echo ""
# --- Step 3: Get Google Drive Folder Location from User ---
# Set a default path for Google Drive.
DEFAULT_GDRIVE_PATH="$HOME/Google Drive/ζˆ‘ηš„ι›²η«―η‘¬η’Ÿ"
GDRIVE_PATH=""
# Check if the default path exists and prompt the user accordingly.
if [ -d "$DEFAULT_GDRIVE_PATH" ]; then
echo "βœ… Default Google Drive folder found at: '$DEFAULT_GDRIVE_PATH'"
read -p "Press ENTER to use this path, or specify a new one: " USER_GDRIVE_PATH
if [ -z "$USER_GDRIVE_PATH" ]; then
GDRIVE_PATH="$DEFAULT_GDRIVE_PATH"
else
GDRIVE_PATH="$USER_GDRIVE_PATH"
fi
else
echo "Please specify your Google Drive folder."
# Provide a helpful tip for macOS users to avoid typing long paths.
echo "πŸ’‘ Tip: Drag and drop your 'My Drive' folder from Finder into this window and press Enter."
read -p "Enter path to your Google Drive folder: " GDRIVE_PATH
fi
# Clean up the path in case it gets wrapped in quotes from drag-and-drop.
GDRIVE_PATH="${GDRIVE_PATH//\'/}"
GDRIVE_PATH="${GDRIVE_PATH//\"/}"
# --- Step 4: Validate Google Drive Path ---
if [ -z "$GDRIVE_PATH" ]; then
echo "❌ Error: No Google Drive path was entered. Aborting."
exit 1
fi
if [ ! -d "$GDRIVE_PATH" ]; then
echo "❌ Error: The specified Google Drive path is not a valid directory."
echo "You entered: '$GDRIVE_PATH'"
exit 1
fi
echo "βœ… Google Drive folder verified: $GDRIVE_PATH"
echo ""
# --- Step 5: Compress and Move ---
echo "βš™οΈ Compressing '$FOLDER_BASENAME'..."
# To handle permissions robustly, we create the zip file locally first,
# in the parent directory of the source folder. This ensures a clean zip structure.
SOURCE_PARENT_DIR=$(dirname "$SOURCE_FOLDER_CLEAN")
ZIP_TEMP_PATH="${SOURCE_PARENT_DIR}/${ZIP_FILENAME}"
# We run zip in a subshell `(...)` to avoid changing the script's working directory.
# We 'cd' into the parent directory and then zip the target folder by its basename.
(cd "$SOURCE_PARENT_DIR" && zip -r -q "$ZIP_FILENAME" "$(basename "$SOURCE_FOLDER_CLEAN")")
# Check the exit code of the zip command to see if it succeeded.
if [ $? -ne 0 ]; then
echo "❌ Error: Compression failed. The script could not read the source folder or write the temporary archive."
exit 1
fi
echo "βœ… Compression successful!"
echo "🚚 Moving '$ZIP_FILENAME' to Google Drive..."
# Now, move the created zip file to its final destination.
mv "$ZIP_TEMP_PATH" "$GDRIVE_PATH/"
# Check the exit code of the move command.
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to move the file to Google Drive. This is likely a permissions issue."
echo "πŸ’‘ Please double-check that Terminal has 'Full Disk Access' in System Settings > Privacy & Security."
# Clean up the temporarily created zip file on failure.
rm "$ZIP_TEMP_PATH"
exit 1
fi
echo "✨ All done! Your archive has been successfully moved to Google Drive."
echo "πŸ“ Final location: $GDRIVE_PATH/$ZIP_FILENAME"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment