Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have one big folder "X" in my home directory, and outdated copy of this on a flash drive -- Y. What is the analog of rm Y; cp X Y, but without overwriting already existing files?

share|improve this question

migrated from stackoverflow.com Apr 8 '12 at 13:18

This question came from our site for professional and enthusiast programmers.

4 Answers 4

up vote 10 down vote accepted

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

share|improve this answer

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.

share|improve this answer
    
But it won't remove files, than removed in X, while rm Y; cp X Y will. –  Lescott Apr 8 '12 at 11:26

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

share|improve this answer
    
But it won't remove files, than removed in X, while rm Y; cp X Y will. –  Lescott Apr 8 '12 at 11:27

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.