Ross Rogers

blog projects resume

Flattening a file tree with cygwin

17 Sep 2014

I had a bunch of old, physical pictures that someone had scanned into a bunch of folders and I needed to move all the of these pictures into one directory to work with a digital picture frame. However, the file names were not unique across all directories, thus I needed to copy files, but change their names so they don't conflict with any other file. If I used the md5 hash of the picture as the file name, then the file names are almost guaranteed to be unique.

Cygwin to the rescue:

while [ 1 ] ; do  
    find Rogers\ Family\ Photos/ -iname '*jpg'   \
        -exec bash -c -x 'mv -f "{}" $(md5sum "{}"| cut -c -32).jpg' \; ; 

Fits the bill. Renames all the files according to their md5sum and, as an added bonus, jumbles up the pictures so the digital picture device iterates through the different years of photos randomly.