OS X 中是否有任何方法可以将一个文件夹的权限克隆到另一个文件夹。为了清楚起见,我不想复制整个文件夹,只复制权限,然后将它们设置在另一个文件夹上。我认为这种类型的事情可以在 Linux/UNIX 上使用 setfacl/getfacl 命令实现,但我不确定如何在 OS X 上做到这一点。
谢谢
OS X 中是否有任何方法可以将一个文件夹的权限克隆到另一个文件夹。为了清楚起见,我不想复制整个文件夹,只复制权限,然后将它们设置在另一个文件夹上。我认为这种类型的事情可以在 Linux/UNIX 上使用 setfacl/getfacl 命令实现,但我不确定如何在 OS X 上做到这一点。
谢谢
在 bash 中在 Mac OS X v10.5.7 上测试:
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
注意:这些操作(尤其是更改所有权)可能需要 root 访问权限;洒上以sudo
获得最佳效果。echo
此外,如果 srcdir 没有附加任何 ACL 条目(chmod -E
可以处理空行,但不能处理完全空的输入) ,最后一行的奇怪命令可以防止错误。
我找到了一个简单的解决方案。
test.txt
在 srcdir创建一个零字节文件,例如/User/test1/srcdir/test.txt
/Users/test2/
sudo ditto /Users/test1/srcdir/test.txt /Users/test2/dstdir/
注意:最后一个斜杠 atdstdir/
是必需的
同上将创建dstdir/
具有相同权限的目录srcdir/
乐
我最终做的是创建一个 Objective C 方法(我本来打算在 Cocoa 应用程序中使用它),它使用 perl 脚本找出文件的权限,然后使用 chmod/chown 来应用这些权限。
这个答案是对 Gordon Davisson 的属性答案的 补充inherited
。
测试:Mac OS X 11.12.1,bash 3.2.57
看起来chmod -E
不支持该inherited
选项。我找到了解决方法。
allow
\deny
条目可以是local
(默认)和inherited
.-a
添加local
条目,-ai
添加inherited
条目。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