Skip to content

Instantly share code, notes, and snippets.

@rswgnu
Created October 9, 2017 03:16
Show Gist options
  • Save rswgnu/9d35f1972a33ca13f6be7330d2d01b7e to your computer and use it in GitHub Desktop.
Save rswgnu/9d35f1972a33ca13f6be7330d2d01b7e to your computer and use it in GitHub Desktop.
Recursively moves and replaces conflicted Dropbox files keeping the newer one of each pair
#!/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'
NC='\033[0m' # No Color
clear
echo "This script will walk the \"$folder\" 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 "${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
echo "Press any key to continue..."
echo "------------------------------\n"
read -n 1
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 "Errors occurred as shown below: "
cat $errors
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment