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).
-
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 GreenDec 12, 2012 at 21:49
-
Define "duplicate". Must the files inside match merely file content? File name? Inode number? File size?– Chris DownDec 12, 2012 at 21:50
-
@ChrisDown The question has been updated.– Anderson GreenDec 12, 2012 at 21:51
-
3Yes. 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 DownDec 12, 2012 at 21:56
-
3@ChrisDown I mean that the files inside should have the same content.– Anderson GreenDec 12, 2012 at 21:57
1 Answer
#!/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
-
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
-
Nice! There should also be a test with
cat > 1/2 <<< bar
andcat > 2/3 <<< bar
to show multiple files and differing metadata (== "Same")– nealmcbJun 19, 2014 at 3:59 -
-
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.– cosineSep 11, 2015 at 10:26