Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 "like" that of another file?

share|improve this question
up vote 66 down vote accepted

On GNU/Linux chown and chmod have a --reference option

chown --reference=otherfile thisfile
chmod --reference=otherfile thisfile
share|improve this answer
Could 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. – Grzegorz Wierzowiecki Jul 31 '12 at 20:40
@GrzegorzWierzowiecki: probably that question should be closed, but is a little bit different than this and already has answers, so I better do nothing. – enzotib Jul 31 '12 at 20:54
As you wish and suggest. Thanks for help, I have never put attention to --reference parameter of chmod and chown before :). – Grzegorz Wierzowiecki Jul 31 '12 at 22:02

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
share|improve this answer
You should have ACL installed and filesystem mounted with ACL enabled. – enzotib Sep 14 '11 at 4:03
@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. – Gilles Sep 14 '11 at 6:57

Did a bash command based on the response of Matteo :)


chmod $( stat -f '%p' "$1" ) "${@:2}"


cp-permissions <from> <to>...

share|improve this answer
Egad!  Where did you learn to say ${*:2}?  Don't ever do that again!  That will fail if any of the filenames contain space (or tabs).  Use "${@:2}".  Also, use "$1" instead of just $1. – G-Man May 26 '15 at 4:10
Fixed the comment and the gist. Thanks! – mjlescano May 29 '15 at 14:16

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 ...)



# 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}


This is even easier using stat:

chmod $( stat -f '%p' ${reference} ) ${files}
share|improve this answer
Instead of parsing ls -l output, you could you could parse statoutput. – jfgagne Sep 21 '11 at 19:51
@jfgagne: thanks makes sense I do not know why I didn't think about it in the first place. I updated the answer – Matteo Sep 22 '11 at 5:28
You're using *BSD stat syntax here. Your chmod $(stat ...) command won't work because %p alone outputs too much information for *BSD's chmod, use %Lp to output just the u/g/o bits. Something slightly more elaborate would be required for sticky/setuid/setgid bits. – mr.spuratic Jun 7 '13 at 10:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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