Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I bulk rename multiple files named

Image401.jpg, Image402.jpg,...

to

Image 001.jpg, Image002.jpg, ... ?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

You just want to change '4' to '0'?

for f in Image4*.jpg
do
  # replace Image4 prefix with Image0
  newname="Image0${f#Image4}"
  mv "$f" "$newname"
done

Or you want to subtract 400 from the numeric part? Or something else?

share|improve this answer
1  
mv "$f" Image0"${f#Image4}" is a much faster and safer way to do this. The $( echo | sed ) approach, even if it works on filenames like Image401.jpg, is not very robust as it will mangle filenames containing odd characters or spaces, and is much slower because of the extra temporary subshell and processes created for every single file. –  jw013 Mar 12 '12 at 18:44
    
All true - and it wasn't even the best sed command to use. –  Useless Mar 12 '12 at 18:59
add comment

Under Linux, to change the first 4 in the file names to 0:

rename 4 0 Image4*.jpg

Under Debian, Ubuntu and derivatives, rename is a different file renaming program, which is based on a Perl expression. Either call rename.ul instead, or call the Perl renaming script:

rename 's/4/0/' Image4*.jpg
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.