How to download a Google Drive url via curl or wget
Simplest and best way (with a real Google Drive file example)
Install gdown using
pip
- Command -
pip install gdown
- Command -
Let's say I wish to download cnn_stories.tgz from Google Drive
- Download Link:
https://drive.google.com/uc?export=download&id=0BwmD_VLjROrfTHk4NFg2SndKcjQ
- Download Link:
Please note the
id
URL parameter0BwmD_VLjROrfTHk4NFg2SndKcjQ
in the linkThat's it! Download the file using
gdown
gdown --id 0BwmD_VLjROrfTHk4NFg2SndKcjQ --output cnn_stories.tgz
TLDR: gdown --id {gdrive_file_id} --output {file_name}
Command Line Args:
--id : Google drive file ID
--output: Output File name
How about this method? When the file is such large size, Google returns a code for downloading the file. You can download the file using the code. When such large file is downloaded using curl, you can see the code as follows.
<a id="uc-download-link" class="goog-inline-block jfk-button jfk-button-action" href="/uc?export=download&confirm=ABCD&id=### file ID ###">download</a>
The query with confirm=ABCD
is important for downloading the file. This code is also included in the cookie. At the cookie, you can see it as follows.
#HttpOnly_.drive.google.com TRUE /uc TRUE ##### download_warning_##### ABCD
In this case, "ABCD" is the code. In order to retrieve the code from the cookie and download the file, you can use the following script.
Sample script :
#!/bin/bash
fileid="### file id ###"
filename="MyFile.csv"
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename}
If this was not useful for you, I'm sorry.
Updated at February 17, 2022
Recently, it seems that the specification of this flow has been changed. So I updated this answer. In order to download a publicly shared file of large size from Google Drive, you can use the following script.
#!/bin/bash
fileid="### file id ###"
filename="MyFile.csv"
html=`curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}"`
curl -Lb ./cookie "https://drive.google.com/uc?export=download&`echo ${html}|grep -Po '(confirm=[a-zA-Z0-9\-_]+)'`&id=${fileid}" -o ${filename}
In this case, the ID for downloading is retrieved from the HTML data as follows.
<form id="downloadForm" action="https://drive.google.com/uc?export=download&id={fileId}&confirm={value for downloading}" method="post">
When you want to download a publicly shared file of small size from Google Drive, you can use the following command.
curl -L "https://drive.google.com/uc?export=download&id=### fileId ###" -o sampleoutput.csv