Find Duplicate Files with ChatGPT: Count Content, Paths and Hard Links
Two filenames can point to identical content without representing two independent copies on disk. This tutorial uses ChatGPT to build a read-only duplicate-file report, then checks its output against a small Linux fixture. The important distinction is between matching paths, distinct files and logical repeated bytes.
What you need
Use Python 3 and a disposable folder on a Linux filesystem that supports hard links. Work with fictional files, as in the video. Keep a separate backup before adapting any file-management tool; our beginner’s backup guide explains why a duplicate report is not a replacement for recovery copies.
1. Give ChatGPT a precise reporting contract
Attach your fictional file listing and explain the directory layout. Request duplicates.py with find_duplicates(folder) and report(folder). Ask for the following behavior:
- Include hidden and empty regular files, recurse into real directories and skip symbolic links.
- Group candidates by size, hash their contents in chunks with SHA-256, then compare bytes to confirm equality.
- Return sorted relative paths. Count distinct files using the pair
(st_dev, st_ino). - Report only. Do not delete, rename or select a file to remove.
Python’s hashlib documentation describes incremental hashing. A matching digest helps narrow the candidates; the byte comparison in this lesson supplies the final content check. Read the entire returned program before executing it.
2. Create a controlled example
From a new practice directory, this creates three independent five-byte files, a hard-linked alias and two empty files. The directory creation deliberately fails if practice-files already exists.
python3 - <<'PY'
from pathlib import Path
import os
p = Path('practice-files')
p.mkdir()
(p / 'a').mkdir()
(p / 'c').mkdir()
for name in ['.hidden.txt', 'a/report.txt', 'c/copy.txt']:
(p / name).write_bytes(b'same!')
os.link(p / 'a/report.txt', p / 'a/report-alias.txt')
for name in ['empty-one.txt', 'empty-two.txt']:
(p / name).touch()
PY
python3 - <<'PY'
from duplicates import report
from pprint import pprint
pprint(report('practice-files'))
PY3. Reconcile the counts
The five-byte group should contain four paths but only three distinct files. Keeping one independent file leaves two repeated copies: (3 - 1) × 5 = 10 logical repeated bytes. Counting paths instead would incorrectly report fifteen bytes. The empty-file group has two distinct files and one repeated copy, but contributes zero logical bytes.
Add another hard link to a/report.txt. The path count should increase while the distinct-file count and ten-byte figure remain unchanged. Then create two independent files containing b'new!'. That new four-byte group adds four logical repeated bytes, bringing the combined total to fourteen. Files sharing a name but containing different bytes must remain separate.
4. Check preservation and state the limits
Compare source filenames and content hashes before and after each scan. None should change. The recorded lesson’s independent checker also exercises hidden files, empty files, symbolic links and the fresh pair.
Logical repeated bytes are not guaranteed reclaimable disk space. Sparse allocation, compression, reflinks and links outside the scan need separate accounting. This example assumes files are not changing concurrently. A deletion workflow would require its own backup, selection policy and recovery checks; this report makes no deletion decision.
Continue with the DrivesHero tutorial library for other source-checked software workflows.
James Kennedy is a writer and product researcher at Drives Hero with a background in IT administration and consulting. He has hands-on experience with storage, networking, and system performance, and regularly improves and optimizes his home networking setup.



