The Computer Oracle

Sync two local folders in bash

--------------------------------------------------
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 Question
00:24 Accepted answer (Score 27)
00:58 Answer 2 (Score 16)
01:38 Answer 3 (Score 1)
01:59 Answer 4 (Score 0)
02:21 Thank you

--

Full question
https://superuser.com/questions/409980/s...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux

#avk47



ACCEPTED ANSWER

Score 27


There are many more properties to rsync than the previous answer mentions. You should look into them yourself:

man rsync

But for your problem I'd suggest:

rsync -a --delete X Y

This way, the receiver (Y) will delete any file it has that is not in X. Be sure you do it right though. Used incorrectly as root can easily kill your system... :)




ANSWER 2

Score 16


Take a look at rsync(1). It's intended to do pretty much exactly what you want.

rsync -av X Y

It supports remote copying, through its own protocol or ssh, but it can also be used locally.

Depending on the exact behaviour you want you may also want to pass the --update option. This will tell rsync not to overwrite files which already exist on the target and are newer than the ones in the source.

If you want to remove extra files (i.e. files which exist in Y, but not in X) you can add the --delete option.




ANSWER 3

Score 0


Try using rsnyc, possibly with the following command:

rsync -avzu /home/x /y

-u = do not overwrite a file at the destination, if it is modified




ANSWER 4

Score 0


I think some of the functionality that you are looking for in option -u (update) actually exists in this option:

"--ignore-existing" - skip updating files that exist on receiver