How to Change File Extensions of Files via Command Line in Mac OS

May 12, 2017 - 6 Comments

You can change all file extensions in a directory easily via Terminal in macOS

Command line users may find it helpful to change and rename the file extension of a group of files in a directory. For example, let’s say you have a batch of files with the file extension “.txt” but you want all of those the file extensions to be “.py” instead. That’s what we’re going to demonstrate here by relying on simple bash scripting to change a group of file extensions at the command line of Mac OS / OS X, but this will also work in linux and other unix flavors too.


Before beginning, realize this is not changing a file type, it is only changing the file extension. Additionally, this walkthrough approach is intentionally using the command line and is thus aimed at more advanced users. However, the Terminal is not the only way to do this, and so if this is too advanced or irrelevant to your user skill set then recall that Mac OS offers simple tools to both batch rename files in Mac OS and batch change file extensions in the Finder as well, neither of which requires the command line at all. OK? Alright on to the command line approach using a simple one line bash script.

By the way you should always make a copy and/or backup of the files you are modifying, particularly if you are new to the command line. Failure to do so could result in data loss, the Terminal is unforgiving to typos or mistakes, so don’t skip your backups.

How to Change All File Extensions in a Directory via the Command Line

Let’s take a few examples. In the first example, we’re going to change all files in the present working directory with the extension “.txt” and change them to “.py” instead. Assuming you are in the directory where you wish to change all files in the current directory to a new file extension, here is the syntax to use:

for file in *.txt; do mv "$file" "${file%.txt}.py"; done

Note this uses the wildcard * meaning anything and everything in the current directory matching “*.txt” file extension will be changed. Also note the multiple instances of “txt” and the one instance of “py” both of which you’d want to change to fit your requirements.

All this is doing is creating a simple loop where files matching the wildcard and initial file extension are found, and then performing the “mv” command to move(rename) those files from the initial file extension to the replacement. Pretty simple, right?

Let’s take another example, let’s say we have a collection of image files in the current directory with file names like “blahblah.jpg.JPEG” but we really just want them to all have the same file name of the less redundant and easier to read “blazblah.jpeg”. In that case, the syntax would look like the following:

for file in *.jpg.JPEG; do mv "$file" "${file%.jpg.JPEG}.jpeg"; done

The command line approach to changing file extensions is quite fast and the file extensions will be changed rapidly, with no warning dialogs or confirmations.

And to reiterate once again, this is not to convert any file types or change anything but the file extension name.

Know of a better way to change a bunch of file extensions of files in a directory via the command line? Just want to browse through some other great command line tips (go here)? Let us know your thoughts in the comments!

.

Related articles:

Posted by: Paul Horowitz in Command Line, Tips & Tricks

6 Comments

» Comments RSS Feed

  1. jurek says:

    this doesn’t work, it only adds yet another extension

  2. Edgar says:

    Wow that’s really complicated compared to good old DOS.

    ren *.txt *.py

  3. tulpan says:

    try KrojamSoft BatchRename

  4. KB says:

    You can also try this for all .txt to .py for example

    for old in *.txt; do mv $old `basename $old .txt`.py; done

  5. Fnordmeister says:

    > Let’s take another example, let’s say we have a collection of image files in the current directory with file names like “blahblah.jpg.JPEG” but we really just want them to all have the same file name of the less redundant and easier to read “blazblah.jpeg”.

    I think there’s a typo there. Otherwise, you need to explain how to change the root blahblah to blazblah.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site