开发者

Help with shell script - Rename filenames (remove special chars) in a directory?

I have a directory of csv files with spaces and all kinds of characters. How do I rename them? The following gives an error.

#! /bin/bash

cd DirectoryName

for file in *.csv; do
    #echo $file
    filename=${file%.*}
    file_clean=${filename//[ ()$+&\.\-\'\,]/_}
    final= "$file_clean.csv"
    mv "$file" $fina开发者_运维知识库l
done

cd ..

Thanks!

UPDATE : (This works)

#! /bin/bash

cd DirectoryName

for file in *.csv; do
    #echo $file
    filename=${file%.*}
    file_clean=${filename//[ ()$+&\.\-\'\,]/_}
    final= "$file_clean.csv"
    mv "$file" $final
done

cd ..


Of course it does. You're not quoting the substitutions, and your assignment to $final is incorrect. Quote all usages of substitution, and remove the space after the equal sign.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜