How to cull duplicates from Spotlight search

Discussion in 'macOS' started by BrianBaughn, Jul 22, 2015.

  1. BrianBaughn, Jul 22, 2015
    Last edited: Jul 22, 2015

    BrianBaughn macrumors 601

    BrianBaughn

    Joined:
    Feb 13, 2011
    Location:
    Baltimore, Maryland
    #1
    Goal: I want to search a external partition for files/folders named or containing one or more of a couple of words and then copy those files to a different location (folder).

    Situation: Spotlight search pulls up duplicate files with duplicate names, which I expected. What I don't want is to copy duplicates.

    Problem: Dragging results from Spotlight to the new location copies everything, renaming the dupes (for example: two copies of myfile.doc copy over as myfile.doc and myfile 2.doc)

    Does anyone know how to get the finder to copy one of the dupes only...that is...have only one version copy over?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    I'd probably use the shell command line.

    The 'mdfind' command searches the same database as Spotlight. See its man page for options.

    Assuming mdfind outputs duplicate pathnames, you can pipe its output through sort and uniq to produce a list with no duplicates.

    I don't know how well any of that will work for you, but I think it's more likely to be tweakable than trying to customize the GUI Spotlight search.
     
  3. BrianBaughn thread starter macrumors 601

    BrianBaughn

    Joined:
    Feb 13, 2011
    Location:
    Baltimore, Maryland
    #3
    The pathnames won't be duplicates.

    The main problem is that the partition contains Time Machine backups for a machine that's not here. It's all the hard links that are creating the issue of duplicates.

    So I guess another way to solve this would be to have a search that disregards hard links...
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Disregarding hard-links might be tricky. You'd have to list the first hard-link, but skip all the others. That's a bit of a puzzle without a way to uniquely identify files (as distinct from filenames).

    Luckily, inode numbers can do this.

    All hard-links to the same file will have the same inode number (you can test this with the 'ln' command in Terminal). The 'ls -i' command will print the inode number. So go through the list produced by mdfind (or whatever), ls -i each item, then sort the resulting list on field 1 (see man sort). An awk script can then discard all but the first file with the same inode number, or you might be able to figure out a 'uniq' command-line.

    The 'uniq' command can also ignore N leading chars, so if the hard-links are all in the same sub-tree relative to a particular date's backup, then ignore the leading "backup date" part of the pathname, but keep the "sub-tree within backup" part.
     
  5. BrianBaughn thread starter macrumors 601

    BrianBaughn

    Joined:
    Feb 13, 2011
    Location:
    Baltimore, Maryland
    #5
    Thanks for replying. Now I'm thinking I'm just going to have to let this slide.

    I did think of another idea, though. If I select all of my search results and Option-Command-Drag aliases to a new folder, one alias has the file name and all the dupes have "alias" in the name...and those are easy to weed out.

    So...if I have a folder full of aliases how would I get the originals onto a flash drive in one-fell-swoop?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    I think it would take an AppleScript, unless there's some utility on the App Store that does exactly that.

    Resolving symlinks from the command-line is pretty easy. But aliases aren't symlinks, and AppleScript is the only thing I can think of that can "get original" on it.
     
  7. BrianBaughn thread starter macrumors 601

    BrianBaughn

    Joined:
    Feb 13, 2011
    Location:
    Baltimore, Maryland
    #7
    I think I found an answer to that: via a DVD Burn. If you option-drag a folder of aliases to a burn folder, the originals will be copied to the DVD. From there you can go to the flash drive.
     

Share This Page