LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   Merge directory trees (http://www.linuxquestions.org/questions/linux-software-2/merge-directory-trees-863412/)

mrwall-e 02-17-2011 06:03 PM

Merge directory trees
 
I have a couple directories in the following format:

+ Dir 1
----+ Dir A
-------- file1
----+ Dir B
-------- file2
+ Dir 2
----+ Dir A
-------- file3
----+ Dir C
-------- file2
+ Dir 3
----+ Dir B
-------- file2
----+ Dir C
-------- file1

My desired structure is:

+ Dir Final
----+ Dir A
-------- file1
-------- file3
----+ Dir B
-------- file2-1 [or something so that the two files don't overwrite]
-------- file2-2
----+ Dir C
-------- file2
-------- file1

Basically, I would like to combine all the directories that I can, without deleting/overwriting any files. I have looked at Rsync, but I could not find the correct options to do what I wanted.

Thanks

MTK358 02-19-2011 07:27 AM

Just use cp.

Code:

mkdir dir_final

cp -R dir_1/* dir_final
cp -R dir_2/* dir_final
cp -R dir_3/* dir_final


mrwall-e 02-20-2011 08:14 AM

Re: Merge directory trees
 
One of these directories is quite large. Would it work if I used it as the final directory? Because otherwise I don't think I have enough space.

Thanks

MTK358 02-20-2011 08:22 AM

Sure.

If dir_1 is the large one, just do it like this:

Code:

cp -R dir2/* dir1
cp -R dir3/* dir1

I think you understand the concept. If a dir is copied to a place that already has a dir by the same name, the contents of the conflicting dir are not deleted.

druuna 02-20-2011 08:29 AM

Hi,

Isn't one of the wishes the creation of a renamed duplicate if one is encountered?

Quote:

# in
+ Dir 1
----+ Dir B
-------- file2

+ Dir 3
----+ Dir B
-------- file2

# out
----+ Dir B
-------- file2-1 [or something so that the two files don't overwrite]
-------- file2-2
The cp -R solution will override duplicate files.......

MTK358 02-20-2011 08:37 AM

Quote:

Originally Posted by druuna (Post 4264727)
Hi,

Isn't one of the wishes the creation of a renamed duplicate if one is encountered?



The cp -R solution will override duplicate files.......

I didn't notice that.


All times are GMT -5. The time now is 02:01 PM.