Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to use cp command without to overwrite target file permissions

for example

cp /tmp/file   /home/file

I dont want to change chown and chgrp on /home/file

share|improve this question
1  
It's not clear whether you are asking about preserving the source permissions or the target permissions. You've also cross-site spammed to SuperUser. – Tom Shaw May 26 '11 at 9:00
up vote 20 down vote accepted

If you've only opened the manual for cp...

The next will not overwrite the file permissions and ownership + groupship:

cp --no-preserve=mode,ownership /tmp/file /home/file

Note that root privileges are needed if you want to preserve the ownership and groupship.

An excerpt from the manual:

  --preserve[=ATTR_LIST]
      preserve   the   specified   attributes   (default:  mode,owner-
      ship,timestamps), if possible  additional  attributes:  context,
      links, xattr, all
share|improve this answer

cat will work, too:

cat /tmp/file > /home/file
share|improve this answer

Or you can use even better install program from GNU coreutils that has been made for this particular purpose. Please note it is not able to recurse (no -R or -r option).

http://www.gnu.org/software/coreutils/manual/html_node/install-invocation.html

share|improve this answer
1  
I didn't see a switch for this to preserve the target's ower or group. Just force it to a new value. – Pete Oct 7 '15 at 22:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.