Google Drive on Linux, with auto-update!

Google Drive has become part of my internet existence. It is connected to every phone, tablet, and computer I use. Of course, my favourite OS is Linux, and while I can use Google Drive in a web browser, the convenience of having a synced copy of my files on my computer just isn’t there. It’s been years, and Google still hasn’t bothered to make a UI for us outside-the-boxers.

Enter Grive2. This is a nifty little command line program that will synchronise your Google Drive with your Linux machine, and with a bit of tweaking you can set it up to maintain that synchronisation.

This is for Ubuntu 16.04.

First, let’s get Grieve. Open a terminal and run these commands:

sudo add-apt-repository ppa:nilarimogard/webupd8

sudo apt update

sudo apt install grive

Now, find a spot for your Google Drive folder and create it — whatever name you like is good. My Google Drive folder is in Documents/google-drive.

mkdir ~/Documents/google-drive

cd ~/Documents/google-drive

grive -a

This command instructs grive to query Google Drive for permission to access your Drive folder. You should see something like this:

Please go to this URL and get an authentication code:

https://accounts.google.com/o/oauth2/auth?scope=https[BUNCH OF CODE HERE]

-----------------------
Please input the authentication code here:

Copy everything from “https:” to the end and paste it into the URL of your web browser. If you have more than one Google account, you will see a list of them. Pick which one you want to use. Now you will see a permission page. Grant permission. Finally, you will see a page like this:

Once you paste this code into your terminal and hit the enter key, grive will authenticate and then begin to synchronise your folders. If you have a lot of content, this could take a while. But after the first time, grive will only update changes, and be much quicker.

Okay, so now you have a copy of Google Drive on your Linux machine, but what happens when you change something? Until you open a terminal, switch to your Google Drive folder and type “grive”, nothing. What you need is a way to automate the process. And that’s our friendly neighbourhood cron.

Open a terminal and type:

crontab -a

If you’ve never edited your cron before, you will be asked what editor you want to use. If you know what you want here, select it. Otherwise, select vim (either one), because I will be using those commands below.

Scroll down to the bottom of the text you see, then as far to the right as you can go. Type:

a [ENTER] <– that’s the enter key after the letter “a”

Now type:

MAILTO=”” <– if you want to email the output of your crons, and your system is set up to send email, feel free to put your email address here.

Type:

[ENTER]

then

0 * * * * grive –path /home/[your username]/Documents/[your drive folder name]

The above command will sync your local files with Google Drive once every hour.

Just a warning: the text above is ‘–path’, that is a ‘-‘ followed by another ‘-‘ followed by the word ‘path’. No spaces. Some fonts make the two ‘-‘ look like one long dash. Don’t do that.

There are many options for setting up your cron, but keep in mind that Google does restrict how often you can hit their servers. Once a minute will probably get you kicked, and you’ll have to run grive -a, open a web browser, and get a new key. Since this cron is running in the background, you won’t know this has happened. I like once an hour, but maybe you want half an hour, or every 15 minutes:

0,30 * * * *<– that’s at minute 0 and minute 30 of every hour

0,15,30,45 * * * * <– get the idea? Here’s 4 times an hour, every fifteen minutes.

There are hundreds of permutations for when a cron runs, and it is documented all over the internet. If you want something more exotic, a search will turn up just what you need.

Finally, we need to save your new crontab:

[ESC] [ESC] <– hit the escape key twice

:wq [ENTER]

This last command will save your crontab and exit you back to your terminal. You can do a little test to see if the cron is working by adding a small text file to your local Google Drive folder, waiting until the time has passed (give it an extra couple minutes for the sync to happen), then open Google Drive in your web browser and see if the file has been added there. If you see it, success! If not, go over the steps above and make sure everything is done correctly.

Happy syncing!

 

Leave a Reply

Your email address will not be published. Required fields are marked *