遞歸遍歷目錄修改文件和目錄權限

1.下面文章是用來遞歸修改指定目錄下文件和自目錄權限的一個腳本實現,對於指定文件的修改,能夠經過指定後綴名來實現,腳本中sud變量接受指定的後綴名



shell

  1 #!/bin/sh
  2 #
  3 # Filename:list_dir.sh
bash

  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
spa


2.在通過上述腳本的運行後,發現若是要皮兩修改指定文件後綴的多種文件類型的權限的時候,有點不能達到與其目標,因而對上述腳本進行了擴充,使其能對多種文件類型的文件進行權限修改,使用的是for循環,代碼以下

code

#!/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語句進行修改了一下上述腳本,很不錯,程序以下:

ip

#!/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
ci

相關文章
相關標籤/搜索