10

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).

10
  • 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. Dec 12, 2012 at 21:49
  • Define "duplicate". Must the files inside match merely file content? File name? Inode number? File size?
    – Chris Down
    Dec 12, 2012 at 21:50
  • @ChrisDown The question has been updated. Dec 12, 2012 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, 2012 at 21:56
  • 3
    @ChrisDown I mean that the files inside should have the same content. Dec 12, 2012 at 21:57

1 Answer 1

5
#!/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
6
  • Since this script is untested, I'm eager to see whether it works the way it's supposed to work. Dec 12, 2012 at 22:08
  • 1
    @AndersonGreen Check the updated answer, tested it.
    – Chris Down
    Dec 12, 2012 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, 2014 at 3:59
  • @ChrisDown: does sort in last step needed? Jun 19, 2014 at 5:49
  • Elegant script, only minor bug is that it returns Same when either or both directories do not exists. Should be easily fixable if one is better in scripting than I.
    – cosine
    Sep 11, 2015 at 10:26

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.