The fastest way is just to compare hash code of files having same size.
This is the idea of of this answer on SOanswer on SO (see the second command line and its explanations).
There is no security issue while detecting duplicated files, therefore I would recommend a fast hashing code. For instance the project ccache uses MD4:
ccache uses MD4, a very fast cryptographic hash algorithm, for the hashing. (MD4 is nowadays too weak to be useful in cryptographic contexts, but it should be safe enough to be used to identify recompilations.)
If two files have same size and same hash code, they are probably equal. But there will still be a little chance these two files are different (except if file size is less than hash code size).
As you imply in your question, false positive can happen more frequently as the file size is larger.
There are two options to fix the large files issue:
- Use a second hash code for large file (e.g. MD4 and MD5).
- Use a dynamic-length hash code
The limit to consider a file as enough large to require a second check depends on how critical is your application.
Finally, the safest way to proceed is:
- Detect files having same size
- If same size => compare their (already computed) hash codes
- If same size and same hash code => compare the full content