Buy Now

Clean up5 min read

How to find duplicate files on Mac using Terminal

Use find, shasum and sort to list identical files in any folder on your Mac, step by step, and review them before anything goes to the Trash.

Short answer

Go to the folder, compute a SHA-256 checksum for every file, sort the list, and print the files whose checksums repeat:

cd ~/Downloads
find . -type f -size +100k -print0 | xargs -0 shasum -a 256 | sort | awk '$1 == h { if (!n++) print "\n" p; print; next } { h = $1; p = $0; n = 0 }'

Each group in the output is a set of identical files. Nothing is deleted; you review the list and move extra copies to the Trash in Finder.

You don’t need an app to find exact duplicates. macOS ships with everything required, and the method is the same one dedicated tools use underneath: compare file contents by checksum. It takes a minute to set up and works on any folder.

How it works

A checksum (here, SHA-256) is a long string calculated from every byte of a file. Change one byte and the checksum changes completely. Two files with the same checksum have the same contents, whatever they’re called and wherever they are. So the plan is simple: fingerprint every file, then look for repeated fingerprints.

Step 1: Open Terminal in the right folder

Open Terminal from Applications › Utilities, or with Spotlight. Type cd and a space, drag the folder from Finder into the Terminal window to paste its path, and press Return:

cd ~/Downloads

Start with one folder, like Downloads or Documents, rather than your whole home folder. It’s faster, and the results are easier to review.

Step 2: Fingerprint every file

find . -type f -size +100k -not -path "*/.git/*" -print0 | xargs -0 shasum -a 256 > /tmp/hashes.txt
  • find . -type f lists every file in the folder and its subfolders.
  • -size +100k skips files under 100 KB. Tiny files rarely matter for space, and there are a lot of them. Change it to +1M to look only at bigger files.
  • -not -path "*/.git/*" skips Git’s internal files if you have code projects.
  • -print0 and xargs -0 keep file names with spaces in one piece.
  • shasum -a 256 prints each file’s checksum followed by its path, and > /tmp/hashes.txt saves the list.

Every file is read in full, so a folder with many gigabytes of video takes a while. The fan may spin up; that’s normal.

Step 3: Print the groups of duplicates

sort /tmp/hashes.txt | awk '$1 == h { if (!n++) print "\n" p; print; next } { h = $1; p = $0; n = 0 }'

sort puts lines with the same checksum next to each other. The awk part prints a line only when its checksum matches the line before, plus the first file of each group, with an empty line between groups. The output looks like this:

f900c6ec8d0c…  ./Contract.pdf
f900c6ec8d0c…  ./Contract (1).pdf
f900c6ec8d0c…  ./Clients/Acme/Contract signed.pdf

65e0383f0da9…  ./IMG_2210.MOV
65e0383f0da9…  ./Export/IMG_2210.MOV

To save the list for later, add > ~/Desktop/duplicates.txt at the end of the command.

Step 4: Review, then remove in Finder

For each group, decide which copy to keep: the one in the folder where it belongs, with the plain name, usually the oldest. To look at a file, reveal it in Finder:

open -R "./Clients/Acme/Contract signed.pdf"

Then drag the extra copies to the Trash in Finder. Avoid rm: it deletes immediately, with no Trash and no undo, and one mistyped path can remove the wrong thing.

When you’re done, remove the temporary list:

rm /tmp/hashes.txt

A faster option: jdupes

If you use Homebrew, jdupes does the same job faster, because it compares sizes first and only reads files that could match:

brew install jdupes
jdupes -r ~/Downloads

It prints groups of identical files. It also has options that delete files or replace them with links; leave those alone until you’re sure what they do, because they don’t use the Trash.

Where not to run it

Careful

Don’t run a checksum over iCloud Drive or other synced folders if files are stored only in the cloud: reading each file makes macOS download it. And don’t remove duplicates inside ~/Library, app bundles, the Photos library or Music library, or code projects. Those copies are there on purpose.

Or skip the commands

Findy runs the same kind of check in the background, with a review screen instead of a text list. It groups identical files by checksum, suggests which copy to keep (not the one in Downloads, not the one called “copy” or “(1)”), and shows each copy’s folder and size. You tick the groups you want gone, and the extras go to the Trash with Undo. It skips files that are only in iCloud, apps, and build folders in code projects, and it finds similar camera photos in a separate section that’s off until you turn it on.

Questions

Is MD5 good enough instead of SHA-256?

For finding duplicates on your own Mac, yes: md5 -r works the same way. SHA-256 is just as easy to run and has no known accidental collisions, so there’s no reason not to use it.

Can this find similar photos?

No. A checksum only matches identical files. A resized or re-saved photo has a completely different checksum. Similar images need a tool that compares what the pictures look like.

Will this command change or delete my files?

No. find, shasum, sort and awk only read and print. The only things written are the list in /tmp and the files you choose to save.