Created
March 15, 2024 08:28
-
-
Save kenchou/9cca74c1b52d98940f9fc0ab4d0d2667 to your computer and use it in GitHub Desktop.
Download or export file from GDrive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# ## Dependencies: | |
# brew install gdrive | |
# ## Setup: | |
# gdrive account add | |
# Recursive function to download files in a folder | |
gdrive_download_files() { | |
local folder_id=$1 | |
local parent_path=$2 | |
echo ">>> $folder_id => $parent_path" | |
# List files in the folder | |
gdrive files list --parent "$folder_id" --full-name --skip-header --field-separator , | while IFS=, read -r -a line; do | |
# Get the file ID, name, and type | |
local id="${line[0]}" | |
local name="${line[1]}" | |
local type="${line[2]}" | |
local full_path="${parent_path}/${name}" | |
# Check the file type | |
if [[ "$type" == "document" ]]; then | |
# Check if the file name has an extension | |
if [[ "$name" != *.* ]]; then | |
# If not, add the .xlsx extension | |
full_path="${full_path}.xlsx" | |
fi | |
# Download the file | |
echo "Exporting $id to $full_path..." | |
mkdir -p "$(dirname "$full_path")" | |
gdrive files export "$id" "$full_path" | |
elif [[ "$type" == "regular" ]]; then | |
echo "Download $id to $parent_path..." | |
gdrive files download "$id" --destination "$parent_path" | |
elif [[ "$type" == "folder" ]]; then | |
# Create directory for folder | |
mkdir -p "$full_path" | |
# Recurse into the folder | |
gdrive_download_files "$id" "$full_path" | |
fi | |
done | |
} | |
# Start the download | |
gdrive_download_files "$1" "${2:-'.'}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment