119

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.

splattne
  • 28,348
  • 19
  • 97
  • 147
dhblah
  • 1,293
  • 2
  • 9
  • 7

9 Answers9

209

The switch you need is --parents, e.g.:

jim@prometheus:~$ cp --parents test/1/.moo test2/
jim@prometheus:~$ ls -la test2/
total 42
drwxr-xr-x   3 jim jim    72 2010-09-14 09:32 .
drwxr-xr-x 356 jim jim 43136 2010-09-14 09:32 ..
drwxr-xr-x   3 jim jim    72 2010-09-14 09:32 test
jim@prometheus:~$ ls -la test2/test/1/.moo
-rw-r--r-- 1 jim jim 0 2010-09-14 09:32 test2/test/1/.moo
John Kugelman
  • 103
  • 12
James Yale
  • 5,042
  • 1
  • 16
  • 20
31

You can also use rsync -R, which works on OSX where cp --parents isn't available.

https://stackoverflow.com/a/13855290/598940

alecbz
  • 409
  • 4
  • 7
10

Use tar with something like:

mkdir b; tar cpf - myProject/ | tar xpf - -C b/

(Not tested. Give it a dry run first or try in a mockup scenario.)

John Kugelman
  • 103
  • 12
lorenzog
  • 2,719
  • 1
  • 18
  • 24
  • works surprisingly well! On mac: mkdir b; tar -c -f new.tar $(cat myP); tar -x -f new.tar -C b/; #myP is text file with list of paths-to-files to be copied – alexey Aug 23 '16 at 21:14
5

First use mkdir -p to create the destination folder with recursive parent path creation. Then copy the contents to the destination folder:

mkdir -p b/myProject/.project
cp -r a/myProject/.project/file b/myProject/.project
techraf
  • 4,163
  • 8
  • 27
  • 44
Weimin
  • 51
  • 1
  • 1
4

I use cpio in combination with find. Explanation here.

Example for your use case:

find /a/myProject/.project/ -type f | cpio -p -dumv /b/.

This command finds all files in /a/myProject/.project/ and copies, while preserving the path, any files contained within.

techraf
  • 4,163
  • 8
  • 27
  • 44
  • 1
    .project is a file, not dir! – ddbug Oct 15 '16 at 11:12
  • This is most flexible as you can pick the files/directories to be copied, discard the -v option to avoid verbose outputs, and add the --quiet option can suppress the "xx blocks" at the end. – jasxun Jul 09 '21 at 23:17
3
cp -P a/myProject/.project b

See man cp for more information.

techraf
  • 4,163
  • 8
  • 27
  • 44
Maxfer
  • 193
  • 10
  • This doesn't seem to help. It only copies '.project' itself into b, but it doesn't copy 'myProject'. – dhblah Sep 14 '10 at 08:11
2

Additionally to --parents it is also required to add -r option in order to avoid omitting the copy of most inner directory

$ cp --parents test/1/.moo test2/
cp: omitting directory ‘test/1/.moo’

So the command that works for me is

$ cp --parents -r test/1/.moo test2/
Katherine Villyard
  • 18,510
  • 4
  • 36
  • 59
cml.co
  • 121
  • 3
1

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.

techraf
  • 4,163
  • 8
  • 27
  • 44
chandra67
  • 11
  • 1
-3

I used --parents with the cp command and worked perfeclty with me. for more details always use the manual. Thank you.

sys0dm1n
  • 1
  • 1