- 論壇徽章:
- 0
|
(問題已解決。
現(xiàn)有文件夾,目錄結(jié)構(gòu)如下:
/tmp/test
/tmp/test/1/1/1/1/1/1/1/1/1/1/1
/tmp/test/1/2/2/2/2/2/2//2/2/2/2
/tmp/test/2/1/1/1/1/1//1/1/1/1/1
/tmp/test/2/3/3/3/3/3/3/3/3/3/3/3
/tmp/test/3/4/4/4/4/4/4/4/4/4/4/4
/tmp/test/3/5/5/5/5/5/5/5/5/5/5/5
現(xiàn)要求寫一個shell程序,對如上目錄逐級授權(quán)(chmod 755) ,也就是說先進(jìn)test目錄,對1,2,3目錄授權(quán),然后再進(jìn)1的子目錄,對1,2授權(quán),以此類推,1級目錄授權(quán)完畢后,再進(jìn)2級目錄授權(quán)。一個分支授權(quán)完畢后進(jìn)入另外一個目錄分支授權(quán)。
ps : 因為每一級目錄下都有幾千萬的文件,我不需要對文件操作,只需要對文件夾操作,以減輕操作量。
目前,我只實現(xiàn)了授權(quán)完一個目錄分支,就退出來了,代碼如下:
- #!/bin/bash
- # site: Chinaunix Author: echo52
- # name: chmodfile
- PATH=$PATH:/bin:/sbin:/usr/bin:/usr/local/bin
- DIRPATH1=`find $1 -maxdepth 1 -type d|sed '1d'`
- DIRNUM=1
- # 授權(quán) 初始 目錄
- chmod 755 $DIRPATH1
- #echo "$DIRPATH1"
- until [ $DIRNUM = 0 ]
- do
-
- for a in $DIRPATH1
- do
- A_DIRPATH=`echo $a`
-
- echo "授權(quán)路路徑 $A_DIRPATH"
- chmod 755 $A_DIRPATH
-
- DIRPATH1=`find $A_DIRPATH -maxdepth 1 -type d|sed '1d'`
- DIRNUM=`find $A_DIRPATH -maxdepth 1 -type d|sed '1d'|wc -l`
- done
- done
復(fù)制代碼
執(zhí)行結(jié)果如下:
- [root@test tmp]# ./chmodfile ./
- 授權(quán)路路徑 ./test
- 授權(quán)路路徑 ./test/2
- 授權(quán)路路徑 ./test/1
- 授權(quán)路路徑 ./test/3
- 授權(quán)路路徑 ./test/3/4
- 授權(quán)路路徑 ./test/3/5
- 授權(quán)路路徑 ./test/3/5/5
- 授權(quán)路路徑 ./test/3/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5/5/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5/5/5/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5/5/5/5/5/5/5
- 授權(quán)路路徑 ./test/3/5/5/5/5/5/5/5/5/5/5/5
復(fù)制代碼
可以看到只是 ./test/3/5/5/5/5/5/5/5/5/5/5/5 完全完成了,DIRNUM 為0了,循環(huán)退出。
總結(jié)一下樓下各位的建議,這個問題的通用性比較好的解決方法如下:
- #!/bin/bash
- # site: Chinaunix
- # Author: crisman,damofeixue,ivhb,echo52
- # name: chmodfile
- # 如若轉(zhuǎn)載,引用, 請保留以上信息
- if [ $# -ne 2 ]
- then
- echo " "
- echo " usage: chmodfile DIR NUM"
- echo " DIR 為需要授權(quán)的起始目錄 NUM為需要授權(quán)的目錄級數(shù)(為正的整數(shù))"
- echo " 例如: ./chmodfile ./ 5
- echo " "
- exit 1
- fi
- for i in `seq 1 $2`
- do
- find $1 -mindepth $i -maxdepth $i -type d |xargs chmod 755
- done
復(fù)制代碼
感謝crisman,damofeixue,ivhb 特此結(jié)貼!
[ 本帖最后由 echo52 于 2009-7-14 16:33 編輯 ] |
|