Batch rename file extensions in Mac OS X Terminal

A copy & paste mac os x terminal command that simply does what the title says: rename the extensions of multiple files in multiple directories (recursively)

I downloaded a webshop HTML-template collection I liked, but it was customized for a system I don't use. Result: many files in various folders that had some kind of functionality in it, had an extension of .liquid, and which I thus needed to rename to .php.

But here is the problem: there were many files with that extension, and they were situated in multiple folders. It would be too much work to manually rename them all, and I also wasn't prepared to buy a piece of software because I knew the terminal should be able to help me.

The code

I tried several snippets (like mv *.liquid *.php), but they didn't work. It also wasn't recursive, so I would still have to manually visit every folder and repeat the command.

Then I found this, and it worked like a charm. It finds all .liquid files and renames (mv) them to {filename}.php:

find . -iname "*.liquid" -exec bash -c 'mv "$0" "${0%\.liquid}.php"' {} \;

A word of warning

Make sure you move to the right directory in the terminal (and nothing higher!), or risk renaming a lot more files ... ;)


For those of you who like to watch, this is how it looked before:

Renamed Before

Enter the command, hit Enter, ... and less than a second afterwards it looked like this:

Renamed After

What's next?

Not much really. But if you think this was useful to you, it might be for others also. Feel free to share:

Email, LinkedIn, WhatsApp, or click / push the link below to copy it: