Batch renaming files and adding padded zeros to numbers

Today a customer sent me some hundred images I should add to an image gallery. Unfortunately the files were renamed under windows: uppercase JPG extension and - really annoying - the numbers didn't have leading zeros. Fortunately every Linux distribution has the "rename" command, so I fired up a Vagrant VM and renamed the files.

First I renamed the .JPG extensions to .jpg:

rename -n -f 's/\.JPG$/\.jpg/' *.JPG

The -n flag means "dry run", remove it when you actually rename the files. -f means force, which would overwrite existing files. I needed this, cause the files are synced with my Windows harddisk.

Next I removed white spaces and brackets:

rename -n 's/[\(\)]//' *

And finally I adjusted this answer I found on StackOverflow to fit my needs:

rename 's/(\d+)\.jpg/sprintf("%03s.jpg", $1)/e' *


Rate this post

Post your comment


No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments