17

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).

2
  • Is rsync an option?
    – Bobby
    Feb 3, 2011 at 11:43
  • It is, but I don't know much about rsync. Feb 3, 2011 at 11:44

5 Answers 5

21

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/

2
  • 1
    BEWARE of this command. It will delete all the files which wont differ. I ran it and LOST almost everything!!
    – Arvind K.
    Apr 12, 2016 at 8:15
  • 2
    @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
    Jun 4, 2016 at 19:34
20

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

9
  • +1 I get so used to synching across machines I forget the elegant ways.
    – Tog
    Feb 3, 2011 at 12:11
  • does not work for me :( Feb 3, 2011 at 12:51
  • That's actually good gnu cp. ;-)
    – Keith
    Feb 3, 2011 at 19:58
  • 1
    On 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. Nov 12, 2012 at 2:11
  • 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
    Mar 21, 2013 at 16:16
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.

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/*
1

You can diff the 2 directories.

diff -r dir1 dir2

it will show you the list of files that are differnet

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .