Thursday, January 31, 2013

unix copy file and create directory if it doesn't exist

My goal was to re-create a directory tree of files under a new root by copying them, creating all the same directory levels. I found a convoluted command line for doing this using xargs and split and stuff, but it turns out that cp has the most wonderful argument, --parents. Here was the solution:

find . -name "regex" -exec cp -p --parents {} /newroot \;