tags:

views:

33

answers:

1

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" $final
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 ..
+1  A: 

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.

Ignacio Vazquez-Abrams
You sir, are a master :) Never realised a space will mess it up!
ThinkCode