I am looking for a way to merge the contents of two nested directories if their names match. The new combined folder needs to be placed in a new directory. If there is not a folder in directory 2 that matches the name of the folder in directory 1, the folder from directory 1 must still be copied into the new master directory.
For example, let's say I have a directory structure as follows:
* dir1 * foo * file1 * bar * file2 * extra * file3 * dir2 * foo * file4 * bar * file5
I would like the output to be:
* newdir * foo * file1 * file4 * bar * file2 * file5 * extra * file3
Thank you in advance for your help!