Extending on this question:

Automatically sync a folder to a USB when USB is plugged in?

I do not want an automatic script but just a small bash script to do the following:

  • Copy current ~/Documents/ to a backup folder
  • Copy newer files from USB/Documents to ~/Documents/
  • Copy and carry over deletions from ~/Documents/ to USB/Documents/

I now have:

# Backup local:
rsync -av ~/Documents/ ~/backup20112012/Documents/

# Synch new files from USB:
rsync -auv /media/20CC-A5E5/Documents/ ~/Documents/

# Sync and delete USB:
rsync -av --delete ~/Documents/ /media/20CC-A5E5/Documents/

which works with one exception. If I delete a file in ~/Documents, it is created again in step 2 (cause it is on USB). Is there a way to omit copying files that have been deleted in step two?

  • There is something wrong with your requirements, there is no way to distinguish a deleted from a non existing file. So, "carry over deletions from ~/Documents" implies that you want to keep only files which are already present in ~/Documents, such conflicts with the "Copy newer files from USB" which is likely to include files which are not present on ~/Documents. – João Pinto Sep 15 '11 at 17:58
  • What about saving the time of the last run in a file and only copy files made after that from the USB? – Zagga Sep 15 '11 at 18:04
  • That would not work when you rename files on the USB, actually your requirements do no address "mv" – João Pinto Sep 15 '11 at 18:59

Your requirements are a little strange. It looks like you want to synchronize ~/Documents and USB/Documents so that they have the same content. If that's so, use unison Install unison, which is the tool of choice when you're looking for a bidirectional rsync.


If you want to delete all files that are on the USB that are not on the PC then I believe you can do this:

# deletes any files that are on the USB that are not on the PC and synchronizes any files that have been changed more recently on the PC then on the USB.
rsync -auv --delete ~/Documents/ /media/20CC-A5E5/Documents/
# synchronizes files that are newer on the USB then on the PC
rsync -auv /media/20CC-A5E5/Documents/ ~/Documents/

By having the first command use -u, you should not overwrite any files that are on the USB and the PC, and by using --delete you will delete all files that are on the USB that are not on the PC. The second command then synchronizes the USB with the PC.

The one downside to doing this is that ANY files that are on the USB and are not on the PC will be deleted. This is what you want I think though. If you create any new files on the USB that are not on the PC the new files on the USB will be deleted. If you delete any files on the PC they will be deleted on the USB.

From what you want I don't think this will suffice, but it is the most you can do with RSync.

If you wanted to just delete files on the USB that were DELETED on the PC, and NOT delete files on the USB that were never on the PC, then you would have to create some sort of program that would log every time you deleted a file on your PC, and then only delete those from the USB. Which is probably a lot more complicated and more work than you want to put in for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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