Skip to content

Instantly share code, notes, and snippets.

@wernerb
Last active September 4, 2023 18:02
Show Gist options
  • Save wernerb/7864141 to your computer and use it in GitHub Desktop.
Save wernerb/7864141 to your computer and use it in GitHub Desktop.
Sync folder with Google Drive. Recursively downloads with wget and Google Drive public links. Used to sync ebooks to my eReaders (Kobo). Be warned that public links are dangerous as they could allow someone to download your files. Be sure to use this only in secure environments. This has been tested to be used in POSIX shells as well as in bash.
#!/bin/sh
#input sharelink here. Example: https://drive.google.com/folderview?id=0B1g-MbiD2F6vdtOT92b3MoerO&usp=sharing
SHARELINK="https://drive.google.com/folderview?id=idU&usp=sharing"
DESTINATION="/full/path/to/folder"
# Change following to false when you don't want to delete files when they are missing from google drive. This can
REMOVEFILES=true
# Begin code
download () {
url=$1
folder=$2
changed=$3
wgetoutput="`wget --no-check-certificate -qO- $url 2>&1`"
mkdir -p "$folder"
commands="$(echo "$wgetoutput" | grep -Eo '\[,,\"(.*?)\",,,,,\"(.*?)\"')"
missingfiles="$(find "$folder" -type f -depth 1 -print)"
missingdirectories="$(find "$folder" -type d -depth 1 -print)"
#Go to next
subfolders="$(echo "$wgetoutput" | grep -Eo '\[,".*?\",\".*?folder' | cut -c 4- | cut -d\" -f 1)"
IFS="
"
for subfolder in $subfolders; do
name=$(echo "$wgetoutput" | grep -Eo 'entry-'$subfolder'.*?</div></div><div class=\"flip-entry-title\">(.*?)</div>' | grep -Eo 'entry-title">.*?</div>$' | cut -c 14- | sed 's/<\/div>//')
newdest="$2/$name"
escapedfile="$(echo "$newdest" | sed -e 's/[]\$*.^|[]/\\&/g')"
missingdirectories=$(echo "$missingdirectories" | sed -e "s@$escapedfile@@g" | sed '/^$/d')
output=$(download "https://drive.google.com/folderview?id=$subfolder&usp=sharing" "$newdest" $changed)
changed=$output
done
#Download files
echo "Downloading for $folder..." 1>&2;
for line in $commands; do
id=$(echo "$line" | grep -Eio '\"[0-9a-z]+-[0-9a-z]+\"' | cut -c 2- | sed s'/.$//')
name=$(echo "$line" | sed -e 's/\[,,\"//' | sed s/\".*//'')
file="$folder/$name"
cmd=$(echo "wget --no-check-certificate -nc -q --no-cookies -O \"$file\" \"https://docs.google.com/uc?export=download&id="$id"\"")
eval "$cmd"
commandresult=$?
if [ "$commandresult" -eq 0 ]; then
echo "Downloaded: $file" 1>&2;
changed=$(($changed + 1))
else
echo "Skipped (Already exists or error): $file" 1>&2;
fi
escapedfile=$(echo "$file" | sed -e 's/[]@$*.^|[]/\\&/g')
missingfiles=$(echo "$missingfiles" | sed 's@'$escapedfile'@@g' | sed '/^$/d')
done
echo "Finished downloading!" 1>&2;
#Remove missing files
if [ $REMOVEFILES = true ]; then
echo "Checking files to remove from $folder..." 1>&2;
i=0
for line in $missingfiles; do
echo "Removing file: $line" 1>&2;
rm -f "$line"
changed=$(($changed + 1))
done
for line in $missingdirectories; do
echo "Removing directory: $line" 1>&2;
rm -rf "$line"
changed=$(($changed + 1))
done
echo "Finished scan!" 1>&2;
fi
echo $changed
}
result=$(download "$SHARELINK" "$DESTINATION" 0)
echo "Changed: $result files/folders"
if [ $result -ne 0 ]; then
echo "Sync made changes to disk!"
fi
@wernerb
Copy link
Author

wernerb commented Dec 10, 2013

Updated: +removes files/directories that are deleted in Google Drive

@ajavij
Copy link

ajavij commented May 25, 2017

I tried to run this script but I'm getting the below error.

find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

@bernardolm
Copy link

I tried to run this script but I'm getting the below error.

find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

This error I could fix moving -depth 1 to before -type. Now, I'm got a error find: paths must precede expression: '1'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment