Skip to content

Instantly share code, notes, and snippets.

@josemarcosrf
Created March 19, 2021 10:11
Show Gist options
  • Save josemarcosrf/b750520b78bfe476bdf24d27c5765783 to your computer and use it in GitHub Desktop.
Save josemarcosrf/b750520b78bfe476bdf24d27c5765783 to your computer and use it in GitHub Desktop.
Download Google Drive file with wget
#!/usr/bin/env bash
set -e
set -o pipefail
say() {
echo "$@" | sed \
-e "s/\(\(@\(red\|green\|yellow\|blue\|magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\4@reset/g" \
-e "s/@red/$(tput setaf 1)/g" \
-e "s/@green/$(tput setaf 2)/g" \
-e "s/@yellow/$(tput setaf 3)/g" \
-e "s/@blue/$(tput setaf 4)/g" \
-e "s/@magenta/$(tput setaf 5)/g" \
-e "s/@cyan/$(tput setaf 6)/g" \
-e "s/@white/$(tput setaf 7)/g" \
-e "s/@reset/$(tput sgr0)/g" \
-e "s/@b/$(tput bold)/g" \
-e "s/@u/$(tput sgr 0 1)/g"
}
if [ -z "$1" ]; then
say @red[["Please set the first parameter as the source file ID to download"]]
exit
else
FILE_ID=$1
fi
if [ -z "$2" ]; then
say @red[["Please set the second parameter as the file NAME to save as"]]
exit
else
FILE_NAME=$2
fi
wget --load-cookies /tmp/cookies.txt \
"https://docs.google.com/uc?export=download&confirm=$( \
wget --quiet --save-cookies /tmp/cookies.txt \
--keep-session-cookies \
--no-check-certificate \
"https://docs.google.com/uc?export=download&id=$FILE_ID" -O- | \
sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p' \
)&id=$FILE_ID" \
-O $FILE_NAME && rm -rf /tmp/cookies.txt
@josemarcosrf
Copy link
Author

Run as:

./gdrive_download <FILE-ID> <FILE-NAME>

The <FILE-NAME> is any name of your choice to save the file locally.

To get the FILE_ID:

  1. Select the file in Google Drive with right click
  2. Click “Share” – you’ll see a modal open up
  3. Click “Advanced” in the bottom right
  4. Next to “Who has access” press “change”
  5. Select “ON – Public on the web” and hit “save”
  6. This will reveal a link for sharing – copy this full link, and paste into a text editor
    You’ll need the part after https://drive.google.com/file/d/ and before /view?usp=sharing for later. This is what we will call FILE_ID

Extracted from: how-to-wget-files-from-google-drive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment