Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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
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).
( 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
Last edited by MadeInGermany; 08-13-2020 at 02:07 PM.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800
Rep:
Quote:
Originally Posted by ychaouche
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.