Permissions

CHMOD - 對文件和目錄應用不同的權限

  • March 5, 2017

我一直在嘗試清理幾個盒子的權限,並且一直在搜尋 chmod 人以及我沒有任何運氣處理的所有網際網路文件——所以我們開始吧。

基本上,我有一個包含許多子目錄和文件的目錄——我想設置以下權限:

對於目錄:770 (u+rwx, g+rwx, o-rwx)

對於文件:660(U+rw、g+rw、ax、o-rw)

如果可能,我想嘗試使用單個遞歸 chmod 來執行此操作 - 以避免遍歷每個目錄並設置逐個文件的權限。

我想必須有一種方法可以在不編寫我自己的 shell 腳本的情況下做到這一點——但我找不到任何東西。

我感謝您的幫助!

我確實發現一個腳本很有用,因為一次更改文件和目錄權限通常很有用,而且它們通常是連結的。770 和 660 用於文件伺服器上的共享目錄,755/644 用於 Web 伺服器目錄等。我在 root 的 bin/ 中保留了一個帶有該類型伺服器最常用模式的腳本,並且當常見時手動進行查找模式不適用。

#!/bin/sh
# syntax: setperm.s destdir
#
if [ -z $1 ] ; then echo "Requires single argument: <directoryname>" ; exit 1 ;                                       fi

destdir=$1

dirmode=0770
filemode=0660

YN=no

printf "\nThis will RECURSIVELY change the permissions for this entire branch:\n                                      "
printf "\t$destdir\n"
printf "\tDirectories chmod = $dirmode\tFiles chmod = $filemode\n"
printf "Are you sure want to do this [$YN]? "

read YN

case $YN in
       [yY]|[yY][eE][sS])
       # change permissions on files and directories.
       find $destdir -type f -print0 | xargs -0 chmod $filemode $i
       find $destdir -type d -print0 | xargs -0 chmod $dirmode $ii ;;

       *) echo "\nBetter safe than sorry I always say.\n" ;;
esac

引用自:https://serverfault.com/questions/104804