1833995 Members
2827 Online
110063 Solutions
New Discussion

Re: Copy directory tree

 
SOLVED
Go to solution
Chris Watson
Super Advisor

Copy directory tree

I have the following example directory structure;

/test/dir1/subd1.1/file

I would like to end up with /test/dir1 and everything below copying (retaining permissions and ownership) and being named as a tree called /test/dir2. Thus ending up with;

/test/dir1/subd1.1/file
&
/test/dir2/subd1.1/file

So I thought something like;

cp -pr /test/dir1 /test/dir2, but I end up with;

/test/dir2/dir1/subd1/file, not what I wanted.

Any ideas?

Regards

Chris

Moving along nicely
15 REPLIES 15
James R. Ferguson
Acclaimed Contributor

Re: Copy directory tree

Hi Chris:

'cp' is the hard-way to do what you want! Try this:

# cd /test/dir1
# find . | cpio -pudlmv /test/dir2

See the man pages for the 'cpio' options!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Copy directory tree

Hio Chris:

Try something like this:

mkdir /test/dir2
(set ownership and permission of this directory as desired using chown, chmod)

cd /test/dir1
find subd1.1 -print | cpio -pudvm /test/dir2/

Clay


If it ain't broke, I can fix that.
Rita C Workman
Honored Contributor

Re: Copy directory tree

Well I might have tried:

cp -pr /test/dir1/subd1.1/* /test/dir2

Just a thought,
Rita
Bill McNAMARA_1
Honored Contributor

Re: Copy directory tree

cp -R /dir/tree/dir/tree /dir2/tree

The -R should do it, otherwise create a tar
from the tree you wish to keep

cd /mydir/tree
tar -cvf tree.tar tree
cd /newdir
tar -xvf tree.tar

Later,
Bill
It works for me (tm)
Eileen Millen
Trusted Contributor

Re: Copy directory tree

mkdir /test/dir2
cd /test/dir1
find . -depth -print | cpio -pdumv /test/dir2

MANOJ SRIVASTAVA
Honored Contributor
Solution

Re: Copy directory tree

Hi Chris

cd /test/dir1/subd1.1
cp -pr subd1.1/* /test/dir2

Manoj Srivastava
Mark van Hassel
Respected Contributor

Re: Copy directory tree

HI,

mkdir /test/dir2
cp -pR /test/dir1/* /test/dir2

and a "chown"on /test/dir2 to get the same permissions as on dir1
The surest sign that life exists elsewhere in the universe is that none of it has tried to contact us
Volker Borowski
Honored Contributor

Re: Copy directory tree

Chris,

many cp-solutions, but only two cpio ones.
But cpio is the better one, because of the correct handling of sym-links.

See this sample (c being a copy of a instead of b):

# touch a
# ln -s a b
# ll ?
-rw-rw-rw- 1 root sys 0 Aug 1 18:12 a
lrwxrwxrwx 1 root sys 1 Aug 1 18:12 b -> a
# cp -p b c
# ll ?
-rw-rw-rw- 1 root sys 0 Aug 1 18:12 a
lrwxrwxrwx 1 root sys 1 Aug 1 18:12 b -> a
-rw-rw-rw- 1 root sys 0 Aug 1 18:12 c
#

Hope this helps
Volker
Kevin Wright
Honored Contributor

Re: Copy directory tree

your test directory already existed, so it created your structure below it..you could use you same copy command if you just delete the current dir2 and allow the cp -rp command to create if for you.
but as everyone stated, the best way is with find and cpio.
Deshpande Prashant
Honored Contributor

Re: Copy directory tree

Try..
#cd /test
#mkdir test2
#cd /test/dir1
#cp -Rp . /test/dir2


Thanks.
Prashant.
Take it as it comes.
Chris Watson
Super Advisor

Re: Copy directory tree

Thanks to everyone for your replies, of which there are to many to thank individually.

I have ended up with;

cd /test
cp -pr dir1/. dir2

which does the trick.

Thanks yall.

Chris

Moving along nicely
Chris Watson
Super Advisor

Re: Copy directory tree

Manoj,

I based my usage of your suggestion but modified the source to

cp -pr dir1/. dir2

As with an asterix it complained of too many files in my production usage.

Regards

Chris

Moving along nicely
dennis bergman
Occasional Advisor

Re: Copy directory tree

Im new to HP. Ive done this several times on Solaris and have used

cd to target_dir

find /path/source_dir -depth -print |cpio -pvdum .

I know this has already been suggested.....
good luck
Joseph C. Denman
Honored Contributor

Re: Copy directory tree

This way for tar:

mkdir /test/dir2; cd /test/dir1;tar cpf - . | ( cd /test/dir2; tar xpf - )

A million ways to skin a cat!!!

...jcd...

If I had only read the instructions first??
VARITRONIX
Occasional Advisor

Re: Copy directory tree

Try to use "rdist",
it does everything you want to do.
Kwan Chris