您的位置:首页 > 运维架构 > Shell

shell 脚本实现的遍历目录修改子目录及其文件权限

2010-06-02 20:22 1196 查看
      下面文章是用来递归修改指定目录下文件和自目录权限的一个脚本实现,对于指定文件的修改,可以通过指定后缀名来实现,脚本中sud变量接受指定的后缀名

  1 #!/bin/sh

  2 #

  3 # Filename:list_dir.sh


  4 #
Author:CaoJiangfeng

  5 # Date: 2010-06-01

  6 #

  7 # The script is used
to change file attributes

  8 # Define a function

  9

 10 list_dir(

)

{

 11 # Traversal parameter $1

 

12
for

file in $

1

/

*

 13 do

 14 # If it is a directory then
treat it ,after it's treated traverse it

 

15 if

[

-

d $

file

]

;

then

 16 echo "$file is directory"

 17 chmod

755 $

file

 18 list_dir $

file

 19 elif [

-

f $

file

]

;

 20 then

 21 echo $

file

 22 #suffix=`echo -n $file |cut
-f 2 -d '.'`

 

23 #suffix=
echo -n "`echo $file |cut -f 3 -d '.'`"

 

24 suffix=

`

echo -

n $

file

|

awk -

F.

'{print $NF}'

`

 25 #echo "$suffix"

 26 echo "$file is file "

 27 chmod 644 $file

 28 sud=

sh # The varable is used to designate
which suffix will be modified

 

29

 30                 if

[

"$suffix"

=

"$sud"

]

;

 31                 then

 32                     chmod

744 $

file

 33                     echo "$file
changed"

 34                 fi

 35                 list_dir $

file

 36        fi

 37 done

 38 }

 39

 40

 41

 42 # If there is parameter to
traverse the specified directory,

 

43 # otherwise the current directory traversal

 

44
if

[

$

# -gt 0 ] ;

 

45 then

 46     list_dir "$1"

 47 else

 48     list_dir "."

 49 fi


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息