Location>code7788 >text

Use wget to download files in Google Cloud Drive directly

Popularity:536 ℃/2025-02-12 17:45:36

Handle redirection issues: Since large files downloads on Google Drive encounter additional confirmation steps (such as security scans), you may need to bypass this process. If the file is larger (more than 100MB), you may need to passwgetGet a confirmation file first (usuallyconfirm) before continuing to download.

You can use the following script to solve this problem:

#!/bin/bash

# Set file ID and output file nameFILE_ID="1mjIqU-c5q3qMI74XZd3UrkZek0IDTUUh"   # Replace with the actual file IDOUTPUT=""        # Replace with the file name you want to save
# Get the download page and save the responseRESPONSE=$(wget --quiet --save-cookies  \
    --keep-session-cookies --no-check-certificate \
    "/uc?export=download&id=${FILE_ID}" \
    -O -)

# Save the response for debuggingecho "$RESPONSE" > 

# Extract the complete download parametersDOWNLOAD_URL="/download"
ID=$(echo "$RESPONSE" | grep -o 'name="id" value="[^"]*"' | cut -d'"' -f4)
EXPORT=$(echo "$RESPONSE" | grep -o 'name="export" value="[^"]*"' | cut -d'"' -f4)
CONFIRM=$(echo "$RESPONSE" | grep -o 'name="confirm" value="[^"]*"' | cut -d'"' -f4)
UUID=$(echo "$RESPONSE" | grep -o 'name="uuid" value="[^"]*"' | cut -d'"' -f4)

# Check if all parameters have been obtainedif [ -z "$ID" ] || [ -z "$EXPORT" ] || [ -z "$CONFIRM" ] || [ -z "$UUID" ]; then
    echo "Cannot extract all necessary download parameters"
    echo "ID: $ID"
    echo "EXPORT: $EXPORT"
    echo "CONFIRM: $CONFIRM"
    echo "UUID: $UUID"
    exit 1
fi

# Build a complete download URLFULL_URL="${DOWNLOAD_URL}?id=${ID}&export=${EXPORT}&confirm=${CONFIRM}&uuid=${UUID}"

echo "Start downloading files..."
echo "useURL: $FULL_URL"

# Execute the downloadwget --load-cookies  \
    --no-check-certificate \
    "$FULL_URL" \
    -O "$OUTPUT"

# Check whether the download is successfulif [ $? -eq 0 ]; then
    echo "File download successfully:$OUTPUT"
else
    echo "Download failed"
    exit 1
fi

# Clean up temporary filesrm -f 

exit 0

4.How to use

  1. Save the script as a file (for example: download_gdrive.sh)
  2. Modify FILE_ID to the file ID you want to download
  3. Modify OUTPUT to the file name you want to save
  4. Add execution permissions to the script:chmod +x download_gdrive.sh
  5. Run the script:./download_gdrive.sh

This script should be able to correctly handle Google Drive's virus scan warning page and successfully download the file.