Hash every file with SHA-256 and group by digest. Files in one group are byte-for-byte copies, whatever their names. On a real folder of 1,587 images (85,943,049 bytes) we found 2 duplicate groups and 3 redundant files, which wasted 589,696 bytes.

Why not compare names or sizes?

Names lie. The same photo gets copied into three folders under three names. Sizes lie in the other direction: two different photos can share a byte count by chance.

A SHA-256 digest of the bytes answers one question exactly: are these two files identical? Equal digests mean equal bytes for any practical purpose. This is also the idea behind content-addressed storage, which we covered in our article on content-addressed keys for transformed images.

What does the script look like?

Hashing every file means reading every byte. A cheaper first pass groups files by size, because two files with different sizes cannot be identical. Then it hashes only the files that share a size.

// dedupe-images.mjs: group files by SHA-256 and report the bytes the copies waste. import { createHash } from 'node:crypto'; import { readdir, readFile, stat } from 'node:fs/promises'; import { join } from 'node:path'; const root = process.argv[2] ?? 'public/images'; const IMG = /\.(jpe?g|png|webp|avif|gif|svg)$/i; async function* walk(dir) { for (const e of await readdir(dir, { withFileTypes: true })) { const p = join(dir, e.name); if (e.isDirectory()) yield* walk(p); else if (IMG.test(e.name)) yield p; } } // Cheap first pass: only files that share a byte size can be duplicates. const bySize = new Map(); let files = 0; for await (const f of walk(root)) { const { size } = await stat(f); files++; (bySize.get(size) ?? bySize.set(size, []).get(size)).push(f); } const groups = new Map(); let hashed = 0; for (const [size, list] of bySize) { if (list.length < 2) continue; for (const f of list) { hashed++; const h = createHash('sha256').update(await readFile(f)).digest('hex'); const key = `${h}:${size}`; (groups.get(key) ?? groups.set(key, []).get(key)).push(f); } } let wasted = 0, dupFiles = 0; const dups = [...groups.entries()].filter(([, l]) => l.length > 1); for (const [key, list] of dups) { const size = Number(key.split(':')[1]); wasted += size * (list.length - 1); dupFiles += list.length - 1; } for (const [key, list] of dups) { console.log(`${key.slice(0, 12)} ${key.split(':')[1]} bytes x ${list.length}`); for (const f of list) console.log(` ${f}`); } console.log(JSON.stringify({ files, sizeCollisionsHashed: hashed, duplicateGroups: dups.length, redundantFiles: dupFiles, bytesWasted: wasted })); 

The wasted bytes are size * (copies - 1). You keep one copy, so only the extras count.

What did it find?

We ran it on this site's image folder:

7889d569b639 269098 bytes x 3 images/index/compress-image-to-1mb-result.jpg images/index/compress-image-to-a-size-sample-photo.jpg images/tools/change-image-aspect-ratio-online-original.jpg 1d1156615fb5 51500 bytes x 2 images/convert/png-converter-sample-photo.webp images/convert/png-to-webp-converter-result.webp {"files":1587,"sizeCollisionsHashed":65,"duplicateGroups":2,"redundantFiles":3,"bytesWasted":589696} 

Bytes wasted by two duplicate groups: 538,196 and 51,500

One photo lived in three places under three names. Three more copies of nothing new is 538,196 bytes. The second group adds 51,500. Total: 589,696 bytes, about 0.7% of the folder. That is a small win on a tidy repo. On a folder that grew by copy and paste, expect more.

Does the size-first pass pay off?

Only 65 of 1,587 files shared a size with another file, so only 65 were read and hashed. Hashing all 1,587 reads 85,943,049 bytes. Over five runs, hashing everything took 156, 162, 199, 324 and 545 ms in-process (median 199 ms). The size-first script took 0.07 to 0.08 s wall time per run, Node start included, in all five runs.

Those two timings use different clocks, so treat them as "same order, size-first is not slower". The win grows with folder size and with slow disks.

What do you do with the duplicates?

Three choices, in order of risk:

  1. Report only. Print the groups and let a human decide. This is what the script does.
  2. Replace with a hard link or symlink. Same bytes on disk once, every path still works.
  3. Delete extras and rewrite references. Best for a web project, since one canonical path is easier to cache. It needs a search for every path you remove.

Note: Identical bytes only. A JPEG and a WebP of the same picture, or a photo saved at two qualities, hash differently. Finding those needs a perceptual hash or a similarity score, which is a different tool.

After deduping, shrink what is left. Our guide on image optimization lists the next steps, and the how we measure page explains how we count bytes.

How I measured

  • Date: 2026-10-18. Machine: Apple M1 Pro, macOS 15.7.3.
  • Node 24.22.0, sharp 0.35.4, libvips 8.18.6.
  • KB = 1,000 bytes. Raw numbers are in measured.json next to this article.
  • The script above on apps/web/public/images (read only). In-process hash-all timing: median of 5 runs. Wall time: /usr/bin/time, 5 runs.

What I did not test

I did not delete or link anything. I did not test near-duplicates (resized or re-encoded copies).

I build Blinkimg, an image compressor and converter with a developer API. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.