How can I copy a (big) directory over another changing only the files that differ?

14

6

I have directory a and directory b. They are big. b is almost identical to a. "almost" means that 4-5 files differ, and I don't know which they are. I want to copy b over a, but only the files that differ. i'm in bash.

(no, I can't simply delete a and replace it with b, because 1) a is version-controlled 2) a full copy (or a mv) would take too much. I want to copy only the files that differ).

janesconference

Posted 2011-02-03T11:42:02.983

Reputation: 243

Is rsync an option? – Bobby – 2011-02-03T11:43:39.657

It is, but I don't know much about rsync. – janesconference – 2011-02-03T11:44:38.627

Answers

17

You can use rsync to do this, the command I use is rsync -tr "folder to copy from" "folder to copy to"

e.g. rsync -tr /home/me/stuff/* /home/me/otherstuff/

Tog

Posted 2011-02-03T11:42:02.983

Reputation: 4 747

1BEWARE of this command. It will delete all the files which wont differ. I ran it and LOST almost everything!! – Arvind K. – 2016-04-12T08:15:01.223

@Arvind I don't know what command you used that caused you to lose your data but it wasn't rsync -tr. Read the man page. – Tog – 2016-06-04T19:34:33.707

18

It is also possible to do this with good old cp:

Thanks to srcspider for reminding me to use -T!

cp -ruT old-dir new-dir

new123456

Posted 2011-02-03T11:42:02.983

Reputation: 3 707

+1 I get so used to synching across machines I forget the elegant ways. – Tog – 2011-02-03T12:11:49.720

does not work for me :( – janesconference – 2011-02-03T12:51:40.087

That's actually good gnu cp. ;-) – Keith – 2011-02-03T19:58:58.653

1On OS X, the -u option does not exist. I simply used cp -r old-dir new-dir, since I did not have to compare dates. – Jean-François Beauchamp – 2012-11-12T02:11:50.413

Does not work. I am not sure if this is some name convention but cp -ru folder-A folder-B will simply copy folder-A inside folder-B not copy contents of A inside B and overwrite, which would be the desired effect. – srcspider – 2013-03-21T16:16:02.380

@srcspider You're right - in this case, I should have added the -T flag. – new123456 – 2013-03-22T20:19:33.227

@new123456 -T is not documented in --help for me can you paste the description in the answer please. :) – srcspider – 2013-03-23T10:18:42.983

@srcspider You must not be using GNU cp, then - on my machine, cp from coreutils 8.13 mentions the -T flag in the help. The online documentation describing what exactly -T does is on GNU's site.

– new123456 – 2013-03-26T02:43:23.413

@new123456 Yeah it seems the windows git bash version doesn't have it; I'm on a unix box now and it's in the docs. Is there some other command that will copy the files from A to B and overwrite? – srcspider – 2013-03-26T07:49:29.377

1

Another good option is Unison (http://www.cis.upenn.edu/~bcpierce/unison/), particularly if there isn't really a "source" and a "destination". Each directory is a root and Unison syncs them and keeps metadata for future syncs. It offers both a command-line and a GUI option that can easily be scheduled via cron as well.

I use it to make a backup of my Dropbox to my local NAS appliance which can't run a Dropbox client.

Digitalcraig

Posted 2011-02-03T11:42:02.983

Reputation: 111

1

It CAN be done with good ol' cp, though with a slightly different format than stated above. Here's how I did it:

cp -ru --target-directory="destination_path" source_path/*

Sven Croon

Posted 2011-02-03T11:42:02.983

Reputation: 11

1

You can diff the 2 directories.

diff -r dir1 dir2

it will show you the list of files that are differnet

Oleg

Posted 2011-02-03T11:42:02.983

Reputation: 11