Deleting duplicate files in folder

Hi-

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.HEIC
IMG_6003 2.HEIC

Thanks!

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
				else
					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:
https://github.com/adrianlopezroche/fdupes

You can install via homebrew with ‘brew install fdupes’