As a terminal user, I sometime find myself in situations where I need to download files from Google Drive directly into my local machine or a remote server. While Google Drive is a fantastic tool for storing and sharing files, it doesn’t provide a straightforward way to download files via the command line. This can be particularly cumbersome when working with headless servers or when you’re trying to automate your workflow.
To address this challenge, I’ve crafted a Zsh snippet that simplifies the process of downloading files from Google Drive using only the terminal. In this blog post, I’ll share this snippet with you, explain how it works, and provide documentation for its usage.
Google Drive shareable links are designed for use in a web browser. They typically look something like this:
https://drive.google.com/file/d/FILEID/view?usp=sharing
While this link is perfect for sharing, it’s not directly usable in a terminal for downloading the file. The challenge lies in extracting the file ID and filename from the URL and then using them to construct a command that can fetch the file via the command line.
To overcome this challenge, I created a Zsh function that automates the process. I’ve named it gdrive_dl
. Here’s the snippet along with its documentation:
# Function to download files from Google Drive using the shareable link
# Documentation:
# gdrive_dl: A function to download files from Google Drive using the shareable link.
# Usage: gdrive_dl <URL>
# <URL>: The full shareable link of the Google Drive file you wish to download.
function gdrive_dl() {
local URL="$1"
if [[ -z "$URL" ]]; then
echo "Usage: gdrive_dl <URL>"
return 1
fi
# Extract FILEID from the URL
local FILEID=$(echo "$URL" | grep -o 'd/[^/]*' | cut -d'/' -f2)
# Fetch the webpage content and extract the filename using awk
local FILENAME=$(curl -s "$URL" | awk -F'"' '/<meta itemprop="name" content="/ {print $4}')
if [[ -z "$FILENAME" ]]; then
echo "Could not extract the filename from the URL."
return 1
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=${FILEID}' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=${FILEID}" \
-O "${FILENAME}" && rm -rf /tmp/cookies.txt
}
grep
and cut
commands.curl
to fetch the webpage content and awk
to parse the HTML and extract the filename. This step is crucial because the filename is not part of the URL but is embedded within the webpage’s metadata.wget
command that handles the download. It manages cookies and session data to mimic a browser download, ensuring that Google Drive’s download confirmation is appropriately handled.Let’s detail a bit more the core of the logic:
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}" \a -O "${FILENAME}" \
&& rm -rf /tmp/cookies.txt
Initial Request and Cookie Handling:
wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=${FILEID}' -O-
--keep-session-cookies
ensures that session cookies are preserved, which are necessary for maintaining the session state with Google Drive (This is important because Google Drive uses cookies to manage downloads and confirmations.). --save-cookies /tmp/cookies.txt
saves the cookies from the HTTP response to a temporary file.--no-check-certificate
bypasses SSL certificate validation.https://docs.google.com/uc?export=download&id=${FILEID}
is the direct download link for the file, where ${FILEID}
is the variable holding the extracted file ID.-O-
directs wget
to write the output to standard output (stdout) instead of a file.Extracting the Confirmation Token:
sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p'
sed
command is used here to extract the confirmation token from the response. Google Drive requires a confirmation token for downloads, which is provided in the response to the initial request.'s/.*confirm=([0-9A-Za-z_]+).*/\1\n/p'
is used to find the confirmation token in the response and output it.Downloading the File:
wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(...)&id=${FILEID}" -O "${FILENAME}"
wget
command that downloads the file.--load-cookies /tmp/cookies.txt
loads the cookies saved from the initial request. This is necessary to maintain the session and provide the confirmation token.-O "${FILENAME}"
specifies the output filename for the downloaded file.Cleanup:
rm -rf /tmp/cookies.txt
In summary, this part of the function handles the interaction with Google Drive’s download mechanism, managing cookies and confirmation tokens to successfully download the requested file.
To use this function, simply source it in your .zshrc
file or any Zsh configuration file you use. Then, you can download a file from Google Drive by calling the function with the shareable link:
gdrive_dl https://drive.google.com/file/d/FILEID/view?usp=sharing
This Zsh snippet, gdrive_dl
allows me to download files from Google Drive with ease directly from the terminal. I hope you find this snippet as useful as I have, and I encourage you to integrate it into your command-line toolkit.