I was hoping:
cp -R src/prog.js images/icon.jpg /tmp/package
would yield a symmetrical structure in the destination dir:
/tmp | +-- package | +-- src | | | +-- prog.js | +-- images | +-- icon.jpg
but instead, both of the files are copied into /tmp/package. A flat copy. (This is on OSX).
Is there a simple bash function I can use to copy all files, including files specified by wildcard (e.g. src/*.js) into their rightful place within the destination directory. A bit like "for each file, run mkdir -p $(dirname "$file"); cp "$file" $(dirname "$file")
", but perhaps a single command.
This is a relevant thread, which suggests it's not possible. The author's solution isn't so useful to me though, because I would like to simply provide a list of files, wildcard or not, and have all of them copied to the destination dir. IIRC MS-DOS xcopy does this, but there seems to be no equivalent for cp.
In order to copy the content of a directory recursively, you have to use the “cp” command with the “-R” option and specify the source directory followed by a wildcard character.
Overview. We know we can copy a directory recursively using the cp command and the -R option. In this way, the source directory, together with all the files under it, will be copied to the destination.
To copy a directory with all subdirectories and files, use the cp command.
Copying Directories with cp Command To copy a directory, including all its files and subdirectories, use the -R or -r option. The command above creates the destination directory and recursively copy all files and subdirectories from the source to the destination directory.
Have you tried using the --parents option? I don't know if OS X supports that, but that works on Linux.
cp --parents src/prog.js images/icon.jpg /tmp/package
If that doesn't work on OS X, try
rsync -R src/prog.js images/icon.jpg /tmp/package
as aif suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With