How to access your Google Drive account from Linux command line using Gdrive
While Google Drive is no doubt one of the most popular (if not the most popular) cloud storage services available today, what’s really sad is that there is no official Drive client available for Linux. But that doesn’t mean there are no alternatives – in fact the awesome Linux/open-source community has developed several unofficial Google Drive clients, some of which we’ve already discussed here at HowtoForge.
Expanding our coverage on such clients, in this article we will discuss another tool, dubbed Gdrive. Please note that all the instructions and commands mentioned in this write-up have been tested on Ubuntu 16.04.
Gdrive is basically a tool that lets you access your Google Drive account through the command line. Yes, that means you need to run commands for any and every Drive operation that you want to perform. But worry not, it’s not at all difficult to use and the commands aren’t difficult to remember either.
Gdrive Download, Installation, and Setup
You can easily download and install Gdrive on your Linux box by heading to the tool’s official GitHub page, and downloading the executable file for your system. For example, I downloaded a file named gdrive-linux-386 as I work on a 32-bit Linux distribution.
Once downloaded, make sure that the file has executable permissions. If not, grant it the permissions through the chmodcommand. For example, I used the following command:
chmod +x gdrive-linux-386
After this, you need to make sure that the tool is allowed to connect to your Google Drive account. For this, you need to run the executable file with about argument. Here’s the command:
You will be asked to enter a verification code, which is obtained by heading to the url printed in the command’s output and authenticating with the google account for the drive you want access to. The following screenshots will make the process clear:
Clicking the ‘Allow’ button will produce the required code on the web page itself. After the verification code is entered on the command line terminal, Gdrive will connect to your Google Drive account. For example, here is the complete output after I entered the code in my case:
Important note: The aforementioned authentication process will create a token file inside a folder – named .gdrive – located in your home directory. “Note that anyone with access to this file will also have access to your google drive,” the tool’s official documentation says.
Here are examples of some of the basic Google Drive operations that you can carry out using Gdrive. I am assuming that the Gdrive executable that you download on your system is also named gdrive-linux-386 and is kept in your current working directory.
To list the contents (files and directories) of your Google Drive, you can use the following command:
If you want to limit the maximum number of files that can be displayed in the output (default is 30), you can add the -mflag followed a number that represents the new maximum limit. For example:
./gdrive-linux-386 list -m 10
You can also add a query for filtering the output based on your requirement. For example, If I only want specific files (say, that contain ‘Gmail’ in their name) in the output, I can use the following command:
./gdrive-linux-386 list -m 10 --query "name contains 'gmail'"
Here’s the output of the above command on my system:
Download and upload files
Given that Google Drive is a cloud storage service, so needless to say that you’ll want to use Gdrive to upload and download files to/from your Drive account. Here’s how you can do these operations:
To upload a file, run the following command:
./gdrive-linux-386 upload [file-name]
To upload a file to a specific directory, use the following command:
./gdrive-linux-386 upload --parent [dir ID] [filename]
The directory ID ([dir ID] in the command above) can be obtained from the entry of that particular directory when you list your Drive contents using Gdrive.
To create a new directory in your Google Drive, use the following command:
./gdrive-linux-386 mkdir [directory-name]
Of course, you can create a new directory and then use its ID to upload a file to it.
Finally, you can use the following command to download a file from your Google Drive account to your system:
./gdrive-linux-386 download [file-name]
There are several flags that help you with your download operations. For example, -f tells Gdrive to overwrite existing file of the same name (if any), -r tells the tool to download a directory recursively, and –delete makes sure the remote file gets deleted when download is successful. It’s worth mentioning that the last two flags also work with upload operations.
Gdrive also lets you share your Google Drive files with others. For example, if you want to make a file readable to everyone, you can do it through the following command:
./gdrive-linux-386 share [file ID]
Of course, that’s not what we always want. Mostly, we share files with our friends or colleagues. So, for example, if you want to share a file with someone you know, you can do so using the following command:
./gdrive-linux-386 share [file ID] --type user --email [email add]
The above command will grant reader permission to your friend, whose email address you have to provide in place of [email add]. If you want to grant, say, writing or commenting permission, or even want to transfer the ownership, then you’ll have to use the –role flag. Accepted values for this flag include ‘owner’, ‘writer’, ‘commenter’, and ‘reader’.
Here’s an example of how I used this command to share a file with one of my friends as well as grant write permission to them.
Show file info
Gdrive also lets you access information related to any of your Google Drive files. Here’s the command for it:
./gdrive-linux-386 info [file ID]
Here’s an example:
For more Gdrive commands as well as details on how the tool works, head to the project’s official GitHub page.