LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2020, 09:32 PM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Rep: Reputation: 31
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
 
Old 07-31-2020, 09:55 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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?
 
Old 07-31-2020, 11:15 PM   #3
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
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
 
Old 08-01-2020, 12:06 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
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.
 
Old 08-01-2020, 05:11 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by Johng View Post
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 View Post
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.
 
Old 09-24-2020, 06:58 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: GUI To Batch Rename Files On Linux With Exif And Music Tags Support: Inviska Rename LXer Syndicated Linux News 0 05-25-2019 12:07 PM
Trying to batch rename some files in bash, no luck. Any ideas? sajro Linux - Newbie 17 03-26-2010 03:57 PM
Batch Script to rename files... jamie_barrow Linux - Newbie 16 06-14-2009 01:26 PM
Bash - Batch File Rename Help... emailarron Linux - Newbie 4 01-26-2006 07:35 AM
Got a script to rename a batch of files? jamie_barrow Linux - General 1 08-08-2003 06:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration