-   Linux - General (
-   -   Best way to merge two directory trees with replace? (

legacyprog 12-24-2011 10:10 AM

Best way to merge two directory trees with replace?
I realize there must be many ways to merge directory trees, but I am picking my way through a hard drive recovery and I'd appreciate the assist to save me time (which is my enemy right now).

My situation is two large directory trees on the same server, the roots are:

The directory structures are similar (i.e., the /Backup tree was a point in time backup of portions of /Public2/MasterTree, only the most important files, /Public2/MasterTree is complete at an older point in time).

As you may imagine, I need to copy the files from /Backup/PartialTree overlaying or creating any files in the equivalent target path, including creating any folders needed to contain new files.

I have tried cp -u -r (which did not seem to update though I thought it would) and cp -r (which created PartialTree as a tree UNDER /Public2/Backup.

I have never used rsync so I'm not sure I want to turn this into a learning experience while trying to recover data, and my bash is not good enough to trust either (I don't consider myself a newbie, but some might). This is Ubuntu 11.10 if it matters.

Thanks in advance for any help, and Happy Holidays.

jhwilliams 12-24-2011 11:52 AM

Yes, this definitely sounds like a job for rsync. Do turn this into a learning experience, coupled with suggestions here on the Forums.

These days, I only use cp and scp for the simplest of copy jobs.

This is probably a good start:


rsync -av /Backup/PartialTree/ /Public2/MasterTree
-HAX can also be added depending on how anal you want to get.

Just read the critical portions of the man page so you understand what you're doing.

legacyprog 12-24-2011 12:33 PM

Thanks jhwilliams, but my first shot at using rsync using the syntax you suggested just copied all the folders from /Backup/PartialTree/ under /Public2/MasterTree/FirstFolder.

I did review the man rsync page but it is daunting. Your suggested command certainly makes sense. The -a implies recursion. I am puzzled.

I'll be off and on today with family commitments. Feel free to leave any additional comments for me to review later. But in any case thanks and Happy Holidays!

jhwilliams 12-24-2011 12:49 PM

FirstFolder? Do you mean in directory named PartialTree? The slashes are subtle but important; did you include the trailing slash on PartialTree/ ?

legacyprog 12-24-2011 01:16 PM

Running out of the house now, but I up arrowed the Terminal and it seems I did include the trailing slash on PartialTree as you showed, but not on MasterTree, as you showed. Unless it's cause I used sudo, but I doubt that. Will check later. Thanks for hanging in there. I can do experiments with junk directories, maybe that's what I should do.

legacyprog 12-24-2011 08:15 PM

OK, for anyone stumbling on this thread in the future, after I got back from the family Christmas party, I did a test with a couple junk directory trees (I created test files in them with the touch command) and (of course) the rsync -av worked just fine, as jhwilliams and man rsync suggested.

So I figured it must be something I was doing wrong with the two particular directory trees in question. So I kept adjusting the syntax and it eventually worked, but I was left with /Public2/MasterTree/PartialTree. That's right, it put the entire PartialTree hierarchy under the MasterTree, so I deleted it and decided to "try again". This time I thought if I did "sudo rsync -av /Backup/PartialTree/. /Public2/MasterTree/." it might work better, because the -d switch description in man rsync gave me the impression that using /. at the end was an alternate way of calling attention to the contents of a directory, not the directory itself. However now rsync started refusing to do anything, so I looked and somehow all my updates *had* been applied to the actual /Public2/MasterTree structure! So either rsync did that *in addition* to creating the entire PartialTree hierarchy under /Public2/MasterTree, or else in my attempts to get this all to work I had stumbled on something that accomplished what I needed but I had counted it as an error (maybe the time that I ended up with the entire MasterTree hierarchy stored under /Backup/PartialTree? perhaps that was rsync's way of "synchronizing" the two trees?).

I didn't want to just leave this thread dangling, but clearly I do not have a way of confirming the best approach for anyone trying to do the same as me in the future. Sorry! I gave it my best try in the time I had available.

All times are GMT -5. The time now is 10:04 PM.