Skip to content

Duplicate File Finder

In plain English

Scans folders to spot exact duplicate files wasting disk space by matching file sizes first and digital fingerprints second.

A Python 3.12 automation toolkit pairing size-filtered SHA-256 duplicate detection, CSV-to-JSON sanitisation, and SQLite audit logging.

1. Why I built it

Created to replace ad-hoc filesystem scripts with explicit symlink guards, CSV schema checks, and persistent execution audit trails.

Problem being solvedFull-tree hashing wastes I/O on unique file sizes, symlinks introduce traversal hazards, and raw CSV scripts drop header errors silently.

2. How it works

Implemented `AuditLedger`, `compute_sha256()`, `scan_duplicates()`, and `sanitize_and_export_csv()` in `src/automation_toolkit/workflows.py`.

Hashing every file across a folder wastes disk reads on unique byte lengths. `scan_duplicates()` validates root directory existence, skips symlinks (`not entry.is_symlink()`), and buckets regular files by `st_size` so unique sizes never invoke `hashlib.sha256()`.

Only size-collision groups read `64 KiB` chunks sequentially to compute SHA-256 digests, separating identical-size differing contents while totalling exact `reclaimable_bytes`.

`sanitize_and_export_csv()` validates file presence and required CSV header columns, rejects blank `dedupe_key` values, strips duplicates case-insensitively, writes sorted JSON, and logs counts inside SQLite `AuditLedger`.

3. Output & Source Specimen

Two-Stage Deduplication & Audit Flow

01Pass 1 -> root_dir.rglob('*'): skip symlinks & directories, bucket by st_size
02Pass 2 -> Only groups with len(paths) > 1 stream 64 KiB chunks into hashlib.sha256
03CSV -> Verify required columns + non-empty dedupe_key; case-insensitive dedupe
04Ledger -> AuditLedger.record_run() writes workflow_runs with UTC timestamp & JSON details
Directory filter, digest pass, and AuditLedger persistence in workflows.pyRun checks: pytest -v tests/test_workflows.py

4. Design Decisions & Challenges

Key Design Decisions

  • 01.Grouped regular files (excluding symlinks) by `st_size` before reading contents for SHA-256.
  • 02.Checked `input_csv.is_file()`, required header subsets, and non-empty `dedupe_key` values ahead of JSON export.
  • 03.Streamed files in 65,536-byte chunks for constant memory usage across large binaries.

Edge Cases Handled

  • Separating equal-byte-size files whose contents differ during digest grouping while guarding symlinks cleanly.

5. Testing & Limits

Automated Checks

Verified with 6 pytest checks in `tests/test_workflows.py` testing duplicate detection, `--min-size` filtering (`skipped_below_min_size`), equal-size differing payloads, symlink/directory guards, CSV validation, and CLI `--ledger` recording.

Current Limits & Next Steps

  • Same-size large files are read completely; adding a fast 4 KiB head/tail pre-check would filter diverging giants even faster.

6. Key Takeaways

What building Duplicate File Finder reinforced

Cheap metadata bucketing (`st_size`) eliminates unnecessary I/O while explicit header validation prevents silent data corruption.

  • — 6 pytest suites and Ruff checks pass cleanly in CI.
  • — Source hosted at github.com/16lv/sha256-file-deduplicator.

Recent Activity

Latest repository updates

All changes for 16lv/sha256-file-deduplicator

feat: expose --ledger recording and clean exit code 2 handling in CLI

Added optional --ledger SQLite persistence on sha256-dedupe scan and sanitize CLI workflows.

2026-11-244a86687

feat: add --min-size threshold filter for duplicate file scan

Added min_size_bytes filtering and skipped_below_min_size metrics to scan_duplicates() and sha256-dedupe scan.

2026-11-23f313eec

Add python -m automation_toolkit runner and verify editable install in CI

Committed directly to sha256-file-deduplicator (main) · Scans folders to spot exact duplicate files wasting disk space by matching file sizes first and digital fingerprints second.

2026-11-216db866b