cp without overwriting destination permissions

30,625

Solution 1

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

Solution 2

cat will work, too:

cat /tmp/file > /home/file

Solution 3

By default, GNU cp (version 8.32) will NOT overwrite destination permissions, so the question is moot:

% ls -li
total 8,192
19392015 -rwxrwxrwx 1 ravi ravi 4 Jan  3 16:54 bar*
19392014 -rw-r--r-- 1 ravi ravi 4 Jan  3 16:46 foo
% cp foo bar
% ls -li
total 8,192
19392015 -rwxrwxrwx 1 ravi ravi 4 Jan  3 16:57 bar*
19392014 -rw-r--r-- 1 ravi ravi 4 Jan  3 16:46 foo
%

In the case where the destination file is not writable (even though the directory is), cp will say:

cp: cannot create regular file 'dest-file': Permission denied

Other options besides cp:

cat will preserve the inode and permissions of the destination file:

cat file-with-new-data > file-to-overwrite

However, redirections won't work with sudo.

If you want to sudo and keep the destination permissions, use this:

< file-with-new-data sudo tee file-to-overwrite > /dev/null

This is equivalent to the more verbose:

cat file-with-new-data | sudo tee file-to-overwrite > /dev/null
Share:
30,625

Related videos on Youtube

David
Author by

David

Updated on September 18, 2022

Comments

  • David
    David almost 2 years

    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.

    • Naman Bansal
      Naman Bansal about 13 years
      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.
    • mpersico
      mpersico about 7 years
      For the record, the "preservation" options are in reference to the source. cp -p makes the target attributes match (thereby preserving) the source attributes.
    • dozer
      dozer about 7 years
      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.
    • Tom Hale
      Tom Hale over 3 years
      This question is moot. GNU cp default behaviour is as desired by this question, as demonstrated here.
  • Pete
    Pete over 8 years
    I didn't see a switch for this to preserve the target's ower or group. Just force it to a new value.
  • basin
    basin over 5 years
    Not exactly what the author wanted. --no-preserve makes sense if used after --preserve (or its aliases), otherwise it doesn't affect the behavior of cp. The author wanted to keep the file mode of an existing target file and only overwrite its contents
  • basin
    basin over 5 years
    Ex: replace ssh host keys cp --no-preserve=mode,ownership ssh* /etc/ssh/. This makes the secret keys world-readable.
  • beppe9000
    beppe9000 over 4 years
    does it work for binary files too ?
  • Tom Hale
    Tom Hale over 3 years
    @beppe9000 yes, cat outputs whatever it inputs.
  • Tom Hale
    Tom Hale over 3 years
    For an answer that works with sudo, see here
  • Tom Hale
    Tom Hale over 3 years
    That actually forces overwriting of the target permissions -- it preserves the permissions of the source file.
  • Tom Hale
    Tom Hale over 3 years
    --no-preserve=all is default. And by default GNU cp doesn't update target permissions anyway.
  • pacoverflow
    pacoverflow about 3 years
    I verified cp version 8.26 also keeps the permissions/owner/group of the file being overwritten.