Use gdrive from command line

There is no need for a web browser

Posted by rgalindor on August 13, 2021 · 5 mins read

So, do you think that Google Drive is a tool to share only .docx, .xlsx, and .pptx files? Well, yes, but it allows you to share even files with a big volume across different devices. I used to believe that one limitation for it is the need for a web browser in any device, but I was so wrong.

You only need two things to share big files:

  1. A google account.
  2. An internet conection.

Maybe you are aware that on mobile devices you can access files through mobile apps. So it is too in a Linux server without a graphical interface at all. There is a gdrive command-line utility that allows you to interact with Google Drive. This is especially useful when you connect to remote servers through ssh and you have no graphical interface but your command line.

Device   Best utility
Mobile device   App
Personal computer   Web app
Linux server   Command-line utility

Install gdrive

This utility can be installed by cloning the GitHub repo, then compiling it, and so on. However, the simplest way to install it is through a conda installation:

conda create --name gdrive
conda activate gdrive
conda install -c conda-forge gdrive
conda deactivate

And that’s it, you just installed gdrive.

Connect to your account

The first thing you need to do is get inside your custom environment and starting to enjoy:

conda activate gdrive
gdrive list

Then you need to authenticate, to do this you need to copy the url and paste it in the url box within your preferred (and local) web browser. Such link gives you to an authenticate landing page, if you successfully authenticate to your account you will receive a passphrase that you need to copy/paste into the prompt from your command line. Finally, the utility provides you with a list of files in your “root” path in Google Drive.

When you do this, gdrive utility creates auth tokens within your ~/.gdrive/ folder, such files allow the utility to keep logged in and sync your account. So, if you want to perform any other operation you do not need to authenticate again. That is why whether you use a shared server you must clean your auth tokens to avoid security issues:

rm ~/.gdrive/*

Otherwise, you have the access open to list, delete, download, upload, files within your Google Drive Account.

Sync files to your server

You can share files with people, that is so cool. So maybe you have a “Shared with me” folder that is not visible with a simple gdrive list. To have access to such folder, you can perform the following:

gdrive list --query "sharedWithMe"
Id                             Name                    Type   Size     Created
0B3X9GlR6EmbnZ3gyeGw4d3ozbUk   drive-windows-x64.exe   bin    6.6 MB   2015-07-18 16:43:58
0B3X9GlR6EmbnTXlSc1FqV1dvSTQ   drive-windows-386.exe   bin    5.2 MB   2015-10-19 16:43:53
0B3X9GlR6EmbnVjIzMDRqck1aekE   drive-osx-x64           bin    6.5 MB   2018-03-11 16:43:50
0B3X9GlR6EmbnbEpXdlhza25zT1U   drive-osx-386           bin    5.2 MB   2019-07-28 16:43:41
0B3X9GlR6Embnb095MGxEYmJhY2c   drive-linux-x64         bin    6.5 MB   2020-11-17 16:43:38
0B3X9GlR6EmbnMldxMFV1UGVMTlE   linux                   dir             2016-02-21 22:54:00

This operation allows you to display key information about the files within Shared folder, notably, you can view the Id of every item (file), and as you can guess, this is the most important field to perform operations.

Maybe you could inspect the contents of an entire folder, (the linux directory in this example) this is easy:

gdrive list --query "'0B3X9GlR6EmbnMldxMFV1UGVMTlE' in parents"

Perhaps then you could download a file to your remote server (I don’t know, maybe to perform a bioinformatic pipeline). You get it, you need the Id:

gdrive download 0B3X9GlR6EmbnbEpXdlhza25zT1U

And this is how you can transfer somehow big files to your production server!

Photo by Sora Shimazaki from Pexels


← Previous Post Next Post