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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-06-2015, 11:38 AM
|
#1
|
Member
Registered: Sep 2006
Posts: 374
Rep:
|
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?
|
|
|
05-06-2015, 12:09 PM
|
#2
|
Member
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814
|
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.
|
05-06-2015, 12:57 PM
|
#3
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
05-06-2015, 01:50 PM
|
#4
|
Senior Member
Registered: May 2010
Distribution: No more Linux. Done with it.
Posts: 1,238
Rep:
|
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.
|
|
|
05-06-2015, 02:20 PM
|
#5
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
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.
|
|
|
05-07-2015, 03:56 AM
|
#6
|
Member
Registered: Sep 2006
Posts: 374
Original Poster
Rep:
|
@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.
|
|
|
05-07-2015, 08:38 AM
|
#7
|
Member
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814
|
Quote:
Originally Posted by xeon123
@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.
|
|
|
All times are GMT -5. The time now is 11:11 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|