This article will explain how to change the file extension for all files in a directory in Linux using a simple bash shell command. While you are on a learning path, be sure to checkout other useful Linux articles such as Insert/Add String to Beginning of a File and Linux – How to find the number of files in a folder.
Change from one extension to another in Linux
The command below will rename all files with the extension .php4 to .phpfor f in *.php4; do mv $f `basename $f .php4`.php; done;
Add (append) an extension to all files
The command below add the extension .txt to all files in the directoryfor f in *; do mv $f `basename $f `.txt; done;
Remove (delete) an extension from all files
The command below remove the extension .txt from all files in the directoryfor f in *.txt; do mv $f `basename $f .txt`; done;
Actually, #1 above should read:
for f in *.php4; do mv $f `basename $f .php4`.php; done;
Try it for yourself!
Jeff,
Thank you very much for the correction. I have updated the article to reflect what you wrote.
Thanks!
heat
For the sake of correctness, everything should be surrounded with quotes.
So:
for f in “*.php4”; do mv “$f” “`basename “$f” .php4`.php”; done;
Those pesky spaces will trash all that data if we’re not careful.
Thank you for this beautiful and functional piece of code it did exactly what I needed it to do. 🙂
working script to remove extension is in my case:
for f in *.php4; do mv “$f” “`basename “$f” .php4`”; done;
hi i want to know how to change one file extension into another extension.
Example: i have text file which is by defalt geditor file so i want to open it through libre Office file, so how can i change the extension of a file through CLI (Command) mode?
Thanks! That is very concise and helpful! 🙂