简体   繁体   English

如何修改bash命令以更改权限

[英]How to modify bash command to change permissions

I have this bash command to modify all files and folders permissions inside a root folder: 我有此bash命令来修改根文件夹内的所有文件和文件夹权限:

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;

it work ok. 它工作正常。 What i should do to to exclude some folders for change permissions inside the root folder(where the bash is executed). 我应该怎么做才能在根文件夹(执行bash的位置)内排除某些文件夹的更改权限。 For example, if i want to keep folder permission for folder "A" and folder "B" Thanks in advance 例如,如果我想保留文件夹“ A”和文件夹“ B”的文件夹权限,请先感谢

Exclude directories with -prune option: 使用-prune选项排除目录:

find . -type d -name ProductA -prune -o -type d -exec chmod 755 {} \;

It tells: if file is a directory and has name ProductA , then do not descend into it ( -prune ), else ( -o means or) if file is a directory, then execute the chmod 755 on it. 它表明:如果file是目录并且名称为ProductA ,则不要进入( -prune ),否则( -o表示or)如果file是目录,则在其上执行chmod 755

find expressions are made of options, tests (can be false or true) and actions, separated by operators. find表达式由选项,测试(可以为false或true)和操作组成,并由运算符分隔。 When no action is given, then -print action is performed on all files for which the expression is true. 如果未给出任何操作,则对表达式为true的所有文件执行-print操作。 -exec is an action, -prune is another. -exec是一个动作, -prune是另一个动作。 You can chain multiple actions with -a and -o . 您可以使用-a-o链接多个动作。 expr1 -a expr2 will execute both actions, whereas expr1 -o expr2 will execute expr2 only if expr1 evaluates to false. expr1 -a expr2将执行这两个动作,而expr1 -o expr2 expr2仅在expr1计算为false时才执行expr2

So if you want to exclude multiple directories, you can write 因此,如果要排除多个目录,可以编写

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 {} \;

or just: 要不就:

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 {} \;

and you can combine them too: 您也可以将它们结合起来:

find . -type d -name "Product[AC]" -prune -o -type d -exec chmod 755 {} \; -o -type f -exec chmod 644 {} \;

If you have a more complex directory structure, say you want to exclude ProductA/data but not ProductB/data nor ProductA/images , then you can use the -path test: 如果您具有更复杂的目录结构,则说要排除ProductA/data而不是ProductB/dataProductA/images ,则可以使用-path测试:

find . -path ./ProductA/src -prune -o -print

You can try this: 您可以尝试以下方法:

find . -type d \\(-name "*" ! -name "A" ! -name "B" \\) -exec chmod 755 {}\\;

find . -type d \\(-name "*" find . -type d \\(-name "*" - lists all the directories in the current directories find . -type d \\(-name "*" -列出当前目录中的所有目录

!-name "A" !-name "B" - ignores directories with names A and B !-name "A" !-name "B" -name !-name "A" !-name "B" -忽略名称为A和B的目录

您可以使用egrep -v排除多个目录,例如dirname1, dirname2 ,然后使用xargs执行chmod

find . -type d | egrep -v "(dirname1|dirname2)" | xargs chmod 755

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM