【问题标题】:How to exclude files from chown recursive using wildcard in file name?如何使用文件名中的通配符从 chown 递归中排除文件?
【发布时间】:2020-01-02 14:06:32
【问题描述】:

我想在文件夹上递归运行chown,但我不想包含名为"ssl.cert.webmintmp.26599""ssl.cert.webmintmp.356849" 的文件,所以我需要用通配符替换末尾的数字。而且该文件之前的文件夹应该是通配符,因为有几个不同的文件夹名称。

我试过跑步:

sudo chown -R user:apache /home/ !({ssl.cert.**,**/ssl.cert.**})

根据我在这个问题上的发现,它不起作用; (How do I exclude a folder when performing file operations i.e. cp, mv, rm and chown etc. in Linux)

当我遇到错误时;

./chowntest.sh: line 2: syntax error near unexpected token `('
./chowntest.sh: line 2: `sudo chown -R user:apache /home/ !({ssl.cert.**,**/ssl.cert.**})'

这可行吗?

【问题讨论】:

  • 你考虑过运行find ... -exec chown ...吗?
  • chown -R 简单地递归您作为参数传递的目录;没有排除设施。我将支持使用find 的建议。
  • 错误消息看起来您没有启用extglob,但无论如何它对chown -R 没有帮助。
  • @LuisFelipe。恭喜,您现在除了选择答案之外,还有足够的声望来投票!
  • find 命令似乎比chown 花费的时间要长得多,这就是为什么我想知道是否可以不使用它的原因。

标签: linux bash centos7 rhel7


【解决方案1】:

chown -R 不会对您传入的内容进行任何解释。任何使用 !(...) 之类的东西的尝试都由 shell 解释。如果您想在递归中支持花哨的逻辑,请使用 find 之类的东西:

find /home ! -name 'ssl.cert.*' -exec chown user:apache '{}' \;

【讨论】:

    猜你喜欢
    • 2011-01-06
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 2019-06-08
    • 2011-03-25
    • 2022-09-26
    相关资源
    最近更新 更多