Changing file extensions in bulk using Git Bash

I just found myself in a situation where I needed to convert 30+ PHP files in to HTML files. Rather than do this manually, I had a look to see if I could do this using command line.

I found this StackOverflow question which gives a few different methods, however they didn’t all work for me as most used ‘rename’ functionality, which I didn’t have.  This answer was the one that worked fine for me using Git Bash on Windows.

To change all .php files in a folder to .html files, run the following command:

find . -name '*.php' -exec sh -c 'mv "$0" "${0%.php}.html"' {} \;

I used to try and make this a bit more human readable:

find .
Search for files within this directory
-name '*.php'
that has the file extenstion .php
-exec{} \;
Do the following for each file found
sh -c ''
This is a shell command
Move (rename) files
"$0" "${0%.php}.html"
I can tell this is taking the original file name, and replacing .php with .html, but can’t figure out what the expression means. Any help?

Forgive me if this is wrong, I’m new to this and trying to learn – please let me know any corrections!

Leave a Reply

Your email address will not be published. Required fields are marked *