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.

In Ubuntu, is there any to find duplicate folders in a directory (i. e., folders with the same content)? I think there are already some command-line tools available for finding duplicate files (such as fdupes), but I want to find duplicate folders instead. That is, find folders which match in terms of the contents of the files they contain (though the filenames and other metadata might differ).

share|improve this question
    
I might start by generating a list of all folders in a directory (sorted by length), and then check each pair of folders with the same length. –  Anderson Green Dec 12 '12 at 21:49
    
Define "duplicate". Must the files inside match merely file content? File name? Inode number? File size? –  Chris Down Dec 12 '12 at 21:50
    
@ChrisDown The question has been updated. –  Anderson Green Dec 12 '12 at 21:51
3  
Yes. Directories are really just files, so your statement is ambiguous. To have the "same content" in reality would mean that the directories both contain the same inode references. It is unclear whether you mean that, or whether you mean that the files inside should have the same content, and if so, whether there are other stipulations (mtime, filename, etc). –  Chris Down Dec 12 '12 at 21:56
3  
@ChrisDown I mean that the files inside should have the same content. –  Anderson Green Dec 12 '12 at 21:57
show 5 more comments

1 Answer

up vote 4 down vote accepted
#!/bin/bash
shopt -s dotglob

for file in "$1"/*; do [[ -f "$file" ]] && d1+=( "$(md5sum < "$file")" ); done
for file in "$2"/*; do [[ -f "$file" ]] && d2+=( "$(md5sum < "$file")" ); done 

[[ "$(sort <<< "${d1[*]}")" == "$(sort <<< "${d2[*]}")" ]] && echo "Same" || echo "Different"

You can see it in action here:

$ mkdir 1 2
$ ./comparedirs 1 2
Same
$ cat > 1/1 <<< foo
$ cat > 2/1 <<< foo
$ ./comparedirs 1 2
Same
$ cat > 2/1 <<< bar
$ ./comparedirs 1 2
Different
share|improve this answer
    
Since this script is untested, I'm eager to see whether it works the way it's supposed to work. –  Anderson Green Dec 12 '12 at 22:08
1  
@AndersonGreen Check the updated answer, tested it. –  Chris Down Dec 12 '12 at 22:13
    
Nice! There should also be a test with cat > 1/2 <<< bar and cat > 2/3 <<< bar to show multiple files and differing metadata (== "Same") –  nealmcb Jun 19 at 3:59
    
@ChrisDown: does sort in last step needed? –  harish.venkat Jun 19 at 5:49
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.