Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
@ChrisDown I mean that the files inside should have the same content. – Anderson Green Dec 12 '12 at 21:57
up vote 5 down vote accepted
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
$ cat > 1/1 <<< foo
$ cat > 2/1 <<< foo
$ ./comparedirs 1 2
$ cat > 2/1 <<< bar
$ ./comparedirs 1 2
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
@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 '14 at 3:59
@ChrisDown: does sort in last step needed? – harish.venkat Jun 19 '14 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 '15 at 10:26

Your Answer


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.