Bash Diff – How to Get the Opposite of `diff -q` for Matching Identical Files

bashdiff()

I have a number of files in a directory, and I want to check that they are all unique. For simplicity, let's say I have three files: foo.txt, bar.txt and baz.txt. If I run this loop, I will check them all against each other:

$ for f in ./*; do for i in ./*; do diff -q "$f" "$i"; done; done
Files bar.txt and baz.txt differ
Files bar.txt and foo.txt differ
Files baz.txt and bar.txt differ
Files baz.txt and foo.txt differ
Files foo.txt and bar.txt differ
Files foo.txt and baz.txt differ

For the hundreds of files I want to deal with, this would become pretty unreadable; it would be better to list the files that do match, and then I can look over the list quickly and make sure that files are only matching themselves. From the manpage, I would have thought that the -s option would accomplish this:

$ for f in ./*; do for i in ./*; do diff -s "$f" "$i"; done; done
Files bar.txt and bar.txt are identical
Files baz.txt and baz.txt are identical
Files foo.txt and foo.txt are identical

…however, in fact it also prints out the whole contents of any files that differ. Is there any way to suppress this behaviour, so I only get the behaviour above?

Alternatively, is there some other tool that can accomplish this?

Best Answer

If you just want to check whether two files are identical or not, use cmp. To get an output only for identical files, you could use

for f in ./*; do for i in ./*; do cmp -s "$f" "$i" && echo "Files $f and $i are identical"; done; done

diff tries to produce a short, human-readable list of the differences, and this can take quite a lot of time, so avoid the overhead if you don't need it.

Related Question