Operating System - HP-UX
1824842 Members
3906 Online
109674 Solutions
New Discussion юеВ

Copy files & Directories while preserving the same ownership & permission

 
Kunju
Occasional Contributor

Copy files & Directories while preserving the same ownership & permission

Hi,

I need someone's help on the following

I need to copy one filesystem from one server to another. I need to copy the entire directory & files under the filesystem /test.
But After copying the file permissions should be same but I need to change the ownership.
for example I need to change all the files and directories owned by 'testadm' to 'testbdm'
Also I need to confirm that all the file permission should be same.

Can anyone help me for this with any script or some method?

Appreciate your help in advance.

Shaheer
17 REPLIES 17
Dennis Handly
Acclaimed Contributor

Re: Copy files & Directories while preserving the same ownership & permission

You probably want to do this it two steps.
1) Copy files saving permissions and ownership.
Use cp -p, or tar(1), pax(1) or cpio(1).

2) Use find and chown to change the ownership.
# find /file-system -user testadm -exec chown testbdm {} +

James R. Ferguson
Acclaimed Contributor

Re: Copy files & Directories while preserving the same ownership & permission

Hi:

I agree with Dennis -- a 2-step approach as he showed.

I suggest that you consider using 'tar' or 'pax' to collect your files and directories(?) into an archive while preserving their permissions.

Once you have created the archive, copy *that* to your destination and un-archive it preserving the permissions of the contents.

Using 'cp' (permissions aside) if you must copy recursively ('-R') to another directory can become "ugly" when the destination directory exists and isn't empty. See the 'cp' manpages.

Another advantage to using the 'tar' or 'pax' archive is that you intrinsically have a list of files that are transferred and can limit ownership changes on the destination server to this list if necessary.

Regards!

...JRF...
Siju Vadakkan
Trusted Contributor

Re: Copy files & Directories while preserving the same ownership & permission



cp -pR /test /destination
OldSchool
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

I generally use the following (from "man tar"):

Use tar in a pipeline to copy the entire file system hierarchy under fromdir to todir:

cd fromdir ; tar cf - . | ( cd todir ; tar xf -i )
Dennis Handly
Acclaimed Contributor

Re: Copy files & Directories while preserving the same ownership & permission

>OldSchool: cd fromdir ; tar cf - . | ( cd todir ; tar xf -i )

No need to cd to fromdir. You can just use:
tar -cf - -C fromdir . | ( cd todir; tar -xf - )

Also you have "-i" instead of just "-".
Also, the original question mentioned different servers, so a remsh or something would be needed in the pipeline.
Victor Fridyev
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

There is an excellent utility called rsync, which by default stores permission, ownership and time stamps of copied files.
Entities are not to be multiplied beyond necessity - RTFM
Steven Schweda
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

> cp -pR /test /destination

For a possible reason not to use "cp -R", see

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1203742
OldSchool
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

"No need to cd to fromdir. You can just use:
tar -cf - -C fromdir . | ( cd todir; tar -xf - )

Also you have "-i" instead of just "-"."

yeah...i knew that...just quoted the man page verbatim (forgot to remove the typo).

also, when I've used it w/ remote server(s), I usually NFS mount the destination and go with it :-)
Dennis Handly
Acclaimed Contributor

Re: Copy files & Directories while preserving the same ownership & permission

>OldSchool: just quoted the man page verbatim

It looks fine in the online tar(1) man pages for 11.11, 11.23 and 11.31.
Steven Schweda
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

> No need to cd to fromdir. [...]

Well, not if "-C" works everywhere you do,
but I prefer to use a smaller (more reliably
available) feature set, especially when it
makes things more symmetrical, hence easier
to remember:

( cd src ; tar cf - xxx ) | \
( cd dst ; tar xf - )

Learn it once, then relax.

Using parentheses on the "src" side leaves
the user's environment unchanged, which is
nicer than a bare "cd", I claim. For a good
time, you can also slide an "rsh" onto either
side to add some remoteness without dragging
NFS into the argument.
Yogeeraj_1
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

hi Shaheer,

If you have same users and groups (uid and gid) on both server, you can also try to do a remote copy using: scp


e.g.
scp -p -r ./mydir otherserver:/home/yogeeraj/


hope this helps too!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Steven Schweda
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

> [...] do a remote copy using: scp

Does "scp -r" handle links (hard and soft)
any better than "cp -R"? (See reference
above to previous "cp"-related complaint.)
I'd assume not.

Replace "rsh" (above) with "ssh", if you'd
like an ssh=based method which wouldn't have
the same problems as "cp".
piyush mathiya
Trusted Contributor

Re: Copy files & Directories while preserving the same ownership & permission

You can user rcp (the berkly concept). you have to follow the below steps...
#### In destination server
1). vi /.rhosts
root
save it.
2). vi /etc/hosts
make an entry of source server.

### In source server
3). rcp -p -r
rcp -p -r /mnt server2:/mnt

where /mnt is the name of file system and server2 is the destination server and /mnt is exist on server2.
### In destination server after copied all files.
4). find . -user testadm -print -exec chown testbdm {} \;

it will works...

cheaars!!!!
Steven Schweda
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

> You can user rcp [...]

Ok. I give up. You win. Do whatever you
want. It probably doesn't matter if links
are handled well or not.
Yogeeraj_1
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

hi Steven,

we really appreciate your post and concern. In many cases, we often forget the possibility that links to files might exist.

if the initial poster could give us some feedback of its relevance, then maybe we could come to consensus here :)

We really appreciate your contributions and insights. I have learned a lot from your posts..

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
OldSchool
Honored Contributor

Re: Copy files & Directories while preserving the same ownership & permission

dennis said: "It looks fine in the online tar(1) man pages for 11.11, 11.23 and 11.31."

ah...well I was looking at an 11.0 box
Kunju
Occasional Contributor

Re: Copy files & Directories while preserving the same ownership & permission

TAR is best option. Only concern is the size of the data. It worked for me. Thanks all for the help.