开发者

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

开发者 https://www.devze.com 2023-01-04 04:27 出处:网络
I have a directory of csv files with spaces and all kinds of characters. How do I rename them? The following gives an error.

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

精彩评论

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