我有这个 bash 命令来修改根文件夹内的所有文件和文件夹权限:
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
它工作正常。我应该做些什么来排除一些文件夹以更改根文件夹(执行bash的地方)内的权限。例如,如果我想保留文件夹“A”和文件夹“B”的文件夹权限提前谢谢
我有这个 bash 命令来修改根文件夹内的所有文件和文件夹权限:
find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
它工作正常。我应该做些什么来排除一些文件夹以更改根文件夹(执行bash的地方)内的权限。例如,如果我想保留文件夹“A”和文件夹“B”的文件夹权限提前谢谢
-prune
使用选项排除目录:
find . -type d -name ProductA -prune -o -type d -exec chmod 755 {} \;
它告诉:如果 file 是一个目录并且有 name ProductA
,那么不要进入它(-prune
),否则(-o
意味着或)如果 file 是一个目录,然后执行chmod 755
它。
find
表达式由选项、测试(可以为假或真)和操作组成,由运算符分隔。如果未给出任何操作,则-print
对表达式为真的所有文件执行操作。-exec
是一个动作,-prune
是另一个。-a
您可以使用和链接多个操作-o
。expr1 -a expr2
将执行这两个动作,而仅在评估为 falseexpr1 -o expr2
时执行。expr2
expr1
所以如果你想排除多个目录,你可以写
find . -type d -name ProductA -prune -o -type d -name ProductC -prune -o -type d -exec chmod 755 {} \;
find . -type d -name ProductA -prune -o -type d -name ProductC -prune -o -type f -exec chmod 644 {} \;
要不就:
find . -type d -name "Product[AC]" -prune -o -type d -exec chmod 755 {} \;
find . -type d -name "Product[AC]" -prune -o -type f -exec chmod 644 {} \;
你也可以将它们组合起来:
find . -type d -name "Product[AC]" -prune -o -type d -exec chmod 755 {} \; -o -type f -exec chmod 644 {} \;
如果你有一个更复杂的目录结构,比如你想 excludeProductA/data
但不是ProductB/data
nor ProductA/images
,那么你可以使用-path
测试:
find . -path ./ProductA/src -prune -o -print
你可以试试这个:
find . -type d \(-name "*" ! -name "A" ! -name "B" \) -exec chmod 755 {}\;
find . -type d \(-name "*"
- 列出当前目录中的所有目录
!-name "A" !-name "B"
- 忽略名称为 A 和 B 的目录
您可以排除多个目录,例如dirname1, dirname2
usingegrep -v
然后执行chmod
using xargs
。
find . -type d | egrep -v "(dirname1|dirname2)" | xargs chmod 755