It always have a need for this simple operation.
I wanna copy certain files to a destination, but I wanna preserved the sub directories structure, any cp option to set? For examples, I wanna copy my_project/cpp/hello.cc to your_project folder, but I want it to appear as your_project/cpp/hello.cc automatically. Especially I wanna do it in a batch of file.
By just using cp command, until today, I do not see such option for it.
You need additional commands, dirname and mkdir.
You may read a list of file from a file like this
filelist="myfilelist.txt" while read line do # do your copy over here done < $filelist
First you extract the directory name of the file you wanna copy, and then create the directory, lastly copy over the file.
filelist="myfilelist.txt" # assume your filename with relative path. target="your_project" while read line do target_dir=`dirname $line` mkdir -p $target_dir cp $line $target$target_dir done < $filelist
P.S. The snapshot of the code above might not work for all case, it depends on your filename in your file list, but gives you and idea on how to copy files and create directories if it doesn’t exist.