递归遍历目录修改文件和目录权限

1.下面文章是用来递归修改指定目录下文件和自目录权限的一个脚本实现,对于指定文件的修改,可以通过指定后缀名来实现,脚本中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


2.在经过上述脚本的运行后,发现如果要皮两修改指定文件后缀的多种文件类型的权限的时候,有点不能达到与其目标,于是对上述脚本进行了扩充,使其能对多种文件类型的文件进行权限修改,使用的是for循环,代码如下

#!/bin/sh

#

# Filename:list_dir.sh

# Author:CaoJiangfeng

# Date: 2010-08-02 15:06:38

# Version:3.0

# The script is used to change file attributes

# Define a function


list_dir(){
# Traversal parameter $1

for file in $1/*
do
    # If it is a directory then treat it ,after it's treated traverse it

    if [ -d $file ] ;
    then    
        echo "$file is directory"
        chmod 755 $file
        echo "Directory $file changed to 755 "
        list_dir $file
    elif [ -f $file ] ;
    then    
        suffix=`echo -n $file|awk -F. '{print $NF}'` #获取$file文件的后缀

        chmod 644 $file
        echo "Regular file  $file changed to 644"
        for mysuffix in pl plx sh out #指定特定文件的后缀

        do
            if [ $mysuffix = $suffix ];
            then
                chmod 755 $file
                   echo "file $file changed to 755"
            fi
        done
            list_dir $file
       fi
done
}

# If there is parameter to traverse the specified directory,

# otherwise the current directory traversal

if  [ $# -gt 0 ]

then
    list_dir "$1"
else
    list_dir "."
fi


3.经过修改后的代码能够对perl,shell,a.out文件等特殊文件进行特定权限修改,可是每次修改都要循环,占用时间长并且对文件进行多次的进行权限修改,今天我使用case语句进行修改了一下上述脚本,很不错,程序如下:

#!/bin/bash

#

# Filename:list_dir.sh

# Author:CaoJiangfeng

# Date: 2010-08-14 20:16:20

# Version:4.0

# The script is used to change file attributes

# Define a function


list_dir(){
# Traversal parameter $1

for file in $1/*
do
    # If it is a directory then treat it ,after it's treated traverse it

    if [ -d $file ] ;
    then    
        echo "$file is directory"
        chmod 755 $file
        echo "Directory $file changed to 755 "
        list_dir $file
    elif [ -f $file ] ;
    then    
        suffix=`echo -n $file|awk -F. '{print $NF}'` #获取$file文件的后缀

    #下面case语句对文件权限进行修改  

        case $suffix in
            "pl") chmod 755 $file
                   echo "file $file changed to 755"
            ;;
            "plx") chmod 755 $file
                   echo "file $file changed to 755"
            ;;
            "sh") chmod 755 $file
                   echo "file $file changed to 755"
            ;;
            "out") chmod 755 $file
                   echo "file $file changed to 755"
            ;;
            *)chmod 644 $file
                   echo "Regular file  $file changed to 644"
            ;;
        esac
           list_dir $file
       fi
done
}

# If there is parameter to traverse the specified directory,

# otherwise the current directory traversal

if  [ $# -gt 0 ]

then
    list_dir "$1"
else
    list_dir "."
fi



你可能感兴趣的:(递归遍历目录修改文件和目录权限)