bash - 在linux中检查文件权限

标签 bash ubuntu chmod

我需要更改目录中文件的权限,例如:

  • 如果该组没有权限,其他人也应该没有权限
  • 如果组有权限,什么都不应该改变

  • 但是在一个命令中。

    我很确定我必须使用 chmod对于改变本身。
    我最大的问题实际上是如何知道该组是否有权限?此后,在单个命令中使用此信息

    最佳答案

    你可以试试这个:

    #!/bin/bash
    [ "$(stat -c "%a" "$1" | cut -b 2)" -eq 0 ] && chmod o-rwx "$1"
    

    解释:
    stat -c "%a" "$1"   # get file permission as octal number
    cut -b 2            # extract second number (-> group)
    -eq 0               # test if group permissin is 0
    &&                  # then...
    chmod o-rwx "$1"    # remove other permissions
    

    关于bash - 在linux中检查文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423265/

    相关文章:

    linux - phpstorm 在./idea 目录下写入问题

    bash - 您能解释一下 bash shell 中的数学语法吗?

    linux - 获取运行目录名的方法

    google-chrome - Capybara、Selenium、 headless Chrome、Ubuntu Net::ReadTimeout

    java - Hadoop WordCount 示例中的 "Cannot find symbol"错误

    amazon-web-services - 通过 SSH 连接到 AWS 上的 ec2 实例

    git:检查上次获取运行的时间

    git - Linux - 运行多个并行命令,打印顺序输出

    python - pip install -r requirements.txt 在 ubuntu 上

    PHP:fileperms() 值并转换这些