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 explainshell.com 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
-
mv
- 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!
I’ll try my best to give a possible explanation for this last part (since I’m not entirely sure either).
“${0%.php}.html”
The $ likely tells this to evaluate the following expression, and the 0% may be a regular expression specifying “0 or more characters (using ‘%’ in place of the standard ‘*’)”. I haven’t tested this with the ‘*’ character, but it may be interchangeable with the ‘%’.