LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 08-11-2020, 09:57 AM   #1
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 367
Blog Entries: 1

Rep: Reputation: 48
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
 
Old 08-11-2020, 10:51 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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?
 
Old 08-11-2020, 11:10 AM   #3
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 367

Original Poster
Blog Entries: 1

Rep: Reputation: 48
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
 
Old 08-11-2020, 11:34 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Do you want to merge two existing & identical (or, let's say, compatible) directory structures with all files that are in it?
 
Old 08-11-2020, 11:37 AM   #5
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 367

Original Poster
Blog Entries: 1

Rep: Reputation: 48
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).
 
Old 08-13-2020, 02:05 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,790

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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

Last edited by MadeInGermany; 08-13-2020 at 02:07 PM.
 
Old 08-13-2020, 03:25 PM   #7
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,800

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by ychaouche View Post
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...

Last edited by rnturn; 08-13-2020 at 03:27 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to deal with .config/user-dirs.dirs nix84 Fedora 3 10-12-2015 02:37 AM
Create 5 processes from a common parent and ensure that the parent terminates after c pro_learner Linux - Newbie 1 03-06-2013 09:25 AM
[SOLVED] Apple gcc: Insert include dirs between -I dirs and -isysroot dirs f.barker Programming 1 11-11-2011 10:19 AM
File descriptors shared between child and parent causing parent to hang. bharadiaam Linux - Newbie 1 03-02-2009 01:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration