1. Introduction
In today’s digital era, cloud storage solutions have become integral to daily life. They provide a convenient and secure way to store, manage, and share our data. Google Drive is one of the most popular and widely used cloud storage solutions, offering 15GB of free storage. Despite the user-friendly interface of Google Drive, downloading a folder from it using the command line can pose a bit of a challenge.
In this tutorial, we’ll learn to download an entire folder from Google Drive using the command line. In particular, we’ll employ the gdown, gdrive, and rclone command-line tools to download a folder from Google Drive.
Some of these tools require that the authentication parameters be preset first, while others can be used without any prior configuration.
2. gdown
gdown, a Python program, provides the ability to download publicly available files and folders from Google Drive. Initially, it was designed to download large files from Google Drive and developed further to support downloading folders as well.
Before downloading a folder with gdown, we need to set the sharing permission of our target folder as Anyone with the link.
We can install the gdown program from the package installer for Python (pip):
$ pip install gdown
gdown provides several options to download files and folders. Let’s download a folder using the gdown program using the sharing link of our target folder from Google Drive:
$ gdown --folder https://drive.google.com/drive/folders/OUR-DIRECTORY-ID
In this example, we download a folder using the gdown command, where –folder specifies the downloading of a folder, followed by the target folder link.
We can use only the folder ID instead of the complete folder link for downloading a folder. In addition, we can also specify the download path for a folder using the -O option:
$ gdown --folder OUR-DIRECTORY-ID -O /home/Baeldung/
This command downloads our target folder to /home/Baeldung/.
3. gdrive
gdrive is a command-line application to interact with Google Drive. In addition to downloading and uploading options, it also provides a variety of other options like managing accounts and file permissions.
3.1. Download and Installation
We can download the binaries of gdrive from the GitHub of gdrive and store the binary within reach of our PATH variable, i.e., in /usr/local/bin, after unpacking:
$ wget https://github.com/glotlabs/gdrive/releases/download/3.9.0/gdrive_linux-x64.tar.gz
[...]
$ tar -xvzf gdrive_linux-x64.tar.gz
$ cp ./gdrive /usr/local/bin/
The wget command downloads the gdrive binary files from GitHub in the tar.gz format, and the tar -xvzf command unpacks the downloaded TAR Gzip file. Lastly, the cp command copies the unpacked file to a PATH binary directory.
3.2. Obtaining Google Drive API Credentials
Before starting to use gdrive, we need to configure the Google gdrive account. For that, we require Google Drive API credentials.
Thus, we prepared a brief guide to obtain the Google Drive API credentials:
- Go to Google Cloud Console and create a new project
- Search for and enable Google Drive API in the marketplace
- Click on the OAuth consent screen button from the Credentials menu
- Select External and click CREATE
- Start with a unique App name and fill out all the required fields
- Click on the SAVE AND CONTINUE button
- Click on the ADD OR REMOVE SCOPES button and search for google drive api
- Set the scopes according to the needs and click on UPDATE button
- Click on the SAVE AND CONTINUE button
- Click on the ADD USERS button to add the email to use with gdrive
- Click the ADD button
- Click on the SAVE AND CONTINUE button
- After reviewing, click on the BACK TO DASHBOARD button
- Click on the CREATE CREDENTIALS button from the Credentials menu and select OAuth client ID
- Select Desktop app as the application type and give a name
- Once we click on the CREATE button, we will be presented with a Client ID and Client Secret
These instructions are provided in detail on the GitHub documentation of gdrive. We’ll use this Client ID and Client Secret to configure the gdrive.
3.3. Using gdrive
Once we have the credentials, we can add the Google account to gdrive:
$ gdrive account add
This command prompts for the Google Client ID and Client Secret that we just created. Afterward, we’ll be presented with a URL, which asks for approval for gdrive to access our Google Drive. We are all set after granting access to gdrive.
Let’s download a folder using gdrive:
$ gdrive files download --recursive OUR-DIRECTORY-ID
This command downloads the folder with the given folder ID. Let’s understand each option used in this command:
- gdrive is the main command to interact with Google Drive
- files is a sub-command that instructs the gdrive to manage files and folders
- download option tells gdrive to download files and folders
- –recursive option tells gdrive to download files and folders recursively
Currently, the gdrive files download command works only for files with binary content, i.e., images, videos, and compressed files, and not Google Docs. To download Google Docs files, we can use the export option instead of download:
$ gdrive files export OUR-DIRECTORY-ID /home/Baeldung/Sample.docx
This command exports the Google Docs file with the given file ID at the given local path, i.e., /home/Baeldung/Sample.docx. We can also overwrite the local files and folders if they already exist using the –overwrite option.
4. rclone
rclone is a command-line tool to manage files on cloud storage including Google Drive and Microsoft OneDrive.
To begin with, we can install rclone using the official script:
$ sudo -v ; curl https://rclone.org/install.sh | sudo bash
This command installs the rclone command line tool. Let’s take a close look at this command:
- sudo runs the command with superuser privileges
- -v option extends the timeout
- the semicolon (;) separates the two commands
- curl downloads the install.sh script from the rclone website
- | sends the output of the curl command to the next command
- sudo bash runs the install.sh script with superuser privileges
Next, we should configure rclone before its first use. A brief reference guide is available for the rclone configuration. We’ll configure rclone using the instructions from the reference guide with the Client ID and Client Secret from the Google API.
rclone uses file and folder paths instead of IDs to download files from the configured cloud storage, in our case, Google Drive:
$ rclone copy gdrive:Baeldung/ /home/baeldung/local/targetDir/
This command downloads the top-level Baeldung folder from Google Drive to our given local path. Moreover, rclone also provides functionalities to perform bidirectional synchronization between local and remote storage.
5. Conclusion
In this article, we learned how to download a folder from Google Drive using command-line tools.
Firstly, we discussed gdown which works without any explicit preconfiguration. Secondly, we learned to create Google Drive API credentials. Lastly, we explored the gdrive and rclone tools which require configuration before their first usage.
We used gdown and rclone to download the complete folder while we saw that gdrive downloads the folder with only files having binary content. Furthermore, we also looked at the options provided by gdrive to download Google Docs files.