How to copy file preserving directory path in Linux?
Solution 1:
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
Solution 2:
You can also use rsync -R
, which works on OSX where cp --parents
isn't available.
https://stackoverflow.com/a/13855290/598940
Solution 3:
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.)
Solution 4:
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
Solution 5:
cp -P a/myProject/.project b
See man cp
for more information.