
我目前正在嘗試將.html
當前目錄及其所有子目錄中具有擴展名的所有檔案設定為可由其所有者讀取、寫入和可執行,並且只能由群組和其他人讀取(不可寫入或可執行)。但是,有些文件的名稱中有空格,我不確定如何處理。
我的第一次嘗試:
chmod -Rv u+rw,go+r '* *.html'
當我第一次嘗試時,我收到以下訊息:
chmod: cannot access '* *.html': No such file or directory
failed to change mode of '* *.html' from 0000 (---------) to 0000 (---------)
我的第二次嘗試:
find . -type f -name "* *.html" | chmod -Rv u+rw,go+r
我添加了一個管道運算符,以便將find
命令的輸出發送到chmod
.然而,當我嘗試第二次嘗試時,我得到以下資訊:
chmod: missing operand after ‘u+rw,go+r’
經過我的嘗試,我仍然對如何處理檔案名稱中的空格以便遞歸更改設定權限感到困惑。處理這個問題的最佳方法是什麼?如有任何回饋或建議,我們將不勝感激。
答案1
使用-exec
謂詞find
:
find . -name '* *.html' -type f -exec chmod -v u+rw,go+r {} +
(這裡,rw-r--r--
僅添加權限,因為向 html 檔案添加執行權限沒有什麼意義,因為這些權限通常並不意味著被執行。替換+
為=
放這些權限完全代替添加這些位到目前權限)。
您也可以! -perm -u=rw,go=r
在 之前新增 來-exec
跳過已經(至少)具有這些權限的檔案。
隨著sfind
實施find
(這也是find
內建的這bosh
貝殼),您可以使用-chmod
後跟的謂詞-chfile
(套用變更):
sfind . -name '* *.html' -type f -chmod u+rw,go+r -chfile
(在那裡,不需要添加! -perm...
assfind
已經-chfile
跳過已經具有正確權限的檔案)。
這是最有效的,因為它不涉及chmod
在新進程中為每個檔案執行單獨的命令,而且還因為它避免了兩次查找每個檔案的完整路徑(使用檔案的路徑呼叫系統呼叫sfind
) 在爬行期間找到它們,這意味著不需要再次查找通往它們的所有路徑組件)。chmod()
sfind
chmod()
和zsh
:
chmod -v -- u+rw,go+r **/*' '*.html(D.)
這裡使用 shell 的遞歸通配符和D
and.
全域限定符分別包含隱藏檔案並限制為常規的文件(就像-type f
那樣)。新增^f[u+rw,go+r]
也可以跳過已經具有這些權限的檔案。
您不能將chmod
s-R
與 glob 結合使用。通配符由 shell 擴展,不與 匹配chmod
,因此chmod -Rv ... *' '*.html
(請注意,*
必須不加引號,shell 才能將它們解釋為通配符運算符),您只需將html
文件列表傳遞給chmod
且 僅當這些文件中的任何一個是目錄將chmod 遞歸到其中並更改其中所有檔案的權限。
答案2
「帶空格的檔名」需要find
和xargs
。閱讀man find xargs
,並執行以下操作:
find . -type f -name '*.html' -print0 | \
xargs -0 -r echo chmod u=rwx,g=r,o
echo
當它對你有用時刪除“ ”。