Cloning permissions of a folder to another folder
Asked Answered
Y

5

6

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

Yunyunfei answered 19/8, 2009 at 22:6 Comment(4)
Are you trying to learn how to do this generally or as part of a programming task? If it's a general question, this belongs on SuperUser.Heavyhearted
Shell scripts are programs - it is fine on StackOverflow.Snowball
@Jonathan: the question is so vague that it's hard to say if he wants to do this with a shell script, or if he just wants a shell command.Heavyhearted
I'm going to be using this in a shell script. Anything is fine, a shell command or a script that does this task (cloning permissions)Yunyunfei
M
11

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

Mathre answered 25/8, 2009 at 2:36 Comment(2)
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,directory_inherit, on macos mojave)Zombie
@SimonMichael I faced the same issue and found a workaround. I've added the solution as a separate answer.Prepense
S
1

I presume you Googled and found at least:

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

Snowball answered 19/8, 2009 at 22:21 Comment(4)
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.Heavyhearted
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.Yunyunfei
@PCWiz: you can easily put a series of chmod commands (which you've tested to perfection) into a Bash script and run that as needed.Heavyhearted
You can use ls -lde to get the acls to begin with, by the way.Heavyhearted
A
1

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

Antheridium answered 11/11, 2021 at 17:1 Comment(0)
Y
0

What I ended up doing was creating an Objective C method (I was planning on using this in a Cocoa app anyways) that finds out the permissions of a file using a perl script, then uses chmod/chown to apply those permissions.

Yunyunfei answered 23/8, 2009 at 18:44 Comment(0)
P
0

This answer is an addition to Gordon Davisson's answer for inherited attributes.

Tested on: Mac OS X 11.12.1, bash 3.2.57

It looks like chmod -E doesn't support the inherited option. I found a workaround.

  1. It is important to understand that allow \ deny entries can be local (default) and inherited.
  2. flag -a adds local entries, -ai adds inherited entries.
chown $(stat -f%u:%g "$srcdir") "$dstdir" # Copy owner and group
chmod $(stat -f%Mp%Lp "$srcdir") "$dstdir" # Copy the mode bits

chmod -N  "$dstdir" # Removes all ACL entries. It can be important because '+a' adds entries, not replaces
chmod +ai "$(ls -lde "$srcdir"  | tail +2 | sed -e 's/^ [0-9]*: //' -e 's/ inherited / /'; echo)" "$dstdir" # Adding inherited entries
Prepense answered 14/9, 2021 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.