Batch Renaming Image Files on the MacOS Command Line with the 'rename' Perl Utility

Had the following image files and wanted to batch rename them:

$ ls
content___media_external_images_media_231 content___media_external_images_media_235 content___media_external_images_media_239 content___media_external_images_media_243 content___media_external_images_media_232 content___media_external_images_media_236 content___media_external_images_media_240 content___media_external_images_media_244 content___media_external_images_media_233 content___media_external_images_media_237 content___media_external_images_media_241 content___media_external_images_media_234 content___media_external_images_media_238 content___media_external_images_media_242

The rename Perl Utility

In this answer the poster recommends the rename command. It is a Perl utility that can be installed easily the 3rd party Homebrew package manager for MacOS.

Before installing the package let's use some Homebrew commands to view information about the package before installing it.

Lookup Package Information with Homebrew

We can find a short package description with the following brew desc command:

$ brew desc rename
rename: Perl-powered file rename script with many helpful built-ins

Also the following brew home command will launch the homepage for the package:

$ brew home rename

In this case the package homepage appears to be and happens to include documentation and a tutorial for the command.

Rename the Image Files

I just required a simple replacement; swapping a static portion of text with another:

rename s/content___media_external_images_media/misc_pics_/g *

This retained the numbers at the end of the filenames and ended up looking like this:

$ ls
misc_pics_231 misc_pics_235 misc_pics_239 misc_pics_243 misc_pics_232 misc_pics_236 misc_pics_240 misc_pics_244 misc_pics_233 misc_pics_237 misc_pics_241 misc_pics_234 misc_pics_238 misc_pics_242


 Mac  command line  Perl Utilities - rename  Perl  Perl utilities  Homebrew