Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

4 Answers 4

up vote 34 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

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

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

Your Answer

 
discard

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.