Batch rename filename extension on a Mac

June 2nd, 2008, filed under Apple, General

Here’s a little shell script I use for renaming the filename extension of multiple files:

#!/bin/sh
for f in *.$1
do
  mv "$f" "${f%$1}$2"
done

I’ve named this script chgext, and use the following command to rename multiple filename extensions:

~$: chgext JPG jpg

This script will rename the filename extension, for example an uppercase ‘JPG’ extension, into a lowercase ‘jpg’ extension of all files in the same directory matching your criteria.

Installation

Enter your terminal and create a new file named chgext using the following command:

~$: touch chgext

Open the file in an editor of your choice. I often use nano:

~$: nano chgext

Copy and paste the above script in the editor and save it (use ctrl + x in nano and save the changes). In order to access this script from any location on your system it can be placed in the /bin/ directory. You need root access to this directory:

~$: sudo mv chgext /bin/chgext

…and make the script executable:

~$: sudo chmod +x /bin/chgext

To see if the script is properly installed enter the command chgext. It should respond with a message similar to “mv: rename *. to *.: No such file or directory”. That’s ok, because we haven’t added any parameters yet.

Example:

Example using chgext. See full image

3 Responses

Hi,

Je bent niet zo actief meer op je blog zo te zien? Ziet er wel indrukwekkend uit dat chgext script van je. :)
Druk werkervaring aan het op doen?

Groetjes,
Inge

Posted by

thanks men! I love this, have been using for a while now, very useful!!!

Posted by

[...] named this script lowerit and placed it in the /bin directory. You can read my previous post “Batch rename filename extension on a Mac” for more details about installing bash scripts on a Mac. Run this script by typing the [...]

Posted by

Have your say

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>