Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a project with many files, located in several sub-directories. Dropbox somehow replaced the latest versions of some of those files with an old version and renamed the latest ones by adding " (Johns in Konflikt stehende Kopie 2013-03-17).txt" to tmen.

Now, I need to rename all files that have that addition " (Johns in Konflikt stehende Kopie 2013-03-17)" to the original filename, removing the old "authoritative" version (as Dropbox saw them).

Is there a shell script or something to run in terminal for a certain directory, which would do this recursively on a target directory?

For example: "filename (Johns in Konflikt stehende Kopie 2013-03-17)" should be renamed to "filename" and in case there was already a file called filename, remove that first.

share|improve this question
add comment

1 Answer

This worked for me:

while read -r i;
do
        old_file=${i%%(*}
        rm $old_file
        mv "$i" "$old_file"
done <<< "$(find | grep "2013-03-17)$")"

Before:

./files/filename2 (Johns in Konflikt stehende Kopie 2013-03-17)
./files/filename2
./files/filename (Johns in Konflikt stehende Kopie 2013-03-17)
./files/filename
./files/subfolder/filename (Johns in Konflikt stehende Kopie 2013-03-17)
./files/subfolder/filename

After:

./files/filename 
./files/filename2 
./files/subfolder/filename 

Do a backup of your files just in case.

Explanation:

while read -r i; <<< "$(find | grep "2013-03-17)$")" reads all lines from the output of that command, which returns path/filename for all files ending in 2013-03-17) -- you might need to add the extension of the file or get only the filename.

old_file=${i%%(*} sets old_file as everything in the string outputed by the find command, that is, filename (Johns in Konflikt stehende Kopie 2013-03-17) and all others, as everythig before (, that is, filename.

rm $old_file removes the old files, that is, everything named filename and filename2

mv "$i" "$old_file" renames filename (Johns in Konflikt stehende Kopie 2013-03-17) to filename

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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