LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 01-28-2008, 09:53 PM   #1
snaggletooth1134
LQ Newbie
 
Registered: Jan 2008
Posts: 3

Rep: Reputation: 0
Bash script for renaming files (all odd)


I have a ton of picture files that I would like to rename.
I'm very new to bash, could someone help me out?

What I need is a script that will go down a directory and rename the files from P1180001.JPG, P1180002.JPG, ... to 0001.JPG, 0003.JPG, 0005.JPG ...

I also have another directory that looks just like this one as far as file name, but I need to rename them starting with 6, and I need them even:
P1180001.JPG, P1180002.JPG, ... to 0006.JPG, 0008.JPG, 0010.JPG ...

How can this be done?

Thanks.
 
Old 01-29-2008, 01:52 AM   #2
livetoday
Member
 
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
Can you provide a snippet of shell script that you are trying.....I might be able to modify it to fullfill the purpose.



Quote:
Originally Posted by snaggletooth1134 View Post
I have a ton of picture files that I would like to rename.
I'm very new to bash, could someone help me out?

What I need is a script that will go down a directory and rename the files from P1180001.JPG, P1180002.JPG, ... to 0001.JPG, 0003.JPG, 0005.JPG ...

I also have another directory that looks just like this one as far as file name, but I need to rename them starting with 6, and I need them even:
P1180001.JPG, P1180002.JPG, ... to 0006.JPG, 0008.JPG, 0010.JPG ...

How can this be done?

Thanks.
 
Old 01-29-2008, 02:09 AM   #3
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
nevermind, I realize my solution won't work ... solution pending.

Last edited by H_TeXMeX_H; 01-29-2008 at 02:15 AM.
 
Old 01-29-2008, 02:29 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
If I am reading your requirements correctly, you have a list of directories containing a number of duplicate filenames and you want all these files to have unique file names in another folder.

This code assumes that the files you want renamed have an extension of uppercase JPG.

I am renaming the files to lowercase extension to stop them being picked up in any subsequent run. However, they would be overwritten so be careful!
Code:
###
### Bash script to rename JPG files
###

## initialise counter
counter=1

## find all JPG files to be renamed
find . -type f -name '*.JPG'|while read l_orig
do
   if [ $counter -lt 10 ]
   then
      cp -p $l_orig img000${counter}.jpg
   else
      if [ $counter -lt 100 ]
      then
         cp -p $l_orig img00${counter}.jpg
      else
         if [ $counter -lt 1000 ]
         then
            cp -p $l_orig img0${counter}.jpg
         else
            cp -p $l_orig img${counter}.jpg
         fi
      fi
   fi

   ## Increment counter
   counter=`expr $counter + 1`
done
EDIT:-

I was using cp -p instead of mv for testing purposes, I would suggest that you do the same as this will not remove your original files.

Once you are happy that you have all your files you can then remove the originals (or re-run after changing the cp -p statements to mv)

Last edited by Disillusionist; 01-29-2008 at 02:41 AM.
 
Old 01-29-2008, 11:01 AM   #5
snaggletooth1134
LQ Newbie
 
Registered: Jan 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Hmm, it didn't work out the way I wanted it to. Let me try explaining it a little better.

I have two directories. One named 'Odd' and one named 'Even'.

I am looking for a script (or two separate ones, one for Odd and one for Even) that will go down the files in order and rename them accordingly.

Both directories have files in this exact format:
Code:
P1180001.JPG
P1180002.JPG
P1180003.JPG
P1180004.JPG
P1180005.JPG
This is what I want the script to do for the files in the 'Odd' folder:
Code:
P1180001.JPG     -->     0001.jpg
P1180002.JPG     -->     0003.jpg
P1180003.JPG     -->     0005.jpg
P1180004.JPG     -->     0007.jpg
P1180005.JPG     -->     0009.jpg
And this is what I want the script to do for the files in the 'Even' folder:
Code:
P1180001.JPG     -->     0002.jpg
P1180002.JPG     -->     0004.jpg
P1180003.JPG     -->     0006.jpg
P1180004.JPG     -->     0008.jpg
P1180005.JPG     -->     0010.jpg
Sorry for the poor explanation in my previous post.

I appreciate it,

Nick
 
Old 01-29-2008, 01:29 PM   #6
Big_Vern
LQ Newbie
 
Registered: Jan 2008
Posts: 9

Rep: Reputation: 1
Perhaps awk?

e.g for odd, something like this.. (not tested):

Code:
for oldfile in `ls`
do
 newfile=`echo $oldfile | awk '{y=substr($0,5,4);z=substr($0,9); printf("%0.4d%s\n", (y*2)-1, z)}'`
 mv $filename $newfile
done
Regards,
Big Vern
 
Old 01-29-2008, 09:55 PM   #7
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Rep: Reputation: 33
Read your post wrong, but I have a idea on what may work in bash.

Last edited by davimint; 01-29-2008 at 10:20 PM. Reason: Read post wrong, working on solution
 
Old 01-29-2008, 10:27 PM   #8
snaggletooth1134
LQ Newbie
 
Registered: Jan 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks guys.

I tried out Big Vern's method and it worked so that's the one I'm using. The only thing he forgot to do is put $oldfile in place of $filename.

Thanks.
 
Old 01-29-2008, 10:29 PM   #9
livetoday
Member
 
Registered: Jun 2006
Location: India
Distribution: RHEL,Suse,Fedora
Posts: 106

Rep: Reputation: 15
try this for first 'odd' directory:

#!/bin/bash
echo "Enter the number of last file i.e. P11800013.JPG enter only 13"
read f
for ((i=$f;i>1;i=i-1))
do
s=$((i*2-1))
mv P118000$i.JPG P118000$s.JPG
done

You may use cp instead of mv to be secure.

If successful modify it to create 'even' directory.

Quote:
Originally Posted by snaggletooth1134 View Post
Hmm, it didn't work out the way I wanted it to. Let me try explaining it a little better.

I have two directories. One named 'Odd' and one named 'Even'.



I am looking for a script (or two separate ones, one for Odd and one for Even) that will go down the files in order and rename them accordingly.

Both directories have files in this exact format:
Code:
P1180001.JPG
P1180002.JPG
P1180003.JPG
P1180004.JPG
P1180005.JPG
This is what I want the script to do for the files in the 'Odd' folder:
Code:
P1180001.JPG     -->     0001.jpg
P1180002.JPG     -->     0003.jpg
P1180003.JPG     -->     0005.jpg
P1180004.JPG     -->     0007.jpg
P1180005.JPG     -->     0009.jpg
And this is what I want the script to do for the files in the 'Even' folder:
Code:
P1180001.JPG     -->     0002.jpg
P1180002.JPG     -->     0004.jpg
P1180003.JPG     -->     0006.jpg
P1180004.JPG     -->     0008.jpg
P1180005.JPG     -->     0010.jpg
Sorry for the poor explanation in my previous post.

I appreciate it,

Nick
 
Old 01-29-2008, 10:43 PM   #10
davimint
Member
 
Registered: Jan 2006
Distribution: Slackware Current
Posts: 272

Rep: Reputation: 33
Something simple for the odd numbers.

Code:
#!/bin/bash 
#
num=1
for file in *.JPG; do
        mv "$file" "$(printf "%04u" $num).jpg"
        let num=num+2
done
Change line 3 to "num=0" for even numbers.

Last edited by davimint; 01-29-2008 at 10:46 PM.
 
1 members found this post helpful.
Old 05-01-2012, 12:26 AM   #11
oldmankit
LQ Newbie
 
Registered: May 2012
Posts: 1

Rep: Reputation: Disabled
Cool

Quote:
Originally Posted by davimint View Post
Something simple for the odd numbers.

Code:
#!/bin/bash 
#
num=1
for file in *.JPG; do
        mv "$file" "$(printf "%04u" $num).jpg"
        let num=num+2
done
Change line 3 to "num=0" for even numbers.
I had the same needs and so was happy to find this thread. davimint - your solution worked perfectly. Thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash script for sorting and renaming multiple mp3 files by id3 tags simonloach Linux - General 8 02-16-2013 09:07 AM
Script for renaming files auwolf Linux - Newbie 1 10-03-2006 02:39 AM
bash help renaming files kahn Programming 6 06-16-2005 07:15 AM
How do I make a bash sheet for renaming files? Cyberian Linux - Newbie 16 03-16-2005 08:08 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration