AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1630085
Accepted
Decent Dabbler
Decent Dabbler
Asked: 2021-03-02 13:45:57 +0800 CST2021-03-02 13:45:57 +0800 CST 2021-03-02 13:45:57 +0800 CST

区分目录和文件的通用chmod函数(即:使用find)

  • 772

是否有一个通用的 bash 函数可以chmod在各个方面进行模仿,除了它还让我区分文件和目录?

我知道这个答案已经有很多可用的示例,如下所示:

find /path/to/base/dir -type d -exec chmod 755 {} +
chmod 755 $(find /path/to/base/dir -type d)
find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 

...但是这些都带有硬编码的参数,并且难以记住和输入。1)

我想对其进行概括并使其动态化,以便我能够执行以下操作,例如:

# for directories
chmod -d <any valid argument list for chmod> # or
chmodd <any valid argument list for chmod>


# for files
chmod -f <any valid argument list for chmod> # or
chmodf <any valid argument list for chmod>

我自己尝试创建一个可行的解决方案,但由于我的 bash 技能低于标准,而且我不确定如何解析正确的参数并将它们插入正确的位置,所以它非常粗糙且有限:

function chmodf {
  find . -mindepth 1 -type f -print0 | xargs -0 chmod "$@"
}

function chmodd {
  find . -mindepth 1 -type d -print0 | xargs -0 chmod "$@"
}

当然,我更喜欢如下(伪代码):

function chmodd {
  paths = extract paths from arguments list
  recursive = extract recursive option from arguments list
  otherargs = remaining arguments
  if( recursive ) {
    find <paths> -mindepth 1 -type d -print0 | xargs -0 chmod <otherargs>
  }
  else {
    find <paths> -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 chmod <otherargs>
  }
}

您是否已经知道存在这样的功能/二进制文件,或者您能帮助我实现这一目标吗?


我想要这个的主要原因是,我发现自己经常需要在目录上递归设置 setgid 位,而不是在文件上。但是,据我所知g+S,chmod 没有大写字母选项。


1)我知道 unix 慢板“做一件事并做好”,或者在那个程度上,但老实说,至少据我所知,经过近半个世纪的存在chmod并看到对此行为的大量请求,chmod尚未使用此功能进行修改。这似乎是一个显而易见且合适的功能chmod。

bash find
  • 2 2 个回答
  • 136 Views

2 个回答

  • Voted
  1. Best Answer
    Kamil Maciorowski
    2021-03-02T18:01:41+08:002021-03-02T18:01:41+08:00

    我的尝试:

    chmodx() (
       type="$1"
       shift
       cleared=
       marked=
       recursive=
       for a do
          if [ -z "$cleared" ]; then
             set -- -type "$type" -exec chmod
             cleared=y
          fi
          if [ -z "$marked" ]; then
             case "$a" in
             -- )
                set -- "$@" -- {} +
                if [ -z "$recursive" ]; then
                   set -- -prune "$@"
                fi
                marked=y
                ;;
             -R|--recursive )
                recursive=y
                ;;
             * )
                set -- "$@" "$a"
                ;;
             esac
          else
             set -- "$a" "$@"
          fi
       done
       if [ -z "$marked" ]; then
          printf '`--'"'"' required\n'
          return 1
       fi
       exec find "$@"
    )
    
    alias chmodf='chmodx f'
    alias chmodd='chmodx d'
    

    函数和别名轮流

    chmodf  args for chmod  --  starting points
    

    进入

    find  points starting  -type f -exec chmod  args for chmod  {} +
    

    同样chmodd …变成find … -type d ….

    它主要是关于重新安排论点。几乎没有解析。之前的所有内容都--被视为args for chmod. 之后的一切都--视为starting pointsfor find。我们不想真正chmod递归地运行,但我们可能会也可能不会find递归地运行。-prune由于在正确的位置执行非递归操作。如果有-R或--recursive在args for chmod则它将消失但-prune不会出现。

    笔记:

    • 我认为代码是可移植的。我故意避免使用数组。

    • chmodf并且chmodd可能是函数。即使这样,他们也应该调用chmodx以保持代码DRY。将它们实现为两个独立的功能是不优雅的。

    • --是强制性的,因为我决定亲吻。当然可以将路径名与其他参数区分开来,而无需使用--. 实际上chmod是这样做的。它知道自己的语法、所有可能的选项等等;所以其他一切都必须是要操作的文件。将这些知识和功能嵌入到 shell 脚本中就是WET。如果你真的想让事情在没有的情况下工作,--那么你应该通过改进chmod、修改它的源来解决你的问题。

    • 滥用方法有:

      • 我的代码没有 validate ,因此您可以通过这种方式starting points注入例如(因为become )。-maxdepth 33 -maxdepthstarting pointspoints starting
      • 同样没有验证args for chmod. 如果args for chmod包含;或{} +则-exec主节点将在该行的早期终止。这允许您注入更多的原色;或通过生成无效语法来破坏整个命令。

      虽然在技术上是可行的,但这样的技巧是丑陋的。如果我需要使事情复杂化,我肯定更愿意重新编写一个find命令。

    • 1
  2. Decent Dabbler
    2021-03-02T16:52:47+08:002021-03-02T16:52:47+08:00

    我设法想出了我自己的初始功能的更高级版本:

    仅文件chmod( chmodf):

    function chmodf {
      local maxdepth='-maxdepth 1'
      local paths=()
      local args=();
      for arg do
        if [[ "$arg" == '-R' ]]; then
          maxdepth=''
        elif [[ -e "$arg" ]]; then
          paths+=("$arg")
        else 
          args+=("$arg")
        fi
      done
      if [ -z "$paths" ]; then
        paths+=('.')
      fi
      set -- ${args[@]}
      find "${paths[@]}" $maxdepth -type f -print0 | xargs -0 chmod "$@"
    }
    

    仅目录chmod( chmodd):

    function chmodd {
      local maxdepth='-maxdepth 0'
      local paths=()
      local args=();
      for arg do
        if [[ "$arg" == '-R' ]]; then
          maxdepth=''
        elif [[ -e "$arg" ]]; then
          paths+=("$arg")
        else 
          args+=("$arg")
        fi
      done
      if [ -z "$paths" ]; then
        paths+=('.')
      fi
      set -- ${args[@]}
      find "${paths[@]}" $maxdepth -type d -print0 | xargs -0 chmod "$@"
    }
    

    我唯一担心的是某些参数可能不明确,并被错误地解释为带有if [[ -e "$arg" ]]; then. 不知道这通常是如何处理的,即使是getopts. 但除此之外,它似乎按我的意图工作。

    例子:

    chmodf g+rx ./* # add group read and execute bit to first level files in ./*
    chmodf -R g+rx ./* # add group read and execute bit recursively to files in ./*
    
    chmodd g+rx ./* # add group read and execute bit to first level directories in ./*
    chmodd -R g+rx ./* # add group read and execute bit recursively to directories in ./*
    # etc.
    

    仍然好奇其他人是否有替代解决方案。

    • 0

相关问题

  • 在非 root 用户上用 bash 替换 zsh

  • 在 macOS High Sierra 的终端中设置环境变量时遇到问题

  • 对于 cp 或 mv,是否有等同于 cd - 的东西?

  • Notify-发送窗口下出现的通知

  • 如何从 WSL 打开 office 文件

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve