Download Google Drive Files with wget or curl

Often I find myself needing to download google drive files on a remote headless machine without a browser.

Below are the simple shell commands to do this using wget or curl.

Small file = less than 100MB
Large File = more than 100MB (more steps due to no virus scan confirm)

The text in red is what needs changing for the particular file you want to download.

The fileid can be found in the google url of the file you want to download.
eg:
  • https://drive.google.com/open?id=1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_
  • https://drive.google.com/file/d/1yXsJq7TTMgUVXbOnCalyupESFN-tm2nc/view?usp=sharing
Set the filename to anything you like (most likely the origin files name).

I have also now created an easy to use bash script.
See the bottom of the post for info about it.

This will only work with shared google drive files.

wget (Small File)

cd ~

export fileid=1yXsJq7TTMgUVXbOnCalyupESFN-tm2nc
export filename=matthuisman.jpg

wget -O $filename 'https://docs.google.com/uc?export=download&id='$fileid

wget (Large File)

cd ~

export fileid=1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_
export filename=combian.rar

wget --save-cookies cookies.txt 'https://docs.google.com/uc?export=download&id='$fileid -O- \
     | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1/p' > confirm.txt

wget --load-cookies cookies.txt -O $filename \
     'https://docs.google.com/uc?export=download&id='$fileid'&confirm='$(<confirm.txt)

rm -f confirm.txt cookies.txt

curl (Small File)

cd ~

export fileid=1yXsJq7TTMgUVXbOnCalyupESFN-tm2nc
export filename=matthuisman.jpg

curl -L -o $filename 'https://docs.google.com/uc?export=download&id='$fileid

curl (Large File)

cd ~

export fileid=1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_
export filename=combian.rar

curl -L -c cookies.txt 'https://docs.google.com/uc?export=download&id='$fileid \
     | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1/p' > confirm.txt

curl -L -b cookies.txt -o $filename \
     'https://docs.google.com/uc?export=download&id='$fileid'&confirm='$(<confirm.txt)

rm -f confirm.txt cookies.txt

Bash Script

I have written a quick bash script utility that automatically does the above.
It requires wget. Use at your own risk.
wget -O /usr/sbin/gdrivedl 'https://files.matthuisman.nz/gdrivedl'
chmod +x /usr/sbin/gdrivedl
Below are some examples on the different URLS it will work with
gdrivedl https://drive.google.com/open?id=1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_
gdrivedl https://drive.google.com/file/d/1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_/view?usp=sharing
gdrivedl 1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_
Any of those will download the file to a tmp file in the current directory.
It will then attempt to get the filename from the requests headers and then rename the file.

You can also specify your own file name / path as a 2nd argument.
gdrivedl https://drive.google.com/open?id=1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_ /tmp/my_file.rar

Support

Social