batch rename dropbox conflict files

Viewed 1000

I have a large number of conflict files generated (incorrectly) by the dropbox service. These files are on my local linux file system.

Example file name = compile (master's conflicted copy 2013-12-21).sh

I would like to rename the file with its correct original name, in this case compile.sh and remove any existing file with that name. Ideally this could be scripted or in such a way to be recursive.

EDIT

After looking over the solution provided and playing around and further research I cobbled together something that works well for me:

#!/bin/bash

folder=/path/to/dropbox

clear

echo "This script will climb through the $folder tree and repair conflict files"
echo "Press a key to continue..."
read -n 1
echo "------------------------------"

find $folder -type f -print0 | while read -d $'\0' file; do
    newname=$(echo "$file" | sed 's/ (.*conflicted copy.*)//')
    if [ "$file" != "$newname" ]; then
        echo "Found conflict file - $file"

        if test -f $newname
        then
            backupname=$newname.backup
            echo " "
            echo "File with original name already exists, backup as $backupname"
            mv "$newname" "$backupname"
        fi

        echo "moving $file to $newname"
        mv "$file" "$newname"

        echo
    fi
done
4 Answers
Related