We want to find duplicate files, but can’t rely on their names
Comparing pairs of files byte by byte is slow
Better approach:
Calculate an identifier for each file that depends on its content
Group files with the same identifier and compare those
Even better: calculate identifiers so that if two files have the same ID, they’re guaranteed to have the same content
Take filenames as command-line arguments
Generate and print a list of duplicate pairs
[%inc brute_force_1.py mark=main %]
[%inc brute_force_1.py mark=bytes %]
open(filename, "r") opens a file for reading
characters
But images, audio clips, etc. aren’t character data
So use open(filename, “rb”) to open
in [% g binary_mode “binary mode” %]
tests directory with six filesa1.txt |
a2.txt |
a3.txt |
b1.txt |
b2.txt |
c1.txt |
|---|---|---|---|---|---|
| aaa | aaa | aaa | bb | bb | c |
Expect the three a files and the two b
files to be reported as duplicates
No particular reason for these tests—we just have to start somewhere
[%inc brute_force_1.sh %] [%inc brute_force_1.out %]
[%inc brute_force_2.py mark=dup %]
[% figure slug=“dup-triangle” img=“triangle.svg” alt=“Looping over distinct combinations” caption=“Scoping the inner loop to produce unique combinations.” %]
\( N \) objects can be paired in \( N(N-1)/2 \) ways
So for very large \( N \), work is proportional to \( N^2 \)
Computer scientist would say “[%g time_complexity ”time complexity” %] is \( O(N^2) \)”
In practice, this means that the time per file increases as the number of files increases
Sometimes unavoidable, but in this case there’s a better way
Process each file once to produce a short identifier
I.e., use a [% g hash_function “hash function” %] to produce a [% g hash_code “hash code” %]
Only compare files with the same identifier
[% figure slug=“dup-hash-group” img=“hash_group.svg” alt=“Grouping by hash code” caption=“Grouping by hash code reduces comparisons from 15 to 4.” %]
[%inc naive_hash.py mark=hash %]
[%inc naive_hash.py mark=example %]
[%inc naive_hash.out %]
Want all hash codes to be equally likely
Hash each line of the novel Dracula and plot distribution
[% figure slug=“dup-naive-dracula” img=“naive_dracula.svg” alt=“Hash codes of Dracula” caption=“Distribution of hash codes per line in Dracula.” %]
Our text file uses a blank line to separate paragraphs
Look at the distribution of hash codes of unique lines
[% figure slug=“dup-naive-dracula-unique” img=“naive_dracula_unique.svg” alt=“Hash codes of unique lines of Dracula” caption=“Distribution of hash codes per unique line in Dracula.” %]
[%inc grouped.py mark=group %]
[%inc grouped.py mark=main %]
[%inc grouped.out %]
Use a [%g cryptographic_hash_function “cryptographic hash function” %]
Output is completely deterministic
But also unpredictable
And distributed like a uniform random variable
Output depends on order of input as well as value
Odds that two people don’t share a birthday are \( 364/365 \)
Odds that three people don’t have are \( (364/365) {} (363/365) \)
There’s a 50% chance of two people sharing a birthday in a group of 23 people and a 99.9% chance with 70 people
How many files do we need to hash before there’s a 50% chance of a [%g hash_collision “collision” %] with a 256-bit hash?
Answer is “approximately \( 4{}10^{38} \) files”
We’re willing to take that risk
[%inc using_sha256.py mark=example %]
[%inc using_sha256.out %]
hexdigest gives [%g hexadecimal “hexadecimal” %]
representation of 256-bit hash code[%inc dup.py %]
[%inc dup.sh %] [%inc dup.out %]
Runtime is \(O(N)\), i.e., fixed time per file
Which is as good as it can possibly be
What else can we use hashing for?
Dictionaries
Version control
How can we test our duplicate finder?
How can we make sure the code follows style guidelines?
How can we package it for others to use?
[% figure slug=“dup-concept-map” img=“concept_map.svg” alt=“Concept map for finding duplicate files” caption=“Concept map.” %]