Skip to content

Instantly share code, notes, and snippets.

@HoneyBearCodes
Created October 21, 2025 04:53
Show Gist options
  • Select an option

  • Save HoneyBearCodes/a0c0a97999ec38794598f414f74d87e6 to your computer and use it in GitHub Desktop.

Select an option

Save HoneyBearCodes/a0c0a97999ec38794598f414f74d87e6 to your computer and use it in GitHub Desktop.
A self-contained Bash script that downloads files directly from Google Drive by bypassing the web confirmation layer, with automatic filename extraction and URL decoding.
#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <google-drive-url>"
exit 1
fi
URL="$1"
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT
COOKIE="$TMPDIR/cookies.txt"
HDR1="$TMPDIR/h1.txt"
BODY1="$TMPDIR/b1.html"
HDR2="$TMPDIR/h2.txt"
BODY2="$TMPDIR/b2.bin"
# Extract Google Drive file ID from URL
extract_id() {
local u="$1"
if [[ "$u" =~ /d/([A-Za-z0-9_-]+) ]]; then
echo "${BASH_REMATCH[1]}"
return
elif [[ "$u" =~ id=([A-Za-z0-9_-]+) ]]; then
echo "${BASH_REMATCH[1]}"
return
fi
echo ""
}
ID="$(extract_id "$URL")"
if [[ -z "$ID" ]]; then
echo "[-] Could not extract file ID from URL."
exit 1
fi
BASE="https://drive.google.com/uc?export=download&id=${ID}"
echo "[+] Requesting: $BASE"
# Initial download attempt
curl -sSL -D "$HDR1" -b "$COOKIE" -c "$COOKIE" -o "$BODY1" "$BASE"
# Detect and handle Google Drive confirmation pages (large file or warning)
if grep -q "<html" "$BODY1" || grep -qi "uc-download-link" "$BODY1"; then
echo "[!] Detected HTML warning page — extracting real download link..."
# Try to find direct confirmation link in HTML
CONFIRM_URL=$(grep -oE 'https://[^"]+confirm=[^"]+' "$BODY1" | head -n1 || true)
# Fallback: parse form-based confirmation if direct link not found
if [[ -z "$CONFIRM_URL" ]]; then
FORM_ACTION=$(grep -oP '<form[^>]*action="\K[^"]+' "$BODY1" | head -n1 || true)
CONFIRM=$(grep -oP 'name="confirm"\s+value="\K[^"]+' "$BODY1" | head -n1 || true)
if [[ -n "$FORM_ACTION" && -n "$CONFIRM" ]]; then
CONFIRM_URL="${FORM_ACTION}?confirm=${CONFIRM}&id=${ID}&export=download"
[[ "$CONFIRM_URL" =~ ^https? ]] || CONFIRM_URL="https://drive.google.com${CONFIRM_URL}"
fi
fi
# Abort if confirmation link extraction fails
if [[ -z "$CONFIRM_URL" ]]; then
echo "[-] Could not parse confirmation link."
exit 1
fi
echo "[+] Found download URL, fetching actual file..."
curl -sSL -L -D "$HDR2" -b "$COOKIE" -c "$COOKIE" -o "$BODY2" "$CONFIRM_URL"
else
# If no confirmation needed, just reuse the first response
mv "$BODY1" "$BODY2"
cp "$HDR1" "$HDR2"
fi
# Extract filename from HTTP headers
FILENAME=$(grep -i 'Content-Disposition:' "$HDR2" |
sed -E 's/.*filename\*?=([^;]+)/\1/' |
sed -E "s/(UTF-8'')?['\"]?([^'\"]+)['\"]?/\2/" |
tr -d '\r' |
tail -n1)
# Default fallback filename
if [[ -z "$FILENAME" ]]; then
FILENAME="downloaded_file"
fi
# Decode URL-encoded filename if Python is available
if command -v python3 >/dev/null 2>&1; then
FILENAME=$(python3 -c "import urllib.parse,sys;print(urllib.parse.unquote(sys.argv[1]))" "$FILENAME")
fi
# Save final output file
mv "$BODY2" "./$FILENAME"
echo "[+] Saved file as: $FILENAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment