5

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 4

2

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).

1
  • Thanks for your solution Guntram. It put me on the right track. I have taken what you provided and modified / extended it to better fit my purposes. Learnt more about the power of linux bash (respect).
    – cemlo
    Dec 23, 2013 at 7:24
1

a small contribution:

I've had a problem this this script. The files with spaces in their name do not made a copy. So I modified line 17 :

-------cut-------------cut---------

if test -f "$newname"

-------cut-------------cut---------

1

This script displayed above is now outdated; the following works fine with the latest version of Dropbox running on Linux Mint at the time of writing:

#!/bin/bash

#modify this as needed
folder="./"
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/ (.*Case Conflict.*)//')
    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
1

You can use the tool Dropbox Conflict Fix. It resolved all my conflicted copy files.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.