1

I having difficulty locating some macOS software or command line techniques that will look for files that have duplicate file names without regard to the file contents.

Most software I've tried compares several things, including things like file sizes, checksums, modification dates, etc. For my purposes, I do not want to compare anything except the filename.

For example, below could represent the results of a scan of three subfolders that contain several files that have the identical filenames. Assume here that the contents of these files and their metadata (creation dates, etc.) are completely different.

2016 Travel/
    Document1.doc
    IMG_0001.jpg
    Untitled.txt
Work Documents/
    IMG_0001.jpg
    John Smith.vcf
    Untitled.txt
My Downloads/
    Document1.doc
    John Smith.vcf

Can you recommend some software that will do this sort of thing?

1 Answer 1

2

You can do this quite simply with a python or in the python console, (python is preinstalled on macOS IIRC or is available for just about any platform from python.org.

The following is from an interactive session but could as easily be put into a script.

import os
import collections
import sys

# Dictionary for lists of paths where each name is found
npdict = collections.defaultdict(list)    

# We need to collect all of the names with where they are found
startfrom = '.'  # This could be taken from input arguments in a script
for root, dirs, files in os.walk(startfrom):  # Walk the file structure 
    if '.git' in dirs:  # We don't 
        dirs.remove('.git')
    for fn in files:
        npdict[fn].append(root)

# Now to find the duplicates by making a dictionary of filenames 
# that have more than one path
dups = {fn:pths for fn, pths in npdict.items() if len(pths) > 1}

# For the moment just print them out
for fn, pths in dups.items():
    print('Filename:', fn, 'found in:')
    for pth in pths:
         print('\t', pth)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.