[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Shell script to rename files

Posted on 2006-03-31
5
Medium Priority
?
670 Views
Last Modified: 2010-04-21
I have a bunch of files that I need renamed.  The files are names like 20509220.jpg  20509220_1.jpg 20509220_2.jpg etc...  where the 20509220 part is just some number.
I want to change the names of the files so there are no files without a _[1-9] at the end.  So I need to:

20509220.jpg --> 20509220_1.jpg
20509220_1.jpg --> 20509220_2.jpg
20509220_2.jpg --> 20509220_3.jpg
.....
20509220_9.jpg --> 20509220_10.jpg

just to make sure I'm being clear, I have files with the following filenames (the number before the underscore could be any number):

20509220.jpg
20509220_1.jpg
20509220_2.jpg
20509220_3.jpg
20509220_4.jpg
20509220_5.jpg
20509220_6.jpg
20509220_7.jpg
20509220_8.jpg
20509220_9.jpg

and I want to change them all to have filenames like:

20509220_1.jpg
20509220_2.jpg
20509220_3.jpg
20509220_4.jpg
20509220_5.jpg
20509220_6.jpg
20509220_7.jpg
20509220_8.jpg
20509220_9.jpg
20509220_10.jpg

If anyone needs more clarification, let me know!!  I'm fairly good with Linux, but not nearly good enough to create a script like this.  Any help is appreciated.  Thanks!!
btw...I'm using bash.
0
Comment
Question by:wotech
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 16348472
#!/bin/bash
perl -e 'rename $_->[0],"$_->[1]_".($_->[3]+1).".jpg" or warn "$_->[0] $!" for sort{0+$b->[3]<=>0+$a->[3]}map{[/((.*?)(_(\d+))?\.jpg$)/]}@ARGV' *.jpg
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16353483
perl -e 'for $x (sort{$a<=>$b}<*.jpg>){$n=$x;$n=~s/([^_]*)(_\d+)?\.jpg/$1/;rename$x,$n."_".++$y.".jpg"}'
0
 
LVL 85

Expert Comment

by:ozo
ID: 16354930
I think you need to do higher $y before smaller $y, otherwise you
rename "20509220_1.jpg", "20509220_2.jpg" over the old 20509220_2.jpg
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16355130
ozo, I guessed that too when I wrote that, but in my tests it didn't overwrite, suprise ;-)
don't know why perl is that clever here, but it worked for me with given test example from question, still wondering ...
Anyway, reversing the order is more secure.
0
 

Author Comment

by:wotech
ID: 16355927
thanks ozo, worked great.
just for others that are reading this in the future....
everytime you run the script in the folder with the images, the suffix numbers (_1, _2, etc) will be increased by one.   so if you have 20509220_1.jpg, 20509220_2.jpg, etc  and you run the script, you get  20509220_2.jpg, 20509220_3.jpg, etc.

It doesn't really matter, it just threw me for a loop so I thought I'd mention it.  
Thx again!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question