Linux迴圈遍歷所有檔案,刪除指定字尾名檔案

password-u發表於2019-01-04
#!/bin/bash
# 迴圈遍歷當前目錄下的所有檔案,並刪除.pyc檔案
function getfile(){
    for e in `ls $1`
    do  
        fullpath=$1/$e
        if [ -f $fullpath ]; then
            prefix=${fullpath:0-4}
            if [ $prefix == ".pyc" ]; then
                echo delete file:$fullpath
                rm -rf $fullpath
            fi  
        elif [ -d $fullpath ]; then
            getfile $fullpath
        fi  
    done
}

read -p "Are you sure you want to delete all *.pyc files?[Y]:" flag
if [[ ! $flag || "$flag" == "Y" || "$flag" == "y" ]]; then
    getfile .
fi

 

相關文章