Click to See Complete Forum and Search --> : Rename isn't working in script, not sure why


MkIII_Supra
08-19-2010, 01:48 PM
I am trying to get this simple rename script to work. I have two standard file names in use. The first one is a csv file that looks like this:

Example: 201008.csv

The other file is a text file that looks like this:

Example: 201008 Dump Record.txt

There are several files like these and I want to rename them like this:

Example: 201008 2010-08-19(0945).csv
Example: 201008 Dump Record 2010-08-19(0945).txt

But the mv command keeps giving me this error:

$: ./bshFileNameTest
1
200901 2010-08-19 (1039).csv
mv: target `(1039).csv' is not a directory
1
200901 Dump Record
200901 Dump Record 2010-08-19 (1039).txt
mv: target `(1039).txt' is not a directory
1

Below is the main portion, what is missing are some preliminary steps. Anyhow I can't seem to figure this one out.

for file in *; do
if echo "$file" | grep -q '.csv$'; then
if expr length "$file" = 10; then
PRE=${file:0:6}
REN=$PRE$RENUB".csv"
mv $file $REN
fi

elif echo "$file" | grep -q '.txt$'; then
if expr length "$file" = 22; then
PRE=${file:0:18}
REN=$PRE$RENUB".txt"
mv $file $REN
fi
fi
done

MkIII_Supra
08-19-2010, 02:26 PM
Hmmm... okay I figured out how to get the first file to rename, but the second one still isn't working.

JohnT
08-19-2010, 07:43 PM
Is that a redwood behind you? In the National Forest for the day?

michaelk
08-21-2010, 12:04 PM
The problem is due to spaces in your file names.
Try
mv "$file" "$REN"