簡體   English   中英

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

[英]How to modify bash command to change permissions

我有此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是目錄並且名稱為ProductA ,則不要進入( -prune ),否則( -o表示or)如果file是目錄,則在其上執行chmod 755

find表達式由選項,測試(可以為false或true)和操作組成,並由運算符分隔。 如果未給出任何操作,則對表達式為true的所有文件執行-print操作。 -exec是一個動作, -prune是另一個動作。 您可以使用-a-o鏈接多個動作。 expr1 -a expr2將執行這兩個動作,而expr1 -o expr2 expr2僅在expr1計算為false時才執行expr2

因此,如果要排除多個目錄,可以編寫

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

如果您具有更復雜的目錄結構,則說要排除ProductA/data而不是ProductB/dataProductA/images ,則可以使用-path測試:

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

您可以嘗試以下方法:

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

find . -type d \\(-name "*" find . -type d \\(-name "*" -列出當前目錄中的所有目錄

!-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