Batch process bash rename script
by Johng from LinuxQuestions.org on (#56D9M)
I am attempting to write a script to batch rename image files in a directory. ie change the image name from STA_1234.JPG to 1234-STA.jpg. The "STA" may be STB, STC, etc. The part that I'm having trouble with is "reading" the file names in sequence. The script so far looks like this:
Code:#!/bin/bash
echo
pwd
dirname "$(realpath $0)"
echo $dirname
for f in "$dirname"/*; do
read -a FILENAME #read -e FILENAME ###THE PART THAT DOES NOT WORK
FILENAMEX=$FILENAME
echo $FILENAMEX
filename=$FILENAMEX
#strip extension
JPGFile=${filename%.*}
echo "$JPGFile: "$JPGFile
#get file number
string=$JPGFile
echo "$string: "$string
number=${string:4}
echo "$number: "$number
#get panorama part
part=${string:0:-5} #part=${string:0:-5}
echo $part
#create new file name
rename="$number"-"$part".jpg""
echo $rename
echo
mv $FILENAMEX $rename
done


Code:#!/bin/bash
echo
pwd
dirname "$(realpath $0)"
echo $dirname
for f in "$dirname"/*; do
read -a FILENAME #read -e FILENAME ###THE PART THAT DOES NOT WORK
FILENAMEX=$FILENAME
echo $FILENAMEX
filename=$FILENAMEX
#strip extension
JPGFile=${filename%.*}
echo "$JPGFile: "$JPGFile
#get file number
string=$JPGFile
echo "$string: "$string
number=${string:4}
echo "$number: "$number
#get panorama part
part=${string:0:-5} #part=${string:0:-5}
echo $part
#create new file name
rename="$number"-"$part".jpg""
echo $rename
echo
mv $FILENAMEX $rename
done