59

I want to download the file that is viewable at this address to a linux remote:

https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing

I'm hoping I can do this with wget.

I tried

wget https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/vgg16_weights.h5

and the response was a 404.

Is it possible to wget a google drive file? If so, what is the path to provide? If not, are there any alternatives (bash or other) so I can avoid downloading the file to my local and transferring it to the remote?

3

7 Answers 7

88

Insert your file ID into this URL (https://drive.google.com/uc?export=download&id=), then surround the URL with quotes so that Bash doesn't misinterpret the &, like so:

wget "https://drive.google.com/uc?export=download&id=0Bz7KyqmuGsilT0J5dmRCM0ROVHc"

Reference here.


When downloading big files, Google Drive adds a security warning that breaks the script above. In that case, you can download the file using:

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=FILEID' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=FILEID" -O FILENAME && rm -rf /tmp/cookies.txt

(Script taken from here)

12
  • 40
    Google Drive adds a security warning if the file is large. Then the given solution does not work anymore. Does someone has a solution for this?
    – pltrdy
    Jan 10, 2017 at 14:25
  • 3
    For those using curl, the above also works, e.g.: curl -L -o file.out 'https://drive.google.com/uc?export=download&id=0Bz7KyqmuGsilT0J5dmRCM0ROVHc'. Note that the -L command-line option is needed for following Google's redirects.
    – Castaglia
    Feb 27, 2017 at 15:39
  • 4
    Not working even for small files, anymore. gives me a 404
    – Irtaza
    Apr 17, 2017 at 12:33
  • 17
    Just use confirm=yes. wget "drive.google.com/u/3/uc?id= 0Bz7KyqmuGsilT0J5dmRCM0ROVHc&export=download&confirm=yes" Apr 20, 2022 at 20:26
  • 6
    Confirmed, adding &confirm=yes works! @mircobabini
    – Annahri
    Jun 6, 2022 at 7:05
20

The shortest way I have found for downloading big files:

git clone https://github.com/chentinghao/download_google_drive.git
cd download_google_drive/
python download_gdrive.py FILE_ID DESTINATION_PATH

Just view access, FILE_ID and DESTINATION_PATH (including file name) are required. It's working now as of November/12/2019 and has been for more than a year.

Another solution is googleapiclient. It enables you to automate downloading/uploading private files having their ids.

Note: Also make sure that file is publicly shared not with a group or within an organization

2
  • It works July 2021, an example of the above ought to added as it will make things more clear. I got confused with DESTINATION_PATH I just added the path not file.extension Jul 6, 2021 at 13:22
  • Does this work with entire folders?
    – Fred
    Mar 13, 2022 at 16:08
6

First, click the share button in the top right corner, and set the permission to allow anyone with the link can view.

Click File->Download as-> PDF Document(.pdf) in the left cornel, and start to download by browser.

Find the URL in downloader, chrome is in chrome://downloads/.

The URL link by the time I am writing this answer is https://docs.google.com/document/export?format=pdf&id=xxx&token=xxx&includes_info_params=true

I was able to download as pdf with wget by wget -O xxx.pdf "https://docs.google.com/document/export?format=pdf&id=xxx"

0
6

This solution works even for large files, without cookies.

The trick is the &confirm=yes query param. Then:

wget "drive.google.com/u/3/uc?id= 0Bz7KyqmuGsilT0J5dmRCM0ROVHc&export=download&confirm=yes"
1
  • Still works in late April 2023
    – El Bachir
    Apr 28 at 8:51
4

This works for me: https://www.matthuisman.nz/2019/01/download-google-drive-files-wget-curl.html Google shows warning if file is larger then 100MB, so this case should be handled separately. Author shows how to do that, and provides script to automate it: https://github.com/matthuisman/gdrivedl

0
2
  1. Find the ID of the file in the shareable link:

https://drive.google.com/file/d/ID/view?usp=sharing

  1. Download the file with curl -L using the aforementioned ID:
!curl -o out -L 'https://drive.google.com/uc?export=download&confirm=yes&id=ID'
  1. Rename the downloaded file from out to your filename of preference.
1

Here is an answer that has worked for me for large files as of August 10, 2023. The source is from this website. In order to use this command, you must make sure that your file is available to anyone on the internet.

Merely run 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='$FILEID -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=$FILEID" -O $FILENAME && rm -rf /tmp/cookies.txt

Here, FILEID is the ID from the download URL (right-click file >> Share >> Copy link), and FILENAME is the name of the file. It will download the file (even if it's large) to the folder that you run the script with the name of the file as the name of the downloaded file.

If you'd like this as a Bash script (written by GPT-4, but it's tested and works), you can save this as download.sh, make it runnable with chmod +x download.sh, and then run it with ./download.sh.

#!/bin/bash

# Ask for file id
echo "Please enter the file id:"
read FILEID

# Ask for filename
echo "Please enter the filename:"
read FILENAME

# Run the wget command
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='$FILEID -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=$FILEID" -O $FILENAME && rm -rf /tmp/cookies.txt

In addition, here's a (GPT-4-generated) breakdown of each portion of the command:

1. `wget`: This is a free utility for non-interactive download of files from the web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.

2. `--load-cookies /tmp/cookies.txt`: This option tells wget to load cookies from the file `/tmp/cookies.txt`.

3. `"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='$FILEID -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=$FILEID"`: This is the URL to download the file from. It includes a sub-command that uses wget to fetch a confirmation token needed for the download.

4. `--quiet`: This option tells wget to work quietly, i.e., without displaying any output.

5. `--save-cookies /tmp/cookies.txt`: This option tells wget to save cookies to the file `/tmp/cookies.txt`.

6. `--keep-session-cookies`: This option tells wget to keep session cookies. These are cookies that are deleted when the browser is closed.

7. `--no-check-certificate`: This option tells wget not to check the server certificate against the available certificate authorities.

8. `-O-`: This option tells wget to write the documents to standard output.

9. `| sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p'`: This part uses the sed command to extract the confirmation token from the output of the previous wget command.

10. `-O $FILENAME`: This option tells wget to write the downloaded file to a file named `$FILENAME`.

11. `&& rm -rf /tmp/cookies.txt`: This part of the command deletes the `/tmp/cookies.txt` file if the previous commands were successful. The `&&` operator only runs the command to its right if the command to its left was successful. The `rm -rf /tmp/cookies.txt` command removes the file `/tmp/cookies.txt`.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.