【问题标题】:Cloning permissions of a folder to another folder将一个文件夹的权限克隆到另一个文件夹
【发布时间】:2010-11-21 03:12:22
【问题描述】:

OS X 中是否有任何方法可以将一个文件夹的权限克隆到另一个文件夹。为了清楚起见,我不想复制整个文件夹,只复制权限,然后将它们设置在另一个文件夹上。我认为这种类型的事情可以在 Linux/UNIX 上使用 setfacl/getfacl 命令来实现,但我不确定如何在 OS X 上做到这一点。

谢谢

【问题讨论】:

  • 您是想学习如何进行一般性的学习还是作为编程任务的一部分?如果是一般性问题,则属于 SuperUser。
  • Shell 脚本是程序 - 在 StackOverflow 上没问题。
  • @Jonathan:这个问题太模糊了,很难说他是想用 shell 脚本来做这个,还是只想要一个 shell 命令。
  • 我将在 shell 脚本中使用它。任何东西都可以,执行此任务的 shell 命令或脚本(克隆权限)

标签: shell permissions macos acl


【解决方案1】:

我想你至少用 Google 搜索过:

而且这个web page似乎也涵盖了一些重要的信息(比如fsaclctl)。

【讨论】:

  • fsaclctl 用于在文件系统上打开或关闭 acls,而不是控制特定文件夹的 acl 状态。我相信在 10.4 中,acl 支持可用但默认关闭,所以这个命令派上用场了。在 10.5 中,当然 acls 默认是打开的,所以它仍然可以用来关闭该死的东西。
  • 我一直使用 chmod,问题是我需要一个自动化的过程来将权限从源目录克隆到目标目录。 chmod 很容易手动使用,但对于自动化过程来说可能有点困难。
  • @PCWiz:您可以轻松地将一系列 chmod 命令(您已经完美地测试过)放入 Bash 脚本并根据需要运行。
  • 顺便说一句,您可以使用ls -lde 来获取acls。
【解决方案2】:

我最终做的是创建一个 Objective C 方法(我本来打算在 Cocoa 应用程序中使用它),它使用 perl 脚本找出文件的权限,然后使用 chmod/chown 来应用这些权限。

【讨论】:

    【解决方案3】:

    在 Mac OS X v10.5.7 上测试,在 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
    

    注意:这些操作(尤其是更改所有权)可能需要 root 访问权限;撒上sudo 以获得最佳效果。此外,如果 srcdir 没有附加任何 ACL 条目,最后一行的奇怪 echo 命令可以防止错误(chmod -E 可以处理空行,但不能处理完全空的输入)。

    【讨论】:

    • 谢谢你,但唉:chmod: Unknown tag type 'inherited'。 (在 macos mojave 上,ls -e 输出为 0: user:_spotlight inherited allow list,search,readattr,readextattr,readsecurity,file_inherit,directory_inherit
    • @SimonMichael 我遇到了同样的问题并找到了解决方法。我已将解决方案添加为单独的答案。
    【解决方案4】:

    此答案是对 Gordon Davisson 答案的补充 inherited 属性。

    测试于:Mac OS X 11.12.1,bash 3.2.57

    看起来chmod -E 不支持inherited 选项。我找到了解决方法。

    1. 重要的是要了解allow \ deny 条目可以是local(默认)和inherited
    2. 标记-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
    

    【讨论】:

      【解决方案5】:

      我找到了一个简单的解决方案。

      1. 在srcdir创建一个零字节test.txt文件,例如/User/test1/srcdir/test.txt
      2. 确保 dstdir 不会在目标文件夹退出,例如 /Users/test2/
      3. 打开终端并输入以下命令
      sudo ditto /Users/test1/srcdir/test.txt /Users/test2/dstdir/
      

      注意:dstdir/ 的最后一个斜杠是必需的

      同上将创建目录dstdir/,其权限与srcdir/相同

      【讨论】:

        猜你喜欢
        • 2022-10-25
        • 1970-01-01
        • 2011-05-18
        • 2018-11-12
        • 1970-01-01
        • 2011-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多