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?

5 Answers 5


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

  • 1
    Agree that you should be very careful with this. Try adding --dry-run to the command to see what will happen without actually making any changes. If it goes well do the same command without --dry-run Commented Sep 23, 2015 at 4:17
  • 1
    A small correction that took me a while to reach: rsync -a --delete X/ Y. That little slash after X made it work as intended for me, so it might help other as well. Explained here: abbeyworkshop.com/howto/unix/nix_rsync/index.html
    – Shovalt
    Commented Oct 29, 2018 at 9:54

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.

  • But it won't remove files, than removed in X, while rm Y; cp X Y will.
    – Lescott
    Commented Apr 8, 2012 at 11:26
  • In order to remove files that removed from X, use --delete option. Commented Jul 30, 2015 at 5:00

You might use Fitus/Zaloha.sh. It first calculates the necessary synchronization actions, presents them to you, asks for confirmation and then executes them:

$ Zaloha.sh --sourceDir="X" --backupDir="Y"
  • In case someone trying this doesn't notice the original Zaloha has been superceded by github.com/Fitus/Zaloha2.sh . There is a note about this in the original Zaloha repos but I thought it was worth reinforcing.
    – glaucon
    Commented Mar 15, 2021 at 21:07

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

  • But it won't remove files, than removed in X, while rm Y; cp X Y will.
    – Lescott
    Commented Apr 8, 2012 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

You must log in to answer this question.

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