Question

How to copy files from one directory to another while preserving the directory structure in mac?

I found that you can use cp --parents in ubuntu but unfortunately that doesn't work in mac.

Was it helpful?

Solution

I ended up using rsync -R to solve this.

OTHER TIPS

On OS X you can use ditto <source> <destination>

See here: http://osxdaily.com/2014/06/11/use-ditto-copy-files-directories-mac-command-line/

I'm tired of writing this manually, so I'm going to provide a non rsync way for future reference.

#!/bin/bash
cpParents() {
    src=(${*: 1:-1})
    dest=${*: -1:1}
    for filename in $src; do
        [ -e "$filename" ] || continue
        dirPath=$(dirname "${filename}")
        mkdir -p $dest/$dirPath
        cp $filename $dest/$dirPath
    done
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top