Managing filenames collisions while merging folders (linux)

0

I'd need a sort of syncing sw to perform bidirectional (recursive) merging of folders.

Say I have 2 folders A and B.

Folder A contains files : 1, 3, 5 and 7.
Folder B contains files : 1, 2, 3 and 6.

After the execution of merging, the outcome should be as follows :

Folder A: 1, 1-b, 2, 3, 3-b, 5, 6, 7
Folder B: 1, 1-a, 2, 3, 3-a, 5, 6, 7


Breaking them down separately:

both folder A and B containing: 2, 5, 6, 7
(the OR union of non colliding filenames : a bidirectional copy (of same rank, no particular source neither any particular destination) without renaming;

Folder A containing also : 1, 1-b, 3, 3-b (own versions plus added ones)
Folder B containing also : 1, 1-a, 3, 3-a (own versions plus added ones)
(The dash-a or dash-b is just a placeholder for automatic renaming to circumvent name collision, not really important. Every suffix would do). Basically each folder would contain all versions (without newer ones overwriting older ones) of all files, regardless of their names.

How could I get this outcome ? I've looked at 'meld' but didn't work as expected (it proposed overwriting existing files, unable to rename), and 'rsync' seems intrinsically asymmetrical (bestowing higher rank to source than destination). I need true parithetic merging ...

Thanks in advance for any help. BTW ... a dedicated existing SW would be more appreciated than reinventing hot water by-hand. But if none exists, also scripting would do. TY. Gatto

GattoVizzato

Posted 2017-08-21T01:03:32.097

Reputation: 1

This isn't very clear... I'll try editing it by adding some line breaks (only?), but if you can clarify it further that might be more helpful – Xen2050 – 2017-08-21T02:01:36.430

Answers

0

Look into the info pages for plain old cp. A command like

cp --archive --backup --suffix="-original" A/* B

Should copy every file from A into B, and if there are any already existing files (conflicting filenames) the existing destination file will be renamed to [name]-original (i.e. a backup will be made first).

Then running it again using --no-clobber instead of --backup and --suffix should copy any leftover files only in B into A

cp --archive --no-clobber --exclude='*-original' B/* A

This should exclude the [name]-original files in B. Make sure there aren't any files with that suffix to begin with.

And add a -v to see what's going on.

[Tried --recursive but it wasn't quite as expected]

Xen2050

Posted 2017-08-21T01:03:32.097

Reputation: 12 097