Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 78 down vote accepted

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
share|improve this answer

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

share|improve this answer

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

share|improve this answer

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.)

share|improve this answer

cp -P a/myProject/.project b

see "man cp" for more information.

share|improve this answer
This doesn't seem to help. It only copies '.project' itself into b, but it doesn't copy 'myProject'. – dhblah Sep 14 '10 at 8:11

I use cpio in combination with find. Explanation
Ex 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.

Sorry for being late to the party and not using (always) native tools.

share|improve this answer

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

share|improve this answer
Doesn't add anything to the other 5 answers. – Deer Hunter May 15 '15 at 8:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.