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.
精彩评论