← All Exercises

Find Duplicate Files by SHA-256 Hash

intermediatebash~20min

Background

Storage cleanup and backup verification both need content-level duplicate detection. Names and sizes are unreliable; a cryptographic digest identifies equal bytes.

Problem

Given a directory, find groups of regular files with identical contents. Print each path that belongs to a duplicate group, including every member, with one path per line.

Input

A readable directory path. File names may contain spaces, and the directory may contain no duplicates or no files at all.

Output

The paths of all files whose SHA-256 digest is shared by at least one other file, sorted by digest then path; no output when no duplicates exist.

Sample tests

Sample 1

Input: directory /data with a.txt and copy/a.txt having identical content, plus b.txt with different content
Expected: /data/a.txt
/data/copy/a.txt

Sample 2

Input: directory /data where every file has different content
Expected: no output

Your solution

Learning objective

Combine null-delimited find output, cryptographic hashing, sort keys, and streaming group detection.