Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
1  
You can use programs to find duplicated files like duff and then parse their output (instead of hardlink them). –  jofel Dec 18 '12 at 17:26
    
if python is fine then see codeidol.com/community/python/comparing-directory-trees/17542/… –  harish.venkat Dec 18 '12 at 18:54
add comment

2 Answers 2

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?

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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