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

Comments

  • Werner Krauss 22/08/2017 9:01am (4 months ago)

    Sometimes programs with UI might be comfortable, but a basic knowledge of bash and its power can help you everywhere - even now on windows.

  • Dylaner 06/08/2017 11:09am (4 months ago)

    u can also do this by programs like KrojamSoft BatchRename
    try it so u can save time

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