Skip to content

Instantly share code, notes, and snippets.

@wilson428
Last active November 11, 2022 17:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save wilson428/40c0b8bf7ecba5a301f6 to your computer and use it in GitHub Desktop.
Save wilson428/40c0b8bf7ecba5a301f6 to your computer and use it in GitHub Desktop.
Fix Dropbox conflicts automatically.
#!/bin/bash
#Thx, http://stackoverflow.com/questions/20723868/batch-rename-dropbox-conflict-files
#Point to where you want the script to look and where it should backup files it replaces
folder=~/Dropbox/Private
backup=~/Desktop/Dropbox.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 [[ $1 == "replace" ]]; 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 replace\" 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 [[ $1 != "replace" ]]; then
echo "Would have moved $file_to_move to $backupname"
else
echo "Moving $file_to_move to $backupname"
mkdirp "$(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 [[ $1 != "replace" ]]; 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
@Moitah
Copy link

Moitah commented Mar 26, 2016

mkdirp not available on my system because not an npm developer means no backups at all :/

@scheasbro
Copy link

Hello!
Thank you for writing this script! It's exactly what I've been looking for, although I am very new to the idea of scripting. Please bear with me. I am generally a very fast learner.

I did modify your script a bit for my own purposes, but am still having trouble with this error when I actually run the program:

line 49: mkdirp: command not found

Here is the error in context:


/Volumes/Seagate/Dropbox/AGCC/Graduation/2012/._owlclass12.jpg is NEWER than /Volumes/Seagate/Dropbox/AGCC/Graduation/2012/._owlclass12 (Sheila’s MacBook Pro's conflicted copy 2015-10-31).jpg
Moving /Volumes/Seagate/Dropbox/AGCC/Graduation/2012/._owlclass12 (Sheila’s MacBook Pro's conflicted copy 2015-10-31).jpg to /Users/scheasbro/Desktop/Script_Backup/AGCC/Graduation/2012/._owlclass12.jpg
./dropbox.sh: line 49: mkdirp: command not found
cp: /Users/scheasbro/Desktop/Script_Backup/AGCC/Graduation/2012/._owlclass12.jpg: No such file or directory


I'm attaching the script that I've modified ever so slightly (to reflect my source and backup folders), so that you can see line 49.

How do I fix this error?

I would be most appreciative of any guidance or solutions that you can offer.

Thank you for taking the time to read this! :)

______________my dropbox.sh script (can't seem to attach??)

#!/bin/bash

#Thx, http://stackoverflow.com/questions/20723868/batch-rename-dropbox-conflict-files

#Point to where you want the script to look and where it should backup files it replaces
folder=/Volumes/Seagate/Dropbox
backup=~/Desktop/Script_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 [[ $1 == "replace" ]]; 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 replace" 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 [[ $1 != "replace" ]]; then
            echo "Would have moved $file_to_move to $backupname"
        else
            echo "Moving $file_to_move to $backupname"
            mkdirp "$(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 [[ $1 != "replace" ]]; 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