[SOLVED] Bash: How can I rename files in alphabetical order and make extensions uppercase?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash: How can I rename files in alphabetical order and make extensions uppercase?
I am trying to write a bash script that will extract a .cbr (.rar) file, traverse the extracted files in alphabetical order and rename them 001.JPG, 002.JPG, 003.JPG, etc.
So far I only have this much to extract it:
Code:
#!/bin/bash
#
#
inputfile="${1}"
extractdir="${inputfile%.cbr}"
mkdir "${extractdir}"
cd "${extractdir}"
unrar e "../${1}"
Can anyone recommend how I might go about renaming the files as specified above while retaining their original order (and changing all lowercase extensions to uppercase)?
for FILE in * ; do
let COUNT=COUNT+1
if [ ${COUNT} -lt 10 ] ; then
NEWFILE=00${COUNT}.jpg
elif [ ${COUNT} -lt 100 ] ; then
NEWFILE=0${COUNT}.jpg
else
NEWFILE=${COUNT}.jpg
fi
if [ ! -e ${NEWFILE} ] ; then
mv ${FILE} ${NEWFILE}
fi
done
#!/bin/bash
def=000
for f in *;do
((cnt++))
if [[ ! -e "${def:${#cnt}:${#def}}${cnt}.JPG" ]];then
echo mv "${f}" "${def:${#cnt}:${#def}}${cnt}.JPG" #remove preceding echo if everything is fine
fi
done
If you want to change the format to something like 0001.jpg,0002./jpg, i.e. to 4 digits then just change 'def' to
Code:
def=0000
Remove the 'echo' if the output looks ok.
Last edited by crts; 10-21-2010 at 06:00 PM.
Reason: corrected extension
Thank you!
I'm going to use the solution given by forrestt since it's the only one I understand (I'm still learning bash scripting). The other two are beyond my expertise right now.
I still have one question though. Will this keep the files in alphabetical order? Does bash automatically sort files alphabetically when processing them this way?
(I have little way of checking to make sure they are still in order afterwards)
Thank you for the help!
Last edited by FatalKeystroke; 10-21-2010 at 08:18 PM.
Reason: Other replies made before this was submitted.
Will this keep the files in alphabetical order? Does bash automatically sort files alphabetically when processing them this way?
The glob expansion in the scripts (the '*') will sort in collation order. Depending on your environment settings, this might do what you want (strict "C" ordering would place uppercase ahead of lowercase, whereas "en_US" for example will be alphabetical).
Just do the following to see if you get the ordering you want:
Code:
echo *
Incidentally, if any of the filenames have spaces in them, you might need some judicious quotes in the script you intend to use.
Last edited by neonsignal; 10-21-2010 at 08:46 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.