Skip to content

Instantly share code, notes, and snippets.

@MaffooClock
Created October 19, 2023 18:24
Show Gist options
  • Save MaffooClock/ea4ebac6fa35b4d531cc2137913ee67e to your computer and use it in GitHub Desktop.
Save MaffooClock/ea4ebac6fa35b4d531cc2137913ee67e to your computer and use it in GitHub Desktop.
Bash script to scan your Dropbox folder for "conflicted copy" files and move them to your Trash folder (MacOS and Linux only).
#!/usr/bin/env bash
# This script will scan your Dropbox folder for "conflicted copy" files and move them to your Trash folder.
# The path to your Dropbox folder will be determined by reading a JSON file that should be present if Dropbox is installed.
##### Make sure we're running on MacOS or Linux (and snag the path to the trash directory while we're at it)
if [[ "$(uname)" == "Darwin" ]]; then
TRASH=$(realpath ~/.Trash)
elif [[ "$(expr substr $(uname -s) 1 5)" == "Linux" ]]; then
TRASH=$(realpath ~/.local/share/trash)
else
echo "This script only runs on MacOS or Linux."
exit
fi
##### Sanity check: make sure the Trash directory actually exists
if [[ ! -d "${TRASH}" ]]; then
echo "Expected to find Trash directory at ${TRASH}"
exit
fi
##### Check that a info.json file exists (if Dropbox is installed, it should be exactly here)
dropboxInfoFile=$(realpath ~/.dropbox/info.json)
if [[ ! -f "${dropboxInfoFile}" ]]; then
echo "Could not find ${dropboxInfoFile}"
exit
fi
##### Try to read the path to the Dropbox folder from the info.json file
dropboxInfo=$(cat "${dropboxInfoFile}")
dropboxPersonal=${dropboxInfo#*\"personal\":}
dropboxPath=${dropboxPersonal#*path\":}
dropboxPath=${dropboxPath#*\"}
dropboxPath=${dropboxPath%%\"*}
if [[ ! -d "${dropboxPath}" ]]; then
echo "Expected to find Dropbox at ${dropboxPath}"
exit
fi
##### Now search the Dropbox path for files containing "conflicted copy"
conflictedFiles=()
while IFS= read -r -d $'\0'; do
conflictedFiles+=("$REPLY")
done < <(find "${dropboxPath}" -name "*(*'s conflicted copy [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*" -print0)
##### See if we have any work to do
if [[ ${#conflictedFiles[@]} -eq 0 ]]; then
echo "No conflicted files were found."
exit
fi
##### Deal with those files
echo "Found ${#conflictedFiles[@]} files; attempting to move them to ${TRASH}"
for filename in "${conflictedFiles[@]}"; do
mv "${filename}" "${TRASH}/"
done
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment