Cloning permissions of a folder to another folder

10,814

Solution 1

Tested on Mac OS X v10.5.7, in bash:

chown $(stat -f%u:%g "$srcdir") "$dstdir" # Copy owner and group
chmod $(stat -f%Mp%Lp "$srcdir") "$dstdir" # Copy the mode bits
(ls -lde "$srcdir"  | tail +2 | sed 's/^ [0-9]*: //'; echo) | chmod -E  "$dstdir" # Copy the ACL

Notes: These operations (esp. changing ownership) are likely to require root access; sprinkle with sudo for best results. Also, that odd echo command on the last line is there to prevent an error if srcdir doesn't have any ACL entries attached (chmod -E can cope with blank lines, but not a completely empty input).

Solution 2

I found a simple solution.

  1. Create a zero byte test.txt file at srcdir, eg /User/test1/srcdir/test.txt
  2. Make sure dstdir does not exit at target folder, eg /Users/test2/
  3. Open Terminal and type following command
sudo ditto /Users/test1/srcdir/test.txt /Users/test2/dstdir/

Note: the last slash at dstdir/ is necessary

ditto will create directory dstdir/ with same permissions as srcdir/

lok

Solution 3

I presume you Googled and found at least:

And this web page also seems to cover some important information (such as fsaclctl).

Share:
10,814
indragie
Author by

indragie

iOS and Mac Developer. I'm working on Flamingo for Mac and previously built Sonora.

Updated on June 11, 2022

Comments

  • indragie
    indragie almost 2 years

    Are there any ways in OS X to clone the permissions of one folder to another. Just to be clear, I don't want to copy the entire folder, just the permissions and then set them on another folder. I think this type of thing could be achieved on Linux/UNIX using the setfacl/getfacl commands, but I'm unsure on how to do this with OS X.

    Thanks

  • Telemachus
    Telemachus over 14 years
    fsaclctl is for turning acls on or off on a filesystem, rather than controlling the acl status of a particular folder. In 10.4, I believe, acl support was available but off by default, so this command came in handy. In 10.5, of course acls are on by default, so it still comes in handy for turning the damn things off.
  • indragie
    indragie over 14 years
    I use chmod all the time, the problem is that I need an automated process to clone the permissions from a source directory to a target directory. Chmod is easy to use manually, but for an automated process it may be a bit hard.
  • Simon Michael
    Simon Michael over 4 years
    Thanks for this, but alas: chmod: Unknown tag type 'inherited'. (The ls -e output is 0: user:_spotlight inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,d‌​irectory_inherit, on macos mojave)
  • Anton  Korneychuk
    Anton Korneychuk over 2 years
    @SimonMichael I faced the same issue and found a workaround. I've added the solution as a separate answer.