How do you use the cp command without changing the target file's permissions? For example:

cp /tmp/file   /home/file

I don't want to change chown and chgrp on /home/file.

  • 3
    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
  • For the record, the "preservation" options are in reference to the source. cp -p makes the target attributes match (thereby preserving) the source attributes. – mpersico Apr 20 '17 at 16:54
  • 1
    I just stumbled upon this page. cp by default ought to preserve the target files permissions and user:group because it opens the target in update mode and retaining its inode. So it's not clear to my why the answers don't indicate this. – dozer Apr 21 '17 at 11:58
up vote 27 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

cat will work, too:

cat /tmp/file > /home/file

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

  • 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

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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