Clone ownership and permissions from another file?
On GNU/Linux chown
and chmod
have a --reference
option
chown --reference=otherfile thisfile
chmod --reference=otherfile thisfile
On any unix with GNU utilities, such as (non-embedded) Linux or Cygwin, you can use chmod --reference
and chown --reference
.
If your system has ACLs, try the ACL commands getfacl
and setfacl
. These commands differ a little from system to system, but on many you can use getfacl other_file | setfacl -bnM - file_to_change
to copy the permissions. This doesn't copy the ownership; you can do that with careful parsing of ls -l other_file
, assuming that you don't have user or group names containing whitespace.
LC_ALL=C ls -l other_file | {
read -r permissions links user group stuff;
chown -- "$user:$group" file_to_change
}
getfacl other_file | setfacl -bnM - file_to_change
Did a bash command based on the response of Matteo :)
Code:
chmod $( stat -f '%p' "$1" ) "${@:2}"
Usage:
cp-permissions <from> <to>...