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 |
Code:
read -a FILENAME #read -e FILENAME ###THE PART THAT DOES NOT WORK Code:
for f in "$dirname"/*; do What is the desired sequence? |
Thank you michaelk
Quote:
|
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.
|
Quote:
Doesn't look like it to me. Quote:
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 (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. |
If the filenames are all uniform, you may be able to simplify it down to something like this:
Code:
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. |