Clone ownership and permissions from another file?
Solution 1
On GNU/Linux chown
and chmod
have a --reference
option
chown --reference=otherfile thisfile
chmod --reference=otherfile thisfile
Solution 2
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
Solution 3
Did a bash command based on the response of Matteo :)
Code:
chmod $( stat -f '%p' "$1" ) "${@:2}"
Usage:
cp-permissions <from> <to>...
Solution 4
If you are not using a system with GNU's chmod/chown (which support the --reference
option) you could try to parse the output of ls -l
Here a small script for chmod
(if you have a see which supports extended regexes they could be written in a much more readable way ...)
#!/bin/sh
reference=$1
shift
files=$*
# strip the permissions (whith extended regexes could be more readable)
OWNER=$(ls -l ${reference} | sed -e "s/.\(...\).*/\1/" | sed -e "s/[-]//g" )
GROUP=$(ls -l ${reference} | sed -e "s/....\(...\).*/\1/" | sed -e "s/[-]//g" )
OTHER=$(ls -l ${reference} | sed -e "s/.......\(...\).*/\1/" | sed -e "s/[-]//g" )
chmod u=${OWNER},g=${GROUP},o=${OTHER} ${files}
UPDATE:
This is even easier using stat
:
chmod $( stat -f '%p' ${reference} ) ${files}
Solution 5
This works for me:
cp -p --attributes-only <from> <to>
Related videos on Youtube
user394
Updated on September 18, 2022Comments
-
user394 almost 2 years
Is there a command or flag to clone the user/group ownership and permissions on a file from another file? To make the perms and ownership exactly those of another file?
-
Sohail Si over 2 yearsRelated questions: askubuntu.com/questions/56792/… and unix.stackexchange.com/questions/44253/…
-
-
enzotib almost 13 yearsYou should have ACL installed and filesystem mounted with ACL enabled.
-
Gilles 'SO- stop being evil' almost 13 years@enzotib At least on Linux, ACL tools will work to copy permissions (but not ownership) even if the source and target filesystem don't support ACLs.
-
jfg956 almost 13 yearsInstead of parsing
ls -l
output, you could you could parsestat
output. -
Matteo almost 13 years@jfgagne: thanks makes sense I do not know why I didn't think about it in the first place. I updated the answer
-
Grzegorz Wierzowiecki almost 12 yearsCould you reference to this answer (and likely cite it) as answer to my question : unix.stackexchange.com/questions/44253/… ? , I think I will be great addition and I'd love to find up-votes there for it.
-
enzotib almost 12 years@GrzegorzWierzowiecki: probably that question should be closed, but is a little bit different than this and already has answers, so I better do nothing.
-
Grzegorz Wierzowiecki almost 12 yearsAs you wish and suggest. Thanks for help, I have never put attention to
--reference
parameter ofchmod
andchown
before :). -
mr.spuratic about 11 yearsYou're using *BSD
stat
syntax here. Yourchmod $(stat ...)
command won't work because%p
alone outputs too much information for *BSD'schmod
, use%Lp
to output just the u/g/o bits. Something slightly more elaborate would be required for sticky/setuid/setgid bits. -
Ciro Santilli Путлер Капут 六四事 almost 6 years
chmod "$(stat -c '%a' "$fromfile")" tofile
in GNU Coreutils, but you might as well use--reference
in that case since thestat
CLI utility is not POSIX, it even says pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.htmlthatls -l
won't cut it: "The output of ls (with the -l and related options) contains information that logically could be used by utilities such as chmod and touch to restore files to a known state. However, this information is presented in a format that cannot be used directly by those utilities or be easily translated into a format that can be used." -
Stéphane Chazelas over 2 yearsThat copies all attributes though (change
-p
to--preserve=ownership,mode
to copy only permissions and ownership (and ACLs if any)). That will also not work for files of type directory. Also note that if<to>
is a symlink, that will copy the attributes to the target of the symlink (likely what one would want anyway as permissions of symlinks themselves are rarely relevant (though ownership can be)). -
Vlastimil Burián over 2 years