linux merge folders: rsync?

I have two copies of a folder

src/
dest/

I want to merge them, doing the following:

If a file is only in src, I want it to be moved to dest

If a file is only in dest, I want it ignored IE left alone.

If a file is in both and has identical contents (IE same size and date), delete from src

If a file is in both and does not have identical contents, leave behind in src so I can manually merge them.

Only a very small number of files (between 0% and 5% of total files) should be in this last category, but I don't know how to separate the the in both and the same from in both, but different.

I've tried to figure out how to do this with rsync but to no avail so far.


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:

for file in `find src/ -type f`; do diff $file `echo $file | sed 's/src/dest/'` && rm $file || echo $file; done

or

find src -type f -exec bash -c 'cmp -s "$0" "${0/#src/dest}" && rm "$0"' {} \;

if filenames could contain whitespace/new lines/… 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.


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.