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 10-21-2010, 03:27 PM   #1
FatalKeystroke
LQ Newbie
 
Registered: Oct 2010
Distribution: Debian Squeeze x86_64, #!crunchbang 9.04
Posts: 26

Rep: Reputation: 1
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)?
 
Old 10-21-2010, 05:19 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Code:
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
HTH

Forrest
 
Old 10-21-2010, 05:51 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You could do something like this, though it is cheating a bit:
Code:
rename '$a=(defined $a?$a:0)+1; s/.*/$a.JPG/' *.jpg

Last edited by neonsignal; 10-21-2010 at 05:56 PM.
 
Old 10-21-2010, 05:57 PM   #4
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Another alternative
Code:
#!/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
 
Old 10-21-2010, 08:10 PM   #5
FatalKeystroke
LQ Newbie
 
Registered: Oct 2010
Distribution: Debian Squeeze x86_64, #!crunchbang 9.04
Posts: 26

Original Poster
Rep: Reputation: 1
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.
 
Old 10-21-2010, 08:15 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well they will be in number order afterwards.
 
Old 10-21-2010, 08:26 PM   #7
FatalKeystroke
LQ Newbie
 
Registered: Oct 2010
Distribution: Debian Squeeze x86_64, #!crunchbang 9.04
Posts: 26

Original Poster
Rep: Reputation: 1
What I mean is, say the original files are:

a.jpg
c.jpg
b.jpg
d.jpg

will they be renamed like this?

a.jpg -> 001.JPG
c.jpg -> 003.JPG
b.jpg -> 002.JPG
d.jpg -> 004.JPG
 
Old 10-21-2010, 08:38 PM   #8
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by FatalKeystroke View Post
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.
 
Old 10-21-2010, 08:52 PM   #9
FatalKeystroke
LQ Newbie
 
Registered: Oct 2010
Distribution: Debian Squeeze x86_64, #!crunchbang 9.04
Posts: 26

Original Poster
Rep: Reputation: 1
Thank you!
I performed "echo *" in one of my more cluttered directories and it came out alphabetized.

Thank you very much for your help everyone!
 
  


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
Reading all files in a directory in an alphabetical order weeshalll Programming 7 11-25-2009 10:28 PM
Now LQ has so many distributions how about listing them in alphabetical order? catkin LQ Suggestions & Feedback 6 07-01-2009 09:07 AM
Kmenu: alphabetical order? sloteel Linux - Software 3 06-10-2008 04:17 PM
Arranging Files In Alphabetical order swatward Linux - General 4 12-11-2006 08:14 PM
copy files and make all filenames uppercase pas Linux - Newbie 1 12-19-2004 11:39 PM

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

All times are GMT -5. The time now is 04:48 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