Search code examples
linuxdropboxrename

batch rename dropbox conflict files


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

Solution

  • all files from current directory:

    for file in *
    do
        newname=$(echo "$file" | sed 's/ (.*)//')
        if [ "$file" != "$newname" ]; then
            echo moving "$file" to "$newname"
    #       mv "$file" "$newname"     #<--- remove the comment once you are sure your script does the right thing
        fi
    done
    

    or to recurse, put the following into a script that i'll call /tmp/myrename:

    file="$1"
    newname=$(echo "$file" | sed 's/ (.*)//')
    if [ "$file" != "$newname" ]; then
        echo moving "$file" to "$newname"
    #       mv "$file" "$newname"     #<--- remove the comment once you are sure your script does the right thing
    fi
    

    then find . -type f -print0 | xargs -0 -n 1 /tmp/myrename (This is a bit hard to do on the command line without using an extra script because the file names contain blanks).