Deleting duplicate files in folder


I take a lot of pics on my iPhone and Airdrop them to my MBP. Sometimes I get duplicates and have to manually go back and delete them.

It possible to write a command line script (or anything) that I can run in the current folder to delete duplicates or copy them to a new sub folder so I can manually delete that folder?

The files are usually .HEIC or .JPG and below is an example of a duplicate file name. There could even be triplicates. So I suppose any file with the same name “xyz[SPACE]*” would be deleted.

IMG_6003 2.HEIC


Plain AppleScript version, without searching subfolders:

set aFolder to choose folder

tell application "Finder"
	set theFiles to every file of aFolder as alias list
	set countFiles to count theFiles
	repeat with i from 1 to countFiles - 1
		set aFile to item i of theFiles
		set aNameCount to count (name of file aFile)
		set aFilePath to POSIX path of aFile
		set MD5checkSumA to last word of (do shell script "md5 '" & aFilePath & "'")
		repeat with j from i + 1 to countFiles
			set bFile to item j of theFiles
			set bFilePath to POSIX path of bFile
			if last word of (do shell script "md5 '" & bFilePath & "'") = MD5checkSumA then
				if aNameCount < (count (name of file bFile)) then
					move bFile to trash
					move aFile to trash
					exit repeat
				end if
			end if
		end repeat
	end repeat
end tell

Seems to do the trick. Thank you!

Just installed this and it seems to work amazingly well:

You can install via homebrew with ‘brew install fdupes’