Google Drive: How to copy shared folder content to another folder

backupgoogle-drivegoogle-searchsync

I have access to a folder shared with me (let's say SharedWithMe folder) and I want to backup this folder as the sharing will be end in the future. So, is it possible to sync / copy the SharedWithMe folder content with / to another folder (let's say MyBackup) in y Google Drive?

Best Answer

You can not copy shared folders directly, as of yet. For this purpose you'll need to use Google Colaboratory:

  1. Create a new Notebook on Colab
  2. First You'll need to mount your drive as so:
from google.colab import drive
drive.mount('/gdrive')

This asks for a authorisation code.
3. Now, on a separate tab, open your Google Drive and go to Shared with Me. Right click and click 'Add Shortcut to Drive'. This shortcut is temporary and can be deleted later.
4. Type %cd /gdrive/MyDrive/<path-to-the-shortcut> to go to that location and then type pwd to get the path.
5. Execute !cp -r 'above-path/.' '/gdrive/My Drive/<destinantion>'

OR you can avoid all that and simply execute this:

#@title Deeply copy shared folders in Google Drive
from google.colab import drive
import os

print('Mounting Google Drive...')
drive.mount('/gdrive')

src_path = '/gdrive/MyDrive/DE A1' #@param {type: 'string'}
assert os.path.exists(src_path), f"Source '{src_path}' doesn't exist!"

target_path = '/gdrive/MyDrive/Language/German' #@param {type: 'string'}
os.makedirs(target_path, exist_ok=True)
assert os.path.exists(target_path), f"Target '{target_path}' doesn't exist!"

target_path = os.path.join(target_path, os.path.basename(src_path))
print(f'Copying from "{src_path}" to "{target_path}"...')
os.makedirs(target_path, exist_ok=True)
!cp -rf "$src_path"/* "$target_path"  # also work when source is a shortcut
Related Question