Skip to content

Instantly share code, notes, and snippets.

@dkobia
Forked from rswgnu/dropbox-fix-conflicts
Last active December 6, 2019 18:15
Show Gist options
  • Save dkobia/057f1b55821462870793f8cc54285837 to your computer and use it in GitHub Desktop.
Save dkobia/057f1b55821462870793f8cc54285837 to your computer and use it in GitHub Desktop.
Recursively moves and replaces conflicted Dropbox files keeping the newer one of each pair. After that is done, it searches for conflicting files and optionally removes those as well.
#!/bin/bash
# Description: Recursively moves and replaces conflicted Dropbox files keeping the newer one of each pair
# Usage: <script-name> - Shows what files would be moved and replaced but does not do any replacements
<script-name> replace - Performs file replacements
# Based on: http://mechanicalscribe.com/notes/fix-dropbox-conflicts-automatically/
# Thx, http://stackoverflow.com/questions/20723868/batch-rename-dropbox-conflict-files
# Location of your local Dropbox folder
DROPBOX=~/Dropbox
# Dropbox folder in which to resolve conflicts
FOLDER=~/Dropbox
# Where to move older files that are replaced during conflict resolution
BACKUP=~/Dropbox-saved-files
# Where to store any errors that occur during moves and copies
ERRORS=~/Dropbox-conflict-errors
rm -f $ERRORS
# Colors used to highlight whether this is a test run or will perform file replacements
red='\033[0;31m'
purple='\033[1;35m'
yellow="\033[1;33m"
NC='\033[0m' # No Color
# find $FOLDER -path "*(*'s conflicted copy [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*" -print
clear
read -e -p $'Please enter a path in the dropbox folder.\nLeaving this blank will start at the root of Dropbox.\n\nEnter Path (~/Dropbox[/pathname]): ' SUBFOLDER
FOLDER="$FOLDER$SUBFOLDER"
echo -e "\nThis script will walk the ${purple}\"$FOLDER\"${NC} tree and use the newer of any conflicted files, moving the older file to \"$BACKUP\"."
echo "Any error messages will be placed in \"$ERRORS\"."
if [[ $1 == "replace" ]]; then
echo -e "\n${red}This is NOT a drill.${NC} After backing up the older file, this script will replace it in the Dropbox folder with the newer version."
else
echo -e "${purple}This is a test run.${NC} You'll see what files would be replaced. Run \"$0 replace\" to do the actual replacement."
fi
read -n 1 -s -r -p $'\n\n-------------------------------------\nPress any key to continue...\n-------------------------------------\n\n'
find $FOLDER -type f -print0 | while read -d $'\0' file; do
NEWNAME=$(echo "$file" | sed 's/ (.*conflicted copy.*)//')
if [ "$file" != "$NEWNAME" ]; then
if [ -f "$NEWNAME" ]; then
# determine which is newer
if [ "$NEWNAME" -nt "$file" ]; then
# echo -n "$NEWNAME is NEWER than $file; "
file_to_backup="$file"
file_to_keep="$NEWNAME"
else
# echo -n "$NEWNAME is OLDER than $file; "
file_to_backup="$NEWNAME"
file_to_keep="$file"
fi
backupname=${NEWNAME/"$DROPBOX"/"$BACKUP"}
n=$(basename "$NEWNAME"); m=$(basename "$file_to_backup"); bd=$(dirname "$backupname")
if [[ $1 != "replace" ]]; then
echo "Would have moved \"$file_to_keep\" to \"$n\" and backed up older original \"$m\"."
else
(mkdir -p "$bd" && cp -p "$file_to_backup" "$backupname" && mv "$file_to_keep" "$NEWNAME" && \
echo "Moved \"$file_to_keep\" to \"$n\" and backed up older original \"$m\".") 2> $ERRORS
fi
else
# If the unconflicted version isn't there for some reason, just rename the conflicted file to the original name.
if [[ $1 != "replace" ]]; then
echo "Would have moved conflicted file to '$file'."
else
echo "Moved \"$file\" to \"$n\"."
mv "$file" "$NEWNAME" 2> $ERRORS
fi
fi
fi
done
if [[ -s $ERRORS ]]; then
echo -e "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
echo "Errors occurred as shown below: "
cat $ERRORS
echo -e "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"
fi
if [[ $1 == "replace" ]]; then
viewremaining() {
read -p $'\n\nView Remaining Conflicts? (y/n): ' VIEW
case "$VIEW" in
y|Y)
clear
find $FOLDER -path "*(*'s conflicted copy [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*" -print
return 0
;;
n|N)
exit 0
;;
*)
printf %s\\n "Please enter 'Y' or 'N'"
return 1
;;
esac
}
until viewremaining; do : ; done
deleteremaining() {
read -p $'\n\nDelete Remaining Conflicts? (y/n): ' DELETE
case "$DELETE" in
y|Y)
clear
find $FOLDER -path "*(*'s conflicted copy [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*" -print -exec rmtrash '{}' ';'
return 0
;;
n|N)
exit 0
;;
*)
printf %s\\n "Please enter 'Y' or 'N'"
return 1
;;
esac
}
until deleteremaining; do : ; done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment