LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-06-2015, 11:38 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
Merge directories with rsync


I have 2 directories that I want to merge with rsync so that in the end both have the same and most recent files. How can I do this with rsync?
 
Old 05-06-2015, 12:09 PM   #2
maples
Member
 
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814

Rep: Reputation: 265Reputation: 265Reputation: 265
What do you have so far?

I think this should take care of it:
Code:
cp -r /path/to/first/source/ /path/to/destination/
rsync -av --update /path/to/second/source/ /path/to/destination/ --dry-run
The first command copies the first source to the destination.
The second command copies the contents of the second source to the destination ONLY if the file in the second source is NEWER than the file in the destination. The "--dry-run" flag will make rsync print what it's going to do, but not actually do it. If the output looks like it will accomplish what you want, then remove that flag and it will copy it for real.

Hope this helps!
 
1 members found this post helpful.
Old 05-06-2015, 12:57 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
A simple "rsync -ut" in both directions should do it. Note, however, that if the same file has been modified on BOTH sides, ONLY the newer one will be retained. Also note that deleting files can be a pain, since you need to delete it on both sides before the next time the script runs or it'll be copied back over.

Code:
rsync -ut /path/to/source/ /path/to/destination/
rsync -ut /path/to/destination/ /path/to/source/
I use this all the time to keep various config files synchronized between multiple systems. I just need to make the change on one of them, and the next time the script runs all of them are updated.
 
1 members found this post helpful.
Old 05-06-2015, 01:50 PM   #4
Mr. Alex
Senior Member
 
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238

Rep: Reputation: Disabled
Code:
rsync -av --delete /dir/what /where
is my way to use it most of the times.

Note that
Code:
rsync -av --delete /dir/what /where
differs from
Code:
rsync -av --delete /dir/what/ /where
First one will copy (sync) the whole "/dir/what" whereas the second one copies the contents of "/dir/what". Rsync's approach differs from bash here.
 
Old 05-06-2015, 02:20 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
I don't want to speak for the OP here, but he did say merge, not back up. Merge implies that there are changes on BOTH sides that need to be synced to the other. A simple rsync run in one direction will only update the destination, not the source, and you definitely do not want to use --delete or any files created on the destination that aren't on the source yet will be removed.

It could be that the OP simply didn't use the right word to describe what he wanted to do, but he should be well aware of what these flags are doing lest he accidentally run a command that removes or overwrites important files.

Last edited by suicidaleggroll; 05-06-2015 at 02:21 PM.
 
Old 05-07-2015, 03:56 AM   #6
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
@suicidaleggroll, In this case I mean to update/"make a backup" of 2 dirs with the most recent files. So, I have a dir called "mydir" in disk1 and disk2, and I want that "mydir" will have the same and most recent content in disk1 and disk2.

Since I have updated this dir in both disks at different times, they are different. Now I want to merge both dirs with the most recent files, and if there is a conflict in the same file, the most recent updated file will prevail. So, all the solutions that the rest of users gave me are ok.

Last edited by xeon123; 05-07-2015 at 04:01 AM.
 
Old 05-07-2015, 08:38 AM   #7
maples
Member
 
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814

Rep: Reputation: 265Reputation: 265Reputation: 265
Quote:
Originally Posted by xeon123 View Post
@suicidaleggroll, In this case I mean to update/"make a backup" of 2 dirs with the most recent files. So, I have a dir called "mydir" in disk1 and disk2, and I want that "mydir" will have the same and most recent content in disk1 and disk2.

Since I have updated this dir in both disks at different times, they are different. Now I want to merge both dirs with the most recent files, and if there is a conflict in the same file, the most recent updated file will prevail. So, all the solutions that the rest of users gave me are ok.
In that case, post #3 looks like the one you want.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge directories usao Linux - General 15 02-06-2015 05:59 AM
merge directories d-fens Linux - General 9 02-04-2010 06:48 PM
Merge 2 same name directories in linux. nishith Linux - Server 12 09-25-2009 01:15 AM
merge directories ryedunn Linux - Newbie 4 11-15-2006 10:02 AM
how do I Merge 2 directories syssyphus Linux - General 4 01-21-2006 03:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration