Guide on How to Download Google Drive Folder by Using Command Line

Guide on How to Download Google Drive Folder by Using Command Line

Author: HostZealot Team
2 min.

Cloud-based storage has proven itself to be much more convenient than its alternatives. The most renowned cloud storage solution is Google Drive, and there are reasons for it. Google Drive is a free (up to 15 GB) solution that supports remote access and multiple document formats. Plus, it has an intuitive user interface, which makes using the service easy. However, in our experience, when it comes to using its extended functionality, there can be a challenge.

For example, you might want to download a folder through the command line, but, in our experience, it appears to be not as straightforward as you thought.

In this article, we will help you find your way through the download process of a Google Drive folder using the command line tools.

Exploring gdown

If you want to use a command-line approach to download public files and folders from Google Drive onto your device on Linux OS, you can use gdown.

gdown is a tool designed to facilitate fast downloads outside of a graphical user interface. The thing is that in the Linux environment operating through GUI is not always possible. In such cases, gdown is a useful tool for acquiring files from Google Drive through simple command-line combinations.

gdown was built on Python and was designed to support large Google Drive file downloads.

To start using gdown, you need to download it. But before we tell you how to do that, make sure that the folder you want to download is publicly available with the sharing setting - “Anyone with the link”.

Now, we can proceed with installing gdown through the Python package installer (pip):

$ pip install gdownCopy

The program offers you several ways to download files from Google Drive. One of them is using the link-sharing feature on the folder we need to download:

$ gdown --folder

Here, you directly insert the folder’s URL to specify the one you want to download. Alternatively, you can use a folder ID and specify the download path:

$ gdown --folder OUR-DIRECTORY-ID -O /home/HostZealot/Copy

In this command, the folder is identified through its directory.

Overview of gdrive

gdrive is an application that connects directly to Google Drive for downloading and uploading files and also managing file access settings.

To download gdrive, we need to go on GitHub and after unpacking it, save the binary within a PATH variable (for example, /usr/local/bin):

$ wget [...] 
$ tar -xvzf gdrive_linux-x64.tar.gz $ cp ./gdrive /usr/local/bin/Cop

wget is here for downloading gdrive files from GitHub; tar -xvzf is for unpacking the downloaded files; cp is for copying the unpacked file into a PATH binary directory.

Acquiring Google Drive API Credentials

To start using gdrive we need to set up a Google gdrive account, which, in turn, requires Google Drive API credentials. And here’s how you can do that:

  • Open Google Cloud Console
  • Press “Create a new project”
  • Then look for Google Drive API and enable it
  • In the Credentials menu, press the “OAuth consent screen” button
  • Choose External and click ”Create”
  • Insert the necessary information, then press “Save and continue”
  • Press the “Add or remove scopes” button and look for Google Drive API
  • Set the scopes and click “Update”, then press “Save and continue”
  • Click on “Add users” and add the email you want to use with gdrive, then press the “Add” and “Save and Continue” buttons
  • Review the information and click “Back to dashboard”
  • In the “Credentials” section click “Create credentials” and select “OAuth client ID”
  • Choose the desktop version of the application and title it

This is a short version of the guide; the full guide is available on GitHub. We will use Client ID and Client Secret to configure the gdrive.

Operating with gdrive

Now that we have credentials, we link gdrive to our Google account:

$ gdrive account addCopy

Next, the link to approve gdrive’s access to Google Drive will appear. Make sure to confirm the access.

Now, we can download a folder from Google Drive:

$ gdrive files download --recursive OUR-DIRECTORY-IDCopy

The folder ID mentioned in a command will direct us to the right folder to download.

For now, we can only download the files containing images, videos, and compressed files with this command; downloading Google Docs is not possible with this command.

To download Google documents, we need to change the command a bit:

$ gdrive files export OUR-DIRECTORY-ID /home/Baeldung/Sample.docxCopy

This command will export files to a local path.

Introduction to rclone

rclone is a tool for managing files stored on cloud-based platforms, including Google Drive. To use rclone we need to install it through the official script:

$ sudo -v ; curl | sudo bashCopy

In this command sudo means user privileges; v extends the timeout; curl downloads the script; sudo bash runs the script with superuser privileges.

After running the mentioned command, we need to configure rclone before using it. In our example, we’ll configure the tool through Client ID and Client Secret from the Google API.

rclone doesn’t use IDs for download; it uses file and folder paths from the configured cloud storage (Google Drive in our case) instead:

$ rclone copy gdrive: HostZealot/ /home/hostzealot/local/targetDir/Copy

In this command, we specify the path a Google Drive folder needs to be downloaded to.

Now, you have completed downloading the folder from Google Drive.

Wrapping Up

In this tutorial, we’ve guided you through how to download a folder from Google Drive using command-line tools. We used gdown and rclone to download the folder, considering that it supports only binary content.

Related Articles