linux shell递归修改文件夹内的所有文件名

文件名为:build.sh

#!/bin/bash

echo "###################################################################"
echo "# Preparing to Recursively modify files in the folder"
echo "###################################################################"
echo "$(tput sgr0)"
function readFile() {
    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!DIR $1"
    for dirlist in $(ls)
    do
        if test -d ${dirlist}
        then
            cd ${dirlist}
            readFile ${dirlist}
            cd ..
        else
            echo "$dirlist"
            newfile=`echo $dirlist | sed 's/_//g'`
            mv $dirlist $newfile
        fi
    done
}

function setDir() {
    if test -d $1
    then
        cd $1
        readFile
        cd ..
    else
        cd $(dirname $1)
        local name=$(basename $1)
        newfile=`echo $name | sed 's/vvv/_/g'`
        mv $name $newfile
    fi
}

local param=$1
if [ -z "$1" ]
then
    param="./"
    echo "empty string: $param"
else
    param=$1
fi
if test -d $param
then
    setDir $param
elif test -f $param
then
    setDir $param
    exit 1
else
    echo "Neither folder nor file!!!"
    exit 1
fi
然后在终端执行

./build.sh

或者

./build.sh ./

函数就会把build.sh所在的文件夹下的所有文件夹和文件列出来。

然后看sed 's/_//g'这句代码,意思是找出文件名里的"_"去掉。


猜你喜欢

转载自blog.csdn.net/u013654125/article/details/80020261
今日推荐