3 Answers
3

I've only performed limited functionality testing, so please be careful with this command (--dry-run):

rsync -avPr --ignore-existing --remove-source-files src/ dest

Please note the trailing / as this will recurse into src instead of copying src itself, this should maintain your existing paths.

By using the --ignore-existing flag in combination with the --remove-source-files flag you will delete only files from src that are sync'ed from src to dest, that is files that did not previously exist in dest only.

For deleting non-sync'ed files, that is those that already existed in dest/ as in src/, you can use:

Regarding Gilles' comment concerning special characters, that is certainly something to be mindful of and there are many solutions, the simplest would be to pass an -i to rm which will prompt before all deletion. Provided that src/, or its parent path, is provided to find, however, the fully qualified path should result in all file names being handled properly by both the diff and rm commands without quoting.

correction: that command will not remove files from src if an identical copy already exists in dest
–
TokNov 23 '10 at 18:52

Yeah :(. That's the part that I'm finding hard to figure out.
–
David OneillNov 23 '10 at 19:07

2

Well, the good news is that you can solve it independently without much hassle: for file in `find src/ -type f`; do diff $file `echo $file | sed 's/src/dest/'` && rm $file || echo $file; done (you can skip the || echo $file if you like, it is included for completeness)
–
TokNov 23 '10 at 19:16

Nifty: that's what I needed. Edit that into your answer, and I'll accept it!
–
David OneillNov 24 '10 at 0:33

@Tok: Your command will choke on file names that contain special characters (whitespace, \?*[, initial -). You need to use double quotes around variable substitutions, pass -- to utilities before file names, use find … -exec … instead of parsing the output of find. With an rm command in the mix, this is a recipe for disaster.
–
GillesNov 24 '10 at 1:06

unison is the tool you're looking for. Try unison-gtk if you prefer a gui. But I don't think it will delete similar files: unison try to have both directories identical. Nevertheless it will easyly 1) identify which files are to copy; 2) which ones needs manual merge.

The following script should do things reasonably. It moves files from the source to the destination, never overwriting a file and creating directories as necessary. Source files that have a corresponding different file in the destination are left alone, as are files that are not regular files or directories (e.g. symbolic links). The files left over in the source are those for which there is a conflict. Beware, I haven't tested it at all.