How do I bulk rename multiple files named

Image401.jpg, Image402.jpg,...


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


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

for f in Image4*.jpg
  # replace Image4 prefix with Image0
  mv "$f" "$newname"

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

  • 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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