그저 따라하기

구글 드라이브 파일을 wget으로 다운받기

충연 2019. 8. 5. 21:06

Original article: https://www.matthuisman.nz/2019/01/download-google-drive-files-wget-curl.html

Define the fileid (of Google Drive) and the name of the file that will be saved

## Examples
export fileid=1yXsJq7TTMgUVXbOnCalyupESFN-tm2nc
export filename=my_file.hdf5

Small File (less than 100MB)

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

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

Large File (bigger than 100MB)

## WGET
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)


## CURL
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

A quick bash script utility that automatically does the above (based on wget).
https://github.com/matthuisman/files.matthuisman.nz/blob/master/gdrivedl

  1. Download the script and make it executable:

    sudo wget -O /usr/sbin/gdrivedl 'https://f.mjh.nz/gdrivedl'
    sudo chmod +x /usr/sbin/gdrivedl
  1. Some examples of 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.

  1. You can also specify your own output file path as a 2nd argument.

    gdrivedl https://drive.google.com/open?id=1sNhrr2u6n48vb5xuOe8P9pTayojQoOc_ /tmp/my_file.hdf5