Copy specific files and keep the subfolder structure

I got a folder with a large amount of files with different suffixes in several sub folders.

My automator workflow works as follows:

  1. Ask for Finder Items
  2. Get Folder Contents (sub folder option checked)
  3. Filter Finder Items (for a specific file-suffix)
  4. Copy Finder Items

This works like a charm. But all files are copied in the same root destination folder. I need to keep the the sub folder structure. For Example:

sourceFolder
    subfolderA
        fileA.jpg
        fileB.xls
        fileC.xls
    subfolderB
        fileC.jpg
        fileD.xls
        fileE.xls

Expected result (filter for file suffix .xls):

destinationFolder
    subfolderA
        fileB.xls
        fileC.xls
    subfolderB
        fileD.xls
        fileE.xls

Current result:

destinationFolder
    fileB.xls
    fileC.xls
    fileD.xls
    fileE.xls

Update

I'm a developer. So a shell or AppleScript will be also accepted. :)


rsync -avh --include='*/' --include='*.xls' --exclude='*' path/to/sourceFolder/ path/to/destinationFolder

Note the / at the end of the source path, it's important. This solution would add all empty directories, if you don't want them, look at the option --prune-empty-dirs.

With brace expansion you can actually include more filetypes at once, rather than having

--include='*.xls' --include='*.pdf' --include='*.txt'

you can use

--include='*.'{xls,pdf,txt}

I end up with a shell script, that works as requested:

$ find ./sourceFolder -name "*.xls*" -type f -print0 | xargs -0 -I '{}' /usr/bin/rsync -avR "{}" "./destinationFolder/"