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
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:
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).
If you've only opened the manual for
cp
...The next will not overwrite the file permissions and ownership + groupship:
Note that root privileges are needed if you want to preserve the ownership and groupship.
An excerpt from the manual:
cat
will work, too:By default, GNU
cp
(version 8.32) will NOT overwrite destination permissions, so the question is moot:In the case where the destination file is not writable (even though the directory is),
cp
will say:Other options besides
cp
:cat
will preserve the inode and permissions of the destination file:However, redirections won't work with
sudo
.If you want to
sudo
and keep the destination permissions, use this:This is equivalent to the more verbose:
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
Don't use permission-related switches at all, especially
--no-preserve
, because it behaves strangely:good:
bad:
The cp doesn't override permissions by default. If you want to make sure permission won't be overridden, use