LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Batch process bash rename script (https://www.linuxquestions.org/questions/programming-9/batch-process-bash-rename-script-4175679737/)

Johng 07-31-2020 09:32 PM

Batch process bash rename script
 
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


michaelk 07-31-2020 09:55 PM

Code:

read -a FILENAME  #read -e FILENAME  ###THE PART THAT DOES NOT WORK
The read statement is prompting for input from stdin i.e the keyboard. There is no prompt text so your script will just stop unwittingly until you press the enter key.

Code:

for f in "$dirname"/*; do
This iterates through the directory structure with an alphanumeric sort i.e. the same output when you type in the ls -l command. The $f variable for each loop contains the filename but from a quick scan is never used in your script.

What is the desired sequence?

Johng 07-31-2020 11:15 PM

Thank you michaelk
Quote:

The $f variable for each loop contains the filename but from a quick scan is never used in your script.
That was the question. Thank you, that's the answer

syg00 08-01-2020 12:06 AM

It's your data, so I imagine you know its structure, but I never like using presumptions of fixed length. Bash now has pretty good regex support - maybe look into that to extract your substrings.

ondoho 08-01-2020 05:11 AM

Quote:

Originally Posted by Johng (Post 6151545)
Thank you, that's the answer

Are you saying your thread is SOLVED now?
Doesn't look like it to me.

Quote:

Originally Posted by Johng (Post 6151530)
change the image name from STA_1234.JPG to 1234-STA.jpg.

You have to find what is consistent in the filename.
Assuming the underscore _ is consistent, I'd write myself a quick oneliner:
Code:

for img in *JPG; do ext=${img##*.}; base=${img%.*}; echo mv "$img" "${base##*_}-${base%_*}.${ext,,}"; done
ext = extension (i.e. "jpg")
(not tested)
If you like what you see, remove the "echo".
You might need to adjust whether you want to delete the shortest or longest match. See here.

David the H. 09-24-2020 06:58 AM

If the filenames are all uniform, you may be able to simplify it down to something like this:

Code:


for file in "$@"; do

  IFS='_.' read -r pre main ext <<<"$file"
  mv "$file" "$main-$pre.$ext"

done

read can do all the word-splitting for you in one go, based on the characters defined in IFS, and store them in separate variables (or an array).

It might be not work as desired, however, if a name can have multiple IFS characters in it.


All times are GMT -5. The time now is 12:20 PM.