I have Eclipse projects and ".project" file in them, the directory structure looks like 'myProject/.project
'. I want to copy these '.project' files to another directory, but I want the enclosing directory name to be preserved.
Let's say I have 'a/myProject/.project
', I want to copy 'myProject/.project
' to 'b
', so it be 'b/myProject/.project
', but 'b/myProject
' doesn't exist. When I try in a:
cp -r ./myProject/.project ../b
it copies only '.project' file itself, without 'myProject' directory. Please advise.
The switch you need is
--parents
, e.g.:You can also use
rsync -R
, which works on OSX wherecp --parents
isn't available.https://stackoverflow.com/a/13855290/598940
Use
tar
with something like:(Not tested. Give it a dry run first or try in a mockup scenario.)
First use
mkdir -p
to create the destination folder with recursive parent path creation. Then copy the contents to the destination folder:I use cpio in combination with find. Explanation here.
Example for your use case:
This command finds all files in
/a/myProject/.project/
and copies, while preserving the path, any files contained within.See
man cp
for more information.Additionally to
--parents
it is also required to add-r
option in order to avoid omitting the copy of most inner directorySo the command that works for me is
Please be aware that there appears to be a bug in
cp --parents
. When I used--parents
along with--preserve=all
, the date and time attributes of SOME destination directories were NOT preserved.The following link seems to confirm that this is a bug: bug#8767: cp: --preserve=all doesn't work for the parents when --parent is used.
So it looks as though you can't rely on attributes being preserved when using
--parents
along with such as--preserve=all
or-p
.I used
--parents
with thecp
command and worked perfeclty with me. for more details always use the manual. Thank you.