Is there a simple way to (recursively) remove all files from a directory A that are identical (same name and same hash) to files in directory B while preserving the structure?

e.g. A/file1 gets removed if B/file1 exists; but A/file1 gets not removed although B/d/file1 exists


If you want all files in A to exist in B then you could simply use rsync with the --remove-source-files option, to add / update the files in B, with those in A, and then delete the original files from A. Or with a bit more hacking, running rsync in --dry-run mode, and piping the output through grep, xargs and rm -f, only compare the two directory structures, and delete the identical files, see:

Linux / Unix rsync: Delete Source File After Transfer

rsync --remove-source-files deletes source files one by one or after rsync completes?


You could do something like this. Please note this is untested code. I assume $A and $B are your two directories:

(cd $B && find . -type f -print) | (cd $A && while read f; do /bin/rm -f "${f}"; done;)

I just tried it, but echoing the command to stdout instead of running it, and it appears to work ok:

$ (cd $B && find . -type f -print) | (cd $A && while read f; do echo /bin/rm -f "${f}";done;)
/bin/rm -f ./1
/bin/rm -f ./2
/bin/rm -f ./3
/bin/rm -f ./4

If that appears to do what you want, run it again but omit the word "echo" in the command.

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.