Skip to content

Instantly share code, notes, and snippets.

@odd
Forked from wilson428/dropbox.sh
Last active March 1, 2017 12:03
Show Gist options
  • Save odd/4923111e572f901a99a758ebcf8a65bb to your computer and use it in GitHub Desktop.
Save odd/4923111e572f901a99a758ebcf8a65bb to your computer and use it in GitHub Desktop.
Fix Dropbox conflicts automatically.
#!/bin/bash
#Modified from https://gist.github.com/wilson428/40c0b8bf7ecba5a301f6
#Added modifications from https://gist.github.com/nbartzokas/416400824ef2dcf4248e4c75c2d25cfa
#Thx, http://stackoverflow.com/questions/20723868/batch-rename-dropbox-conflict-files
getopts "r" replace && shift
folder=${1:-$(pwd)}
backup=${2:-$(pwd).backup}
#colors
red='\033[0;31m'
purple='\033[1;35m'
NC='\033[0m' # No Color
clear
echo "This script will climb through the $folder tree and repair conflict files by deleting the OLDER of the the conflicted file and its counterpart"
if [[ $replace == "r" ]]; then
echo -e "${red}This is NOT a drill.${NC} The script will backup the older of the conflicted files and then delete it from the Dropbox directory."
else
echo -e "${purple}This is a dry run.${NC} You'll see what files would be replaced. Run \"./dropbox.sh -r [folder] [backup]\" to make it run for real."
fi
echo "Press any key to continue..."
echo "------------------------------"
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 "$newname is NEWER than $file"
file_to_move="$file"
file_to_keep="$newname"
else
echo "$newname is OLDER than $file"
file_to_move="$newname"
file_to_keep="$file"
fi
backupname=${newname/"$folder"/"$backup"}
if [[ $replace == "r" ]]; then
echo "Would have moved $file_to_move to $backupname"
else
echo "Moving $file_to_move to $backupname"
mkdir -p "$(dirname "$backupname")"
cp "$file_to_move" "$backupname"
mv "$file_to_keep" "$newname"
fi
else
# if the unconflicted version isn't there for some reason, just rename the original
if [[ $replace == "r" ]]; then
echo "Didn't see an unconflicted counterpart for $file, so would have just renamed file"
else
echo "Didn't see an unconflicted counterpart for $file, so will replace"
mv "$file" "$newname"
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment