LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   copy and create parent dirs (https://www.linuxquestions.org/questions/linux-general-1/copy-and-create-parent-dirs-4175680259/)

ychaouche 08-11-2020 09:57 AM

copy and create parent dirs
 
Dear LQ,

Say you want to copy /usr/share/zoneinfo inside of another directory (a jail, for context, but that's irrelevant), say /var/www/clients/client1/web5/, but you want to create all the parents of zoneinfo inside that destination directory, so you'd end up having the contents inside /var/www/clients/client1/web5/usr/share/zoneinfo, all the parent directories have to be created.

Is there a way to achieve this in one command ? (cp or rsync with some options maybe ?)

I know you can always do a mkdir -p /var/www/clients/client1/web5/usr/share/zoneinfo beforehand but I was just curious to know if this can be achieved otherwise by a relevant copy command

rtmistler 08-11-2020 10:51 AM

Hi,

I'd say I either am misreading, or I do not understand what you wish to do.

To me why isn't "cp -R" satisfactory? I mean there are switches to preserve, copy, whatever any sym links. I think it's either --preserve or -p or -P, have to look it up, plus also there's a -a option. I do recall having a situation (script) which copied a tree onto a new disk where I was establishing the disk otherwise and dd just wouldn't do it for me at the time, so I used a "cp -a" form.

I mean, ... you seem to want to copy a directory tree. Is that not what things like cp and rsync do for you? Or have you not explored staging this as yet?

ychaouche 08-11-2020 11:10 AM

dear rtmistler,

cp -R/-r /tmp/foo/bar/baz /tmp2/ will copy the baz directory directly under /tmp2/. I want to copy it inside /tmp2/foo/bar/, without creating /tmp2/foo/bar myself -the command should do it for me-

I haven't found any option for cp that do that (even --parents, which only seems to work for a single file, not for a whole directory).

But it seems rsync has a nice --relative option that will create the necessary parent directory to match those of the source directory.

I might as well mark this thread as solved, but other solutions are welcome :)

ondoho 08-11-2020 11:34 AM

Do you want to merge two existing & identical (or, let's say, compatible) directory structures with all files that are in it?

ychaouche 08-11-2020 11:37 AM

I wouldn't say "merge". I like how rtmistler uses the term "directory tree" or directory structure. If I say merge then that would imply the directory structure already exists on both paths, which is not the case (only exists in the source, but hasn't been created on the destination yet).

MadeInGermany 08-13-2020 02:05 PM

I usually do a "tar | tar"
Code:

( cd / && tar cf - usr/share/zoneinfo ) | ( cd /var/www/clients/client1/web5 && tar xf - )
The arguments following the "tar cf - " are stored in the tar stream and extracted by the "tar xf - " relative to its current directory.
For example, if I want to skip the /usr then I do
Code:

( cd /usr && tar cf - share/zoneinfo ) | ( cd /var/www/clients/client1/web5 && tar xf - )
and it will create /var/www/clients/client1/web5/share/zoneinfo

rnturn 08-13-2020 03:25 PM

Quote:

Originally Posted by ychaouche (Post 6154816)
dear rtmistler,

cp -R/-r /tmp/foo/bar/baz /tmp2/ will copy the baz directory directly under /tmp2/. I want to copy it inside /tmp2/foo/bar/, without creating /tmp2/foo/bar myself -the command should do it for me-

I haven't found any option for cp that do that (even --parents, which only seems to work for a single file, not for a whole directory).

But it seems rsync has a nice --relative option that will create the necessary parent directory to match those of the source directory.

I might as well mark this thread as solved, but other solutions are welcome :)

The "-d" switch for cpio(1) will create needed directories though I'm not sure it does exactly what you need. Check the info(1) page for cpio and look at the section on copying directory structures (in the Concept Index).

Unless the target is supposed to be really deeply nested, a single "mkdir -p /subdir1/subdir2/subdir3/..." should be all that's needed to create the other directories, no? What about something like:
Code:

TGT=/top-level/subdir1/subdir2/subdir3/destdir
mkdir -p $TGT
cd $TOP-OF-SRC-TREE
copy -R * $TGT/

I get lazy when dealing with deeply nested directories and when I'm in the destination where I want to place things, I'll do something like:
Code:

$ TGT=`pwd`
$ cd source-directory
$ cp -R * $TGT

or
Code:

$ TGT=`pwd`
$ cd source-directory
$ find . -depth -print0 | cpio --null -pVd $TGT/

More things to play with.

Have a good one...


All times are GMT -5. The time now is 04:58 PM.