透過 +/- r, w, x 或 八進制數值 控制權限
普遍地,利用
1 | chmod -R 0777 /var/www |
雖然這個指令可以將整個目錄及檔案的設定為 0777
但是一般檔案,如在例子中的 /var/www 大部分都可文字檔案,並不希望檔案具備執行權限
因此這種方法在這種狀態並不適合
在下編寫了 shell script 可分別將目錄及檔案的權限分開設定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #!/bin/sh run(){ path= "${1}" dir = "${2}" file = "${3}" cdir= "${4}" cfile= "${5}" for f in "${path}/" *; do if [ -d "${f}" ]; then cdir=$((cdir+1)) echo "chmod '${dir}' '${f}'" chmod "${dir}" "${f}" run "${f}" "${dir}" "${file}" "${cdir}" "${cfile}" elif [ -f "${f}" ]; then cfile=$((cfile+1)) echo "chmod '${file}' '${f}'" chmod "${file}" "${f}" fi done } cdir= '0' cfile= '0' if [ "${#}" - eq '1' ]; then dir = '0755' file = '0644' elif [ "${#}" - eq '3' ]; then dir = "${2}" file = "${3}" else echo "${0} <path> [dir_mode=0755 file_mode=0644]" exit fi run "${1}" "${dir}" "${file}" "${cdir}" "${cfile}" if [ "${cdir}" - eq '0' ]; then echo 'No directories are changed' elif [ "${cdir}" - eq '1' ]; then echo "${cdir} directory changes to ${dir}" else echo "${cdir} directories change to ${dir}" fi if [ "${cfile}" - eq '0' ]; then echo 'No files are changed' elif [ "${cfile}" - eq '1' ]; then echo "${cfile} file changes to ${file}" else echo "${cfile} files change to ${file}" fi # |
script 中最後的 # 是不需要的
使用方法相當簡單
script 只需要任意名稱、如 a.out
利用
1 | . /a .out <path> [dir_mode=0755 file_mode_=0644] |
dir_mode 可選,為目錄權限;若不設定,為 0755
file_mode 可選,為檔案權限;若不設定,為 0644
例如
1 | . /a .out /var/www 0755 0644 |
當然,若果閣下的 Linux 有安裝 X11,便可以直接透過 GUI 設定
沒有留言 :
張貼留言