Script finding and copying files from large library

Discussion in 'Mac Programming' started by svantejerling, Jun 11, 2013.

  1. macrumors newbie

    Joined:
    Jun 11, 2013
    #1
    Hi, I am working on a photo book project.

    I have a folder with subfolders that together contains over 6M photos (jpegs)
    I have a list of filenames (roughly 3000) that I want copy to another folder.

    There are reoccurring filenames ex. IMG_0042.jpg need both files. The script shouldn't replace the reoccurring filename but rename it.

    I can manually remove false positives (ex. 2 out of three IMG_0042)
    I have the list of images I need in a text file

    Text file:
    IMG_6763.jpg
    IMG_0025.jpg
    DSC_0077.jpg
    DSC_0727.jpg
    IMG_3091.jpg
    IMG_9109.jpg
    IMG_4128.jpg

    Would really appreciate the help. And so would my poor assistant who would love to spend two weeks of her life doing other things than finding these pictures manually :)
     
  2. subsonix, Jun 12, 2013
    Last edited: Jun 12, 2013

    macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    So, you have a list of files that resides somewhere in a directory tree, you want to find them all and copy them to a specified location. Is that correct?

    What exactly do you mean by this? It seems like a completely different objective than finding and copying files.

    What is it you want to do exactly?
     
  3. thread starter macrumors newbie

    Joined:
    Jun 11, 2013
    #3
    Sorry for being unclear.
    1. They reside somewhere in a directory tree (2 folder deep)


    2. Reoccurring filenames: It just means that if there are two source files with the same name, the second one shouldnt overwrite the first.

    ex. There might be several IMG_0001 placed in different subfolders.
    If that filename is in the list then all such files should be copied into the target folder.
     
  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    This will copy the files files.txt from /source/directory to /target/directory.

    Code:
    #!/bin/bash
    
    while read line
    do
        find /source/directory -name "$line" -exec cp {} /target/directory \;
    done < files.txt
    
    If there are several files with the same name all of them will be copied but cp will overwrite existing files with the same name in the target directory with the above script.

    There are ways around this, there is a command called pax that can rename the files according to a regex pattern and for example add a numeric suffix to the name, but it's more involved. Have to look at that, the simplest case is if the filenames are unique, if you can fix that somehow.
     

Share This Page