shell递归删除文件或目录


#!/bin/bash 
#功能:删除某一目录下所包含的所有的指定名称的文件及文件夹
#2015-4-18
#wangzg
#例如:删除当前目录下所有的.svn文件及文件夹,./fast_delete.sh ./ .svn
function scandir() {   
    local cur_dir workdir cur_file 
    workdir="$1"
    cur_file="$2"
    if [ "$workdir" = "/" ]   
        then   
            cur_dir=""   
    else   
            cur_dir="$(pwd)"   
    fi   
    for dirlist in $(ls -a "$cur_dir") 
    do
        if [ "$dirlist" = "$cur_file" -a "$dirlist" != "." -a "$dirlist" != ".." ] ;then
            if [ -d "$cur_dir/$cur_file" ];then
                echo "删除文件夹$cur_dir/$cur_file"
            elif [ -f "$cur_dir/$cur_file" ];then
                echo "删除文件$cur_dir/$cur_file"
            fi
            rm -rf "$cur_dir/$cur_file"
            let 'target++'
        else
            if test -d "$dirlist" -a "$dirlist" != "." -a "$dirlist" != ".." -a "$cur_dir/$dirlist" != "/proc" -a "$cur_dir/$dirlist" != "/mnt" -a "$cur_dir/$dirlist" != "/sys" -a "$cur_dir/$dirlist" != "/dev"
                then 
                cd "$dirlist"           
                scandir "$cur_dir/$dirlist" "$2"
                cd ..
            fi
        fi 
    done   }   

IFS=$(echo -en "\n\b")
echo -en $IFS
target=0
count=0
if test -d "$1"   
    then   
        cd "$1"
        scandir "$1" "$2"
        echo "共删除$target个目标"
    elif test -f "$1"   
    then   
        echo "$1不是目录"   
        exit 1   
    else   
        echo "目录$1不存在"
        exit 1   
fi
IFS=$BAK_IFS