We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Shell script to rename files

wotech
wotech asked
on
Medium Priority
699 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.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
#!/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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
perl -e 'for $x (sort{$a<=>$b}<*.jpg>){$n=$x;$n=~s/([^_]*)(_\d+)?\.jpg/$1/;rename$x,$n."_".++$y.".jpg"}'
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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
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.

Author

Commented:
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!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.